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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…