BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XSLT
Using XPath Predicates
VoiceXML with XSLT
XSLT Quickly
The Functional Programming Language XSLT - A proof through examples
The Understanding XSL Game
XSLT Stylesheets, find over 100 example XSLT stylesheets
XSLT - Creating Links and Cross-References
XML in a Nutshell - XPath
The XPath Function: node-set unparsed-entity-url ( string )
The XPath Function: boolean true ( )
The XPath Function: string translate ( string, string, string )
The XPath Function: object system-property ( string )
The XPath Function: number sum ( node-set )
The XPath Function: string substring ( string, number, number? )
The XPath Function: string substring-before ( string, string )
The XPath Function: string substring-after ( string, string )
The XPath Function: string string ( object? )
The XPath Function: number string-length ( string? )
The XPath Function: boolean starts-with ( string, string )
The XPath Function: number round ( number )
<< XQuery
.NET and XML >>

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :11/09/2004
Times viewed :797

 

Creating XPath Predicates

cover image This is an excerpt from Chapter 4 of the New Riders book called Inside XSLT written by Steven Holzner.

PLEASE NOTE:  To work with the XSLT and XPath in this document TopXML recommends you use the demo version of Xselerator XSL Editor

Predicates are true XPath expressions, and XPath is much more of a true language than patterns; for example, XPath expressions can return not only lists of nodes, but also Boolean, string, and numeric values. XPath expressions are not restricted to working with the current node or child nodes, because you can work with parent nodes, ancestor nodes, and more.

Chapter 7 gives its full attention to XPath, but it's worth getting an introduction to the subject during this discussion about patterns, because the predicate part of a pattern is its most powerful part. There are all kinds of expressions that you can work with in predicates; the following list includes some possible types, which are explored in the next sections:

  • Node-sets
  • Booleans
  • Numbers
  • Strings

Predicates: Node Sets

As its name implies, a node set is simply a set of nodes (and it may contain only a single node). The expression child::PLANET returns a node set of all <PLANET> elements. The expression child::PLANET/child::NAME returns a node list of all <NAME> elements that are children of <PLANET> elements. To select a node or nodes from a node set, you can use the following functions that work on node sets in predicates.

  • last(). Returns the number of nodes in a node set.
  • position(). Returns the position of the context node in the context node set (starting with 1).
  • count(node-set). Returns the number of nodes in a node set. Omitting node-set makes this function use the context node.
  • id(string ID). Returns a node set containing the element whose ID matches the string passed to the function, or an empty node set if no element has the specified ID. You can list multiple IDs separated by white space, and this function returns a node set of the elements with those IDs.
  • local-name(node-set). Returns the local name of the first node in the node set. Omitting node-set makes this function use the context node.
  • namespace-uri(node-set). Returns the URI of the namespace of the first node in the node set. Omitting node-set makes this function use the context node.
  • name(node-set). Returns the full, qualified name of the first node in the node set. Omitting node-set makes this function use the context node.

Here's an example; in this case, I number the elements in the output document using the position() function:

Listing 4.6 Using the position function

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="PLANETS">

        <HTML>

            <HEAD>

                <TITLE>

                    The Planets

                </TITLE>

            </HEAD>

            <BODY>

                <xsl:apply-templates select="PLANET"/>

            </BODY>

        </HTML>

    </xsl:template>

    <xsl:template match="PLANET">

        <P>

            <xsl:value-of select="position()"/>.

            <xsl:text> </xsl:text>

            <xsl:value-of select="NAME"/>

        </P>

    </xsl:template>

   </xsl:stylesheet>

Here's the result, where you can see that the planets are numbered:

<HTML>

    <HEAD>

        <TITLE>

            The Planets

        </TITLE>

    </HEAD>

    <BODY>

        <P>

            1. Mercury

        </P>

        <P>

            2. Venus

        </P>

        <P>

            3. Earth

        </P>

    </BODY>

</HTML>

You can also use functions that operate on node sets in predicates, as I do here: PLANET[position() = last()], which selects the last <PLANET> child of the context node.

Predicates: Booleans

You can also use Boolean values in XPath expressions. Numbers are considered false if they're zero, and true otherwise. An empty string, "", is also considered false, and all other strings are considered true.

You can use the following XPath logical operators to produce Boolean true/false results:

  • != means "is not equal to"
  • < means "is less than" (use &lt; in XML or XSL documents)
  • <= means "is less than or equal to" (use &lt;= in XML or XSL documents)
  • = means "is equal to" (C, C++, Java, JavaScript programmers take note: this operator is one = sign, not two).
  • ¦ means "is greater than"
  • >= means "is greater than or equal to"

Using the < character

Note in particular that you shouldn't use < directly in XML or XSL documents, so you should use the entity reference &lt; instead.

You can also use the keywords and and or to connect Boolean clauses with a logical And or Or operation, and you can use not to flip the logical sense of an expression, from true to false, or false to true.

In the following example, I identify Earth's <PLANET> element and place the strings "Earth", "needs", "no", and "introduction." in the table rather than Earth's numeric data. I identify which planet is Earth with the predicate "[NAME='Earth']", which checks the value of the <NAME> element, which in turn is its enclosed text. I also provide a template for the other planets, matching the predicate "[NAME!='Earth']":

Listing 4.7 Finding Planet Earth

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/PLANETS">

        <HTML>

            <HEAD>

                <TITLE>

                    The Planets Table

                </TITLE>

            </HEAD>

            <BODY>

                <H1>

                    The Planets Table

                </H1>

                <TABLE BORDER="2">

                    <TR>

                        <TD>Name</TD>

                        <TD>Mass</TD>

                        <TD>Radius</TD>

                        <TD>Day</TD>

                    </TR>

                    <xsl:apply-templates/>

                </TABLE>

            </BODY>

        </HTML>

    </xsl:template>

       <xsl:template match="PLANET[NAME='Earth']">

       <TR>

          <TD>Earth</TD>

          <TD>needs</TD>

          <TD>no</TD>

          <TD>introduction.</TD>

       </TR>

   </xsl:template>

       <xsl:template match="PLANET[NAME!='Earth']">

       <TR>

          <TD><xsl:value-of select="NAME"/></TD>

          <TD><xsl:apply-templates select="MASS"/></TD>

          <TD><xsl:apply-templates select="RADIUS"/></TD>

          <TD><xsl:apply-templates select="DAY"/></TD>

       </TR>

   </xsl:template>

       <xsl:template match="MASS">

        <xsl:value-of select="."/>

        <xsl:text> </xsl:text>

        <xsl:value-of select="@UNITS"/>

    </xsl:template>

    <xsl:template match="RADIUS">

        <xsl:value-of select="."/>

        <xsl:text> </xsl:text>

        <xsl:value-of select="@UNITS"/>

    </xsl:template>

       <xsl:template match="DAY">

        <xsl:value-of select="."/>

        <xsl:text> </xsl:text>

        <xsl:value-of select="@UNITS"/>

    </xsl:template>

   </xsl:stylesheet>

Here's the result:

<HTML>

    <HEAD>

        <TITLE>

            The Planets Table

        </TITLE>

    </HEAD>

    <BODY>

        <H1>

            The Planets Table

        </H1>

        <TABLE BORDER="2">

            <TR>

                <TD>Name</TD>

                <TD>Mass</TD>

                <TD>Radius</TD>

                <TD>Day</TD>

            </TR>

            .

            .

            .

            <TR>

                <TD>Earth</TD>

                <TD>needs</TD>

                <TD>no</TD>

                <TD>introduction.</TD>

            </TR>

        </TABLE>

    </BODY>

</HTML>


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C
Immediate ASP.NET/C# Openings for C
Sr. Software Engineer

View all Jobs (Add yours)
View all CV (Add yours)



halloween mask
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses
fax server


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP