Testing Expressions with Logical Operators

 
xslt for dummies
Chapter 7 - Adding Programming Logic Isnt Just for Propheads
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

Arguably the most important part of any xsl:if instruction is its test attribute. Its expression must be true in order for the xsl:if content to be processed . In addition to looking for a specific element or attribute value as I did in the preceding example, you can do a variety of tests inside the expression.

To test if an attribute exists, you use:

 <xsl:if test="@favorite"> 

Alternatively, you can use the XPaths built-in function not() to test for the opposite value:

 <xsl:if test="not(@favorite)"> 

XPath also allows you to use or and and operators to enable you to test more than one condition. If you want just one of two or more expressions to evaluate to true, then use or . But if you want all expressions to evaluate to true, then use and . For example, the following test expression is looking for an element with a name attribute that has a value of Language Arts , Reading , or Writing :

 <xsl:if test="(@name='Language Arts') or (@name='Reading') or (@name='Writing')"> 

To show the use of the and operator, the test expression that follows is looking for an element that has a days attribute of 5 and has a parent with a name attribute that equals Justus :

 <xsl:if test="(@days='5') and (../@name='Justus')"> 

To evaluate numeric expressions, you can use the traditional forms of comparison that you learned back in 3rd grade math: < , <= , > , >= , and =. However, in XML, you cant use the < character in your XSLT stylesheet, because XML reserves this character for marking the start of an element tag. If you need to use the less than sign, use &lt; . For example, rather than using 6 < 9 , you use whats in the following expression:

 <xsl:if test="6 &lt; 9"> 

Additionally, in place of using <= to mean less than or equal to, you use 6 &lt;= 9 .

The complete list of comparison operators is shown in Table 7-1.

Table 7-1: XPath Comparison Operators

Operator

Means

>

Greater than

>=

Greater than or equal to

&lt;

Less than

&lt;=

Less than or equal to

=

Equals

!=

Not equal

I have been using the equals sign in previous examples, but I can use its oppositethe != operatorto test for results that arent equal. So, if I want to search on elements with days attributes that arent equal to 5 , then I use:

 <xsl:if test="@days != 5"> 

The following stylesheet uses each of these xsl:if instructions:

 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="class"> <!-- if favorite attribute exists --> <xsl:if test="@favorite"> <xsl:value-of select="@name"/> is the favorite class of <xsl:value-of select="../@name"/><xsl:text> </xsl:text> </xsl:if> <!-- if favorite attribute does not exist --> <xsl:if test="not(@favorite)"> <xsl:value-of select="@name"/> is not the favorite class of <xsl:value-of select="../@name"/><xsl:text> </xsl:text> </xsl:if> <!-- if name is language arts, reading, or writing --> <xsl:if test="(@name='Language Arts') or (@name='Reading') or (@name='Writing')"> <xsl:text>English class topic: </xsl:text><xsl:value- of select="."/><xsl:text> </xsl:text> </xsl:if> <!-- if favorite attribute exists --> <xsl:if test="(@days='5') and (../@name='Justus')"> <xsl:text>One of Justus' 5-day/week classes: </xsl:text><xsl:value-of select="@name"/><xsl:text> </xsl:text> </xsl:if> <!-- if days less than 3 --> <xsl:if test="@days &lt; 3"> <xsl:text>Minor subject: </xsl:text><xsl:value-of select="@name"/><xsl:text> </xsl:text> </xsl:if> <!-- if days greater than or equal to 3 --> <xsl:if test="@days >= 3"> <xsl:text>Major subject: </xsl:text><xsl:value-of select="@name"/><xsl:text> </xsl:text> </xsl:if> <!-- if days does not equal 5 --> <xsl:if test="@days != 5"> <xsl:text>Not a full-time subject: </xsl:text><xsl:value-of select="@name"/><xsl:text> </xsl:text> </xsl:if> </xsl:template> </xsl:stylesheet> 

When applied to the XML source shown in Listing 7-1, the following output is then generated:

 Language Arts is not the favorite class of Jordan English class topic: Sentence diagramming Major subject: Language Arts Reading is the favorite class of Jordan English class topic: Lord Of The Rings Major subject: Reading Writing is not the favorite class of Jordan English class topic: Colonial Times Major subject: Writing Not a full-time subject: Writing Geography is not the favorite class of Jordan Minor subject: Geography Not a full-time subject: Geography Math is not the favorite class of Jordan Major subject: Math Science is not the favorite class of Jordan Major subject: Science Not a full-time subject: Science History is not the favorite class of Jordan Major subject: History Not a full-time subject: History Art is not the favorite class of Jordan Minor subject: Art Not a full-time subject: Art Language Arts is not the favorite class of Jared English class topic: Punctuation Major subject: Language Arts Reading is not the favorite class of Jared English class topic: Voyage Of The Dawntreader Major subject: Reading Writing is not the favorite class of Jared English class topic: Haiku Poetry Major subject: Writing Not a full-time subject: Writing Geography is the favorite class of Jared Minor subject: Geography Not a full-time subject: Geography Math is not the favorite class of Jared Major subject: Math Science is not the favorite class of Jared Major subject: Science Not a full-time subject: Science History is not the favorite class of Jared Major subject: History Not a full-time subject: History Art is not the favorite class of Jared Minor subject: Art Not a full-time subject: Art Language Arts is not the favorite class of Justus English class topic: Capitalization One of Justus' 5-day/week classes: Language Arts Major subject: Language Arts Reading is not the favorite class of Justus English class topic: Sherlock Holmes Solves Them All One of Justus' 5-day/week classes: Reading Major subject: Reading Writing is not the favorite class of Justus English class topic: Penmanship Major subject: Writing Not a full-time subject: Writing Geography is not the favorite class of Justus Minor subject: Geography Not a full-time subject: Geography Math is the favorite class of Justus One of Justus' 5-day/week classes: Math Major subject: Math Science is not the favorite class of Justus Major subject: Science Not a full-time subject: Science History is not the favorite class of Justus Major subject: History Not a full-time subject: History Art is not the favorite class of Justus Minor subject: Art Not a full-time subject: Art 
  
 
 
2000-2002    Feedback


XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

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