Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xslt

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'?

like image 992
Priidu Neemre Avatar asked Sep 24 '12 16:09

Priidu Neemre


People also ask

What is the difference between XSLT and XSL?

XSLT is designed to be used as part of XSL. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.

What are XSL and XSLT used for?

XSL Formatting Objects (XSL-FO) an XML vocabulary for specifying formatting semantics. An XSLT stylesheet specifies the presentation of a class of XML documents by describing how an instance of the class is transformed into an XML document that uses a formatting vocabulary, such as (X)HTML or XSL-FO.

What is the difference between XML and XSLT?

XML and XSLT. ❮ Previous Next ❯. With XSLT you can transform an XML document into HTML. XSLT (eXtensible Stylesheet Language Transformations) is the recommended style sheet language for XML. XSLT is far more sophisticated than CSS. With XSLT you can add/remove elements and attributes to or from the output file.

What is the <XSL> element?

Definition and Usage The <xsl:element> element is used to create an element node in the output document.

What can you do with XSLT?

With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.

What is XSLT conversion code?

Also covers the Importance of XPath to Develop XSLT Conversion Code: The term “XSLT” is generated by combining two words i.e. ‘XSL’ and ‘T’, ‘XSL’ is the short form of ‘Extensible Stylesheet Language’ and ‘T’ is a short form of ‘Transformation’.


1 Answers

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.

like image 175
Ian Roberts Avatar answered Nov 14 '22 21:11

Ian Roberts