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

XSL - Getting Values Out Of Source Elements

Part:   1  2   3 

(Continued from previous part...)

Openning dictionary_xsl.xml with Internet Explorer, I got:

  dictionary
  
   word
    |-acronym=true
    |-symbol=
    name
     XML
    definition
     eXtensible Markup 
Language.
    date
     |-acronym=2002-12-23
   word
    |-acronym=
    |-symbol=true
    name
     <
    definition
     Mathematical symbol representing the "less than" logical 
operation, like: 1<2.
    definition
     Reserved symbol in XML representing the beginning of 
tags, like: <p>Hello world!</p> 

Note that:

  • The line breaks in text contents of source element were maintained by the "value-of" statement.
  • The entities, like &lt;, in text content of source element were also maintained by the "value-of" statement. They were presented by IE with their values, like <.
  • The comment in the source file was ignored. But the link break that starts the comment line was maintained. This is why I got a blank line under "dictionary".
  • The "value-of" statements on undefined attributes returned empty strings.

The 'for-each' Element

The XSL statement we have learned so far are all against an element (or elements if they have the same name) with a specific name, or an attribute with a specific name. Now, let's learn how to work with a group of elements or attributes.

for-each: An XSL element, serving as a loop statement. It loops through a group of elements or attributes in the XML source file that matches a specified criteria, and exectues the enclosed XSL statements at each iteration. Let's look at three simple syntax formats of the "for-each" statement first:

<xsl:for-each select="child_element_name">
 XSL statements
</xsl:for-each>
<xsl:for-each select="*"/>
 XSL statements
</xsl:for-each>
<xsl:for-each select="@*"/>
 XSL statements
</xsl:for-each>

where the "select" attribute defines the group of source elements or attributes, and the value of this attributes specifies which elements or attributes are in this group: "child_element_name" specifies all child elements with this name, "*" specifies all child elements, and "@*" specifies all attributes.

The following XSL functions can also be used with the "select" attribute:

<xsl:for-each select="node()"/>
 XSL statements
</xsl:for-each>
<xsl:for-each select="text()"/>
 XSL statements
</xsl:for-each>
<xsl:for-each select="comment()"/>
 XSL statements
</xsl:for-each>

where "node()" matches any child nodes, including element nodes, text nodes, and comment nodes, "text()" matches any child text nodes, and "comment()" matches any child comment nodes.

Once we have the group defined for the loop, we need to know how to access certain information about this loop, like the total number of iterations. We also need to know who is the current element or attribute at the current iteration. Here are some XSL expressions and functions that are very useful within an XSL loop:

  • "count()": Returns an integer representing the total number of iterations.
  • "position()": Returns an integer representing the position of the current element or attribute.
  • ".": Representing the element or attribut of the current iteration.
  • "name()": Returns the name of the element or attribut of the current iteration.

Now we are ready to do more complex tests to see the power of XSL. Let's revise our dictionary.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- dictionary.xsl, version 2.0
     Copyright (c) 2002 by Dr. Herong Yang
-->
 <xsl:template match="dictionary">
  <pre>
d_dictionary
  <xsl:for-each select="word">
w__<xsl:value-of select="name()"/>
   <xsl:apply-templates select="."/>
  </xsl:for-each>
  </pre>
 </xsl:template>
 <xsl:template match="word">
  <xsl:for-each select="@*">
a___<xsl:value-of select="name(.)"/>=<xsl:value-of select="."/>
  </xsl:for-each>
  <xsl:for-each select="*">
e___<xsl:value-of select="name()"/>
   <xsl:apply-templates select="."/>
  </xsl:for-each>
 </xsl:template>
 <xsl:template match="name">
  <xsl:for-each select="text()">
t____<xsl:value-of select="."/>
  </xsl:for-each>
 </xsl:template>
 <xsl:template match="definition">
  <xsl:for-each select="text()">
t____<xsl:value-of select="."/>
  </xsl:for-each>
 </xsl:template>
 <xsl:template match="update">
  <xsl:for-each select="@*">
a____<xsl:value-of select="name(.)"/>=<xsl:value-of select="."/>
  </xsl:for-each>
 </xsl:template>

(Continued on next part...)

Part:   1  2   3 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes On XML Technologies - XSL - Getting Values Out Of Source Elements