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

XML Path Language (XPath)

Part:   1  2  3  

(Continued from previous part...)

Using XPath in XSL Templates

XPath expressions are used in XSL templates anywhere if the resulting data type is allowed. For example:

<xsl:template match="LocationPathExpression">
<xsl:apply-templates select="LocationPathExpression"/>
<xsl:value-of select="StringExpression"/>
<xsl:for-each select="LocationPathExpression"/>

Let's review my sample XML file, dictionary_xsl.xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="dictionary.xsl"?>
<dictionary>
<!-- dictionary_xsl.xml
     Copyright (c) 2002 by Dr. Herong Yang
-->
 <word acronym="true">
  <name>XML</name>
  <definition referenece="Herong's Notes">eXtensible Markup 
Language.</definition>
  <update date="2002-12-23"/>
 </word>
 <word symbol="true">
  <name><</name>
  <definition>Mathematical symbol representing the "less than" logical 
operation, like: 1<2.</definition>
  <definition>Reserved symbol in XML representing the beginning of 
tags, like: <![CDATA[<p>Hello world!</p>]]>
  </definition>
 </word>
 <word symbol="false" acronym="false">
  <name>extensible</name>
  <definition>Capable of being extended.</definition>
 </word>
</dictionary>

And apply the following XSL file:

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

I got the following output:

d_dictionary
w__word
a___acronym=true
e___name
t____XML
e___definition
a____referenece=Herong's Notes
t____eXtensible Markup 
Language.
e___update
a____date=2002-12-23
w__word
a___symbol=true
e___name
t____<
e___definition
t____Mathematical symbol representing the "less than" logical 
operation, like: 1<2.
e___definition
t____Reserved symbol in XML representing the beginning of 
tags, like: 
t____<p>Hello world!</p>
w__word
a___symbol=false
a___acronym=false
e___name
t____extensible
e___definition
t____Capable of being extended.

Conclusion:

  • XPath technology is not part of XSL. But it is designed to help XSL.
  • XPath allows us to present sub structures of XML files with formal expressions.

Part:   1  2  3  

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes On XML Technologies - XML Path Language (XPath)