XML Tutorials - Herong's Tutorial Examples - Version 5.05, by Dr. Herong Yang
Using XPath in XSLT Templates
This section describes how XPath can be used in XSLT templates. An XPath expression can be used in XSLT template to match or select parts of the source XML document, or to product a string output.
XPath expressions can be used in XSLT templates to produce a set of nodes of the source XML document can be matched or selected. For example:
<xsl:template match="LocationPathExpression"> <xsl:apply-templates select="LocationPathExpression"/> <xsl:for-each select="LocationPathExpression"/>
XPath expressions can also be used in XSLT "value-of" elements to product a string output. For example:
<xsl:value-of select="StringExpression"/>
Note that in this case, the resulting value of data types, including node set, will be converted to a string.
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) 2014, HerongYang.com, All Rights Reserved. --> <word acronym="true"> <name>XML</name> <definition reference="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) 2014, HerongYang.com, All Rights Reserved. --> <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____reference=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.
Last update: 2014.
Table of Contents
Introduction of XML (eXtensible Markup Language)
DOM (Document Object Model) Programming Interface
SAX (Simple API for XML) Programming Interface
DTD (Document Type Definition) Introduction
Validating an XML Document against the Specified DTD Document Type
XSD (XML Schema Definition) Introduction
Validating XML Documents Against Specified XML Schemas
XSL (Extensible Stylesheet Language) Introduction
XSLT (XSL Transformations) Introduction
Data Types, Literals and Variables
Expressions and Location Paths
►Using XPath in XSLT Templates
XSLT Elements as Programming Statements
Control and Generate XML Element in the Result
XML Tools Plugin for Notepad++