Herong's Tutorial Notes On XML Technologies
Dr. Herong Yang, Version 3.04

XSL - Formatting Output

Part:   1  2  

(Continued from previous part...)

2. Using "element" and "attribute" statements:

element: An XSL element, serving as an output statement. It inserts an XML element with the specified name into the output.

<element name="e_name">
 XSL statements
</element>

where "e_name" is used as the name of the element to be inserted into the output. Output produced by the enclosed XSL statements will be used as the content of the "e_name" element.

attribute: An XSL element, serving as an output statement. It inserts an XML attribute with the specified name into the output.

<attribute name="a_name">
 XSL statements
</attribute>

where "a_name" is used as the name of the attribute to be inserted into the output. Output produced by the enclosed XSL statements will be used as the value of the "a_name" attribute. Of course, "attribute" statements can only be used inside an "element" statement.

Here is an exampple:

<xsl:template match="update">
 <xsl:element name="old_update">
  <xsl:attribute name="old_date">
   <xsl:value-of select="@date"/>
  </xsl:attribute> 
 </xsl:element> 
</xsl:template>

In this template, I am trying to copy the "update" element from the source to the output. All the child nodes and attributes of "update" are ignored, except the "date" attribute.

There are two more XML related XSL elements: "text" and "comment".

text: An XSL element, serving as an output statement. It inserts an XML text node into the output.

<text>
 XSL statements
</text>

The output produced by the enclosed XSL statements will be used as the content of the text node.

comment: An XSL element, serving as an output statement. It inserts an XML comment node into the output.

<comment>
 XSL statements
</comment>

The output produced by the enclosed XSL statements will be used as the content of the comment node.

3. Using "copy" statement:

copy: An XSL element, serving as an output statement. It inserts the current node into the output.

<copy>
 XSL statements
</copy>

The behavior of the "copy" statement depends on the type of the current node:

  • If the current node is an element node, an XML element of the same name will be inserted into the output. Child nodes and attributes will not be copied automatically.
  • If the current node is an attribute node, an XML attribute of the same name will be inserted into the output. The value is also copied.
  • If the current node is a text node, the text content is copied into the output.
  • If the current node is a comment node, the entire node is copied into the output.
  • If the current node is a processing-instruction node, the entire node is copied into the output.

A good example of how to use the "copy" statement is this identity template:

<xsl:template match="*|@*|comment()|processing-instruction()|text()">
 <xsl:copy>
  <xsl:apply-templates
   select="*|@*|comment()|processing-instruction()|text()"/>
 </xsl:copy>
</xsl:template>

Conclusion:

  • "output" statement can be used to specify the format type of the transformation output.
  • "element" statement can be used to define an element in the output.
  • "attribute" statement can be used to define an attribute in the current element in the output.
  • "text" statement can be used to define a text string in the current element in the output.
  • "copy" statement can be used to copy the current node (element, attribute, or other type of node) to the output.

Question: Any one knows what "preserve-space" is for?

Part:   1  2  

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes On XML Technologies - XSL - Formatting Output