Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an XSL document look like if the mirrors the input data?

Tags:

xslt

The typicle XSL usage is:

XML1.xml -> *transformed using xsl* -> XML2.xml

How does an XSL document look like, if I want to simply mirror the input data?

ex:

XML1.xml -> *transformed using xsl* -> XML1.xml
like image 201
corgrath Avatar asked Apr 17 '11 15:04

corgrath


People also ask

What is XSL explain with example?

XSL is a language for expressing style sheets. An XSL style sheet is, like with CSS, a file that describes how to display an XML document of a given type. W3C developed XSL because an XML based stylesheet language was needed to transform XML documents.

How is XSL style sheet declared?

The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>. Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used!

What is output format in XSLT?

The method attribute can have the value xml , html , or text for XML, HTML, and text output, respectively.


2 Answers

How does an XSL document look like, if I want to simply mirror the input data?

There are more than one answers to this question, however all of them could be named "Identity Transform":

  1. <xsl:copy-of select="/"/> This is the shortest, simplest, most efficient and most inflexible, non-extensible and unuseful identity transform.

  2. The classical identity rule, which everybody knows (or should know):

_

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

This is still very short, one-template transformation, which is so much more extensible and useful identity transform, known also as the "identity rule". Using and overriding the identity transform is the most fundamental and powerful XSLT design pattern, allowing to solve common copy and replace/rename/delete/add problems in just a few lines. Maybe 90%+ of all answers in the xslt tag use this form of the identity transform.

.3. The fine-grained control identity rule, which everybody should know (and very few people know):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="@*|node()[1]"/>
  </xsl:copy>
  <xsl:apply-templates select="following-sibling::node()[1]"/>
 </xsl:template>
</xsl:stylesheet>

This is similar to the generally known identity rule defined at 2. above, but it provides a finer control over the XSLT processing.

Typically with 2. the <xsl:apply-templates select="@*|node()"> triggers a number of transformations (for all attributes and child nodes), that can be done in any order or even in parallel. There are tasks where we don't want certain types of nodes to be processed after some other nodes, so we have to plumb the leakage of the identity rule with overriding it with empty templates matching the unwanted nodes and adding other templates in a specific mode to process these nodes "when the time comes"...

.3. is more appropriate for tasks where we want more control and really sequential-type processing. Some tasks that are very difficult to solve with 2. are easy using 3.

like image 174
Dimitre Novatchev Avatar answered Oct 24 '22 05:10

Dimitre Novatchev


It would look like the identity transform:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

This is one of the most fundamental XSLT transforms. It matches any attribute or other node, copies what it matches, and then applies itself to all attributes and child nodes of the matched node.

This turns out to be quite powerful for other tasks, too. A common requirement is to copy most of a source file unchanged, while handling certain elements in a special way. This can be solved using the identity transform plus one template for the special nodes. It's a generic, flexible (and short) solution.

like image 35
Wayne Avatar answered Oct 24 '22 07:10

Wayne