Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
997 views
in Technique[技术] by (71.8m points)

xml - What is the difference between using XSLT <xsl:element> and declaring elements literally?

I have started using XSLT just recently and am wondering what the effective difference is between using <xsl:element> for defining elements vs. just declaring them as literals in the XSLT. For example, lets take a simplified case where I'm converting the contents of a tiny XML document into (x)HTML.

1.I could go with the <xsl:element> way:

<xsl:element name="h1">
    <xsl:value-of select="heading"/>
</xsl:element>

2. Or define the element by hand:

<h1>
    <xsl:value-of select="heading"/>
</h1>

What is the actual difference between these two and if a difference exists, which of them is considered 'good-style'?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

They're almost identical, the exception being that a literal <h1> element will add to the result tree the namespace nodes that are in scope at that point in the stylesheet, whereas the <xsl:element name="h1"> won't. What difference this makes to your output depends on exactly what namespace declarations your stylesheet includes and where in the result tree you make use of them, if at all. For example, run against any input XML document the following transform:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:foo="http://example.com">

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <root>
      <foo:element1 />
      <foo:element2 />
    </root>
  </xsl:template>
</xsl:stylesheet>

produces the following output (using xsltproc):

<?xml version="1.0"?>
<root xmlns:foo="http://example.com">
  <foo:element1/>
  <foo:element2/>
</root>

but changing the literal <root> in the stylesheet to <xsl:element name="root"> instead produces

<?xml version="1.0"?>
<root>
  <foo:element1 xmlns:foo="http://example.com"/>
  <foo:element2 xmlns:foo="http://example.com"/>
</root>

as the <xsl:element> form doesn't attach the "foo" namespace node to the generated element. If this matters, and you actually want to copy the stylesheet namespace declarations onto an element you create with <xsl:element> you can do so by nesting something like

<xsl:copy-of select="document('')/*/namespace::foo" />

directly inside it (using the idiom of document('') which provides access to the stylesheet XML document itself).

But generally, the main use of <xsl:element> is when the element name is calculated rather than a "compile-time" literal.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...