What is the difference between using XSLT and declaring elements literally?

后端 未结 4 1297
旧时难觅i
旧时难觅i 2020-12-20 20:59

I have started using XSLT just recently and am wondering what the effective difference is between using for defining elements vs. just decla

相关标签:
4条回答
  • 2020-12-20 21:20

    xsl:element allows defining elements whose name you don't know when writing the stylesheet and also element whose names are dynamically created. I would always use the inline definition (i.e. by hand) if possible. It is shorter and I consider it more readable.

    0 讨论(0)
  • 2020-12-20 21:21

    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.

    0 讨论(0)
  • 2020-12-20 21:32

    One other technical difference not yet noted in the answers: if you restrict yourself to the xsl:element constructor, your stylesheet can be valid against the DTD for XSLT. In some environments (where you have a DTD-aware editor, for example) that can be advantageous. That said, I have been told many times that no one actually uses a DTD to guide their XSLT construction (except me, that is).

    There is also a metaphysical difference (literal result elements can be viewed as a form of tag abuse, because you're using an h1 where the meaning is not "this is a first-level heading" but instead "put a first-level heading here"), but this is of interest primarily to markup theorists and language designers; I mention it only for completeness' sake.

    0 讨论(0)
  • 2020-12-20 21:34

    I would strongly advise using literal result elements everywhere this is possible.

    Therefore, if the name of the element is known in advance (not determined dynamically) and there are no namespace manipulations required, then use a literal-result element.

    The major advantage of using a literal-result element is its readability and less moving parts than compared with xsl:element -- which means less error-prone.

    0 讨论(0)
提交回复
热议问题