Part 3 of Step Patterns: Predicates

Part 3 of Step Patterns: Predicates

Predicates, the third part of step patterns, contain XPath expressions. You can use the [] operator to enclose a predicate and test whether a certain condition is true.

For example, you can test

  • The value of an attribute in a given string.

  • The value of an element.

  • Whether an element encloses a particular child, attribute, or other element.

  • The position of a node in the node tree.

Youll work with XPath expressions in more detail in Chapter 7, but well get an introduction to them here because you can use them in pattern predicates.

XPath expressions are more involved than match patterns. If you run into trouble creating them, one thing thats good to know is that the Xalan package has a handy example program, ApplyXPath.java, that enables you to apply an XPath expression to a document and see what the results would be. For example, if I apply the XPath expression "PLANET/ NAME " to planets.xml, the following example shows what the result looks like, displaying the values of all <NAME> elements that are children of <PLANET> elements (the opening and closing <output> tag is added by ApplyXPath):

 C:\>java ApplyXPath planets.xml PLANET/NAME  <output>  <NAME>Mercury</NAME>  <NAME>Venus</NAME>  <NAME>Earth</NAME>  </output> 

If the value of a predicate is numeric, it represents a position test . For example, NAME[1] matches the first <NAME> child of the context node. W3C position tests, and position tests in Xalan, Oracle, XT, Saxon, and MSXML3 (the Microsoft XML processor invoked using JavaScript, which you saw in Chapter 1 and will see more on in Chapter 10, Using XSLT Processor APIs) are 1-based, so the first child is child 1. Position tests in XML documents that use XSL stylesheets, and are loaded into the current version of Internet Explorer (version 5.5 and in newly released version 6.0), are 0-based (and you can use only a very restricted form of XPath expressions in predicates) and so, in consequence, is much of the XSL documentation on the Microsoft site. Otherwise, the value of a predicate must be true or false, called a Boolean test . For example, the predicate [@UNITS = "million miles"] matches elements that have UNITS attributes with the value "million miles" .

Predicates are full XPath expressions, although predicates used in patterns have two restrictions:

  • When a pattern is used in a match attribute, the predicate must not contain any reference to XSL variables (which youll see in Chapter 9). This restriction does not apply to predicates used in <xsl:number> elements.

  • Patterns may not use the XPath current function in predicates. This function returns the current node, and its use is restricted so processing is implementation-independent and does not depend on the current processing state.

The pattern in the following example matches <PLANET> elements that have child <NAME> elements:

 <xsl:template match = "PLANET[NAME]">      .      .      .  </xsl:template> 

This pattern matches any element that has a <NAME> child element:

 <xsl:template match = "*[NAME]">      .      .      .  </xsl:template> 

Now Ive given the <PLANET> elements in planets.xml a new attribute, COLOR , which holds the planets color:

 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="planets.xsl"?>  <PLANETS>    <PLANET COLOR="RED">      <NAME>Mercury</NAME>      <MASS UNITS="(Earth = 1)">.0553</MASS>      <DAY UNITS="days">58.65</DAY>      <RADIUS UNITS="miles">1516</RADIUS>      <DENSITY UNITS="(Earth = 1)">.983</DENSITY>      <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->    </PLANET>    <PLANET COLOR="WHITE">      <NAME>Venus</NAME>      <MASS UNITS="(Earth = 1)">.815</MASS>      <DAY UNITS="days">116.75</DAY>      <RADIUS UNITS="miles">3716</RADIUS>      <DENSITY UNITS="(Earth = 1)">.943</DENSITY>      <DISTANCE UNITS="million miles">66.8</DISTANCE><!--At perihelion-->    </PLANET>    <PLANET COLOR="BLUE">      <NAME>Earth</NAME>      <MASS UNITS="(Earth = 1)">1</MASS>      <DAY UNITS="days">1</DAY>      <RADIUS UNITS="miles">2107</RADIUS>      <DENSITY UNITS="(Earth = 1)">1</DENSITY>      <DISTANCE UNITS="million miles">128.4</DISTANCE><!--At perihelion-->    </PLANET>  </PLANETS> 

The following expression matches <PLANET> elements that have COLOR attributes:

 <xsl:template match="PLANET[@COLOR]">      .      .      .  </xsl:template> 

What if you wanted to match planets whose COLOR attribute was "BLUE" ? You can do that with the = operator as shown in Listing 4.5.

Listing 4.5 Using the = Operator
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="PLANETS">          <HTML>              <xsl:apply-templates/>          </HTML>      </xsl:template>      <xsl:template match="PLANET[@COLOR = 'BLUE']">              The <xsl:value-of select="NAME"/> is blue.      </xsl:template>      <xsl:template match="text()">      </xsl:template>  </xsl:stylesheet> 

The stylesheet shown in Listing 4.5 filters out all planets whose color is blue and omits the others by turning off the default rule for text nodes. Heres the result:

 <HTML>     The Earth is blue.  </HTML> 


Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net