DatabaseLearning XML: Using Conditional Tests

Learning XML: Using Conditional Tests

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.


Preface

I have authored numerous online articles on XML.  These articles cover the waterfront from introductory topics to advanced topics. A consolidated index of hyperlinks to all of my XML articles is available at my personal website. You can access earlier articles from there.

As of this writing, Microsoft IE5 is the only widely-used web browser that has the ability to render XML documents. IE5 can render XML documents using either CSS (see my personal website) or XSL. This is one in a series of articles that discuss the use of XSL for the rendering of XML documents, with particular emphasis on the use of IE5 for that purpose.

 

Introduction

In a previous article, I showed you how to use the xsl:for-each processing element to loop through an XML file, extracting data from that file.  I showed you how to insert that data into an output HTML table. In this article, I am going to continue using the HTML table model.  I will show you how to perform a conditional test on each data value to decide whether or not to insert that piece of data into the output table. Along the way, I am going to show you how to cause an XML data value that would otherwise be treated as a string to be treated as a numeric value for testing purposes. In particular, I will cause the values of a set of data elements in the XML file to be treated as numeric values.  Then I will filter out all of those that fail to meet a specific test.  Only those that pass through the filter will be written into the table in the HTML output.

A Sample Program

I will develop the XML code representing a small database of information on books.  The database will contain Title, Author, and Price information for each book. I will also develop the XSLT code required to process that database. When you load the XML file into IE5, portions of the XML data will be converted into an HTML table and displayed in the browser window. The HTML table will have three columns:

  • Title
  • Author
  • Price

Only those books whose price is “greater than” 9.95 will be allowed to pass through the filter to be inserted into the HTML table. Alternately, you can load the XML file and the XSLT file into the XSL Debugger discussed in an earlier lesson and get essentially the same result. This example is based on XSLT information available at the W3C and on IE5 information available at Microsoft.  There are some other important Microsoft links that I will refer to later as well. I will discuss the code in fragments.  Complete listings of the XML file, the XSLT file, and the HTML output are presented near the end of the lesson.

The XML File

The XML file is named XSL006.xml. This file is so similar to the files used in previous articles. The following is one small fragment:

<top xmlns:dataType=
“urn:schemas-microsoft-com:datatypes”>

<theData>
  <title>Java</title>
  <author>R.Baldwin</author>
  <price dataType:dt=”number”>9.95</price>
</theData>

</top>

For brevity, I have deleted everything except one data element and the namespace information in the top-level element.  You can view the entire file near the end of the lesson. 

The  fragment shown illustrates how the data describing a book is stored in the XML file. Each book is described in an element named theData.  Each element describing a book contains three elements named title, author, and price. Each of the elements containing information about a book belongs to the top-level element named top. Each of the price elements is treated as a “number”. 

As in an earlier lesson, I am going to gloss over the namespace topic and touch on it only briefly here. We can give the namespace attribute any name that we please so long as we use it properly later. You should note that the Microsoft literature tends to use an attribute name of xmlns:dt.  For reasons that you will see in a minute, that can be confusing, so I elected to give it another name, xmlns:dataType

What Microsoft says about Namespaces and Datatypes

Data types are referenced from the data type namespace, “urn:schemas-microsoft-com:datatypes”. To use data types within an XML Schema, declare the data type namespace within the schema document.

The statement from Microsoft in the sidebar raises another question, “What is a schema?” Like namespaces, schema is also a topic that I plan to cover in a future article. For the time being, suffice it to say that in order to do the things that we will do here with data types, we need to set the xmlns:dataType attribute to “urn:schemas-microsoft-com:datatypes”

The next important feature of the XML file is the code that establishes the price data as a “number”. Following is the specific line of XML code that accomplishes this (repeated from above for emphasis):

 <price dataType:dt=”number”>9.95</price>

In this case, what we see is an attribute of the price element, named dataType:dt, with a value of “number”.  Now you see why I elected to use another name for the namespace attribute.  I think that dt:dt is a little more confusing than dataType:dt.

Although the name of the namespace attribute can be whatever you choose, the name of the price attribute (dt) seems to be fixed.  (More correctly, the name of the attribute that specifies the type is fixed by IE5). I was unable to find any documented support for this conclusion, but it is easy to show that if the name is changed, the ability of IE5 to properly transform the XML data fails. Apparently, the IE5 XML processor expects this attribute name to be fixed as dt.

What Microsoft says about the type "number"

Number, with no limit on digits; can potentially have a leading sign, fractional digits, and optionally an exponent. Punctuation as in U.S. English. (Values have same range as most significant number, R8, 1.7976931348623157E+308 to 2.2250738585072014E-308.)

You can see from the sidebar on what Microsoft has to say about "number" that you have a pretty wide range to use. The range of values should be sufficient to handle the price of a book (unless we have a bad inflation problem). Now let’s  take a look at the XSLT file.

The XSLT File

The XSLT file for this example is named XSL006.xsl. This file shows how to use XSLT to transform a simple database of book information into an HTML table, filtering out all books except those whose price is greater than 9.95. I will discuss this XSLT file in fragments.  A complete listing of the file is presented near the end of the lesson. 

I am going to skip over all of the preliminary material, and also skip over the code that creates the table and creates the first row in the table containing the literal text Title, Author, and Price. I discussed code similar to that in an earlier lesson and won’t discuss it further here. The following listing contains the code that is the focus of this lesson, with the important new code highlighted in boldface and Italics.

<!– Do a for-each loop –>
<xsl:for-each select=”top/theData”>
    <!– Perform conditional test
    Note the period in the syntax –>
    <xsl:if test=”price[.>9.95]”>
        <!– Create new table row –>
        <tr>
            <!– Create and populate table cells –>
            <td><xsl:value-of select=”title”/></td>
            <td><xsl:value-of select=”author”/></td>
            <td><xsl:value-of select=”price”/></td>
        </tr>
    </xsl:if>
</xsl:for-each>

 The important thing to note here is that the code that creates the rows in the output HTML table is wrapped in the conditional test element shown in the following code snippet:

   <xsl:if test=”price[.>9.95]”>
        …
    </xsl:if>

This is one of two conditional tests that are available in XSLT.  The other provides for multiple choices.  That test construct will be the topic of a future lesson.

What the W3C has to say about xsl:if

The xsl:if element has a test attribute, which specifies an expression.

The content is a template. The expression is evaluated and the resulting object is converted to a boolean as if by a call to the boolean function.

If the result is true, then the content template is instantiated; otherwise, nothing is created.


The W3C’s I believe that this can be paraphrased to say that if the expression (“price[.>9.95]”) is true then the result of processing the code contained within the element is sent to the output.  Otherwise, nothing is sent to the output. Although the syntax may be a little strange, this expression is asking if the value of the price element is greater than 9.95.  Of course, the right angle bracket (>) is a “greater than” test.

The syntax that requires the square brackets and the period deserves some explanation.  This falls in a realm that Microsoft refers to as filtering. For more on this, see the sidebar on what Microsoft has to say about filtering.

What Microsoft has to say about Filters

Filters are always evaluated with respect to a context. That is, the expression “book[author]” means for every book element that is found, test whether it has an author subelement.
Likewise, “book[author = ‘Bob’]” means for every book element that is found, test whether it has an “author” child element with the value “Bob”.
One can examine the value of the context as well, by using the period (.) character.
For example, “book[. = ‘Trenton’]” means that for every book that is found in the current context, test whether its value is “Trenton”.


On the strength of the explanation provided in the sidebar, we can say that the following expression tests to determine if the value of the price element in the current book element is greater than 9.95.

 <xsl:if test=”price[.>9.95]”>

If this express is true, the result of processing the code that makes up the content of the element is passed to the output. If the expression is not true (false), the code that makes up the content of the element is simply ignored. This has the effect of inserting the data for only those data elements whose price value is greater than 9.95 into a row in the output HTML table. I will skip the remaining code in the XSLT file because it consists solely of end tags that I have discussed in earlier lessons.

In summary, this XSLT file, when used to transform the given XML file, will produce an HTML table containing information about those books in the XML database whose price is greater than 9.95. Now let’s look at some output.

The Output

A listing of the output HTML produced by this program is shown near the end of the lesson.  I captured this HTML code using the Microsoft XSL Debugger discussed in an earlier lesson. If you load this XML file into your IE5 browser, you should see something very similar to the following table in the browser window.
 

Title Author Price
Python R.Baldwin 15.42
XML R.Baldwin 19.60

 Browser Output

As you can see, the book named Java described by the XML element shown in the following, did not make its way into the output HTML table. This is because its price was exactly 9.95, which is not greater than 9.95.  Therefore, it was filtered out.

<theData>
<title>Java</title>
<author>R.Baldwin</author>
<price dataType_dt=”number”>9.95</price>
</theData>

The other two books in the XML database had price values greater than 9.95, and therefore met the filter criteria.  They were passed through to the output table.

Recap

So there you have it.  The use of an XSLT file to transform the contents of a simple XML database into an HTML table, and to use the xsl:if conditional processing element to determine which data are passed through to the output, and which data are filtered out. Although it isn’t illustrated here, the conditional test can also be performed on attribute values associated with the XML data elements. For example, if the books had a type attribute, books with a type attribute of dogs could be passed through while books with a type attribute of plants could be rejected.

Complete Program Listings

The following is a complete listing of the XML file (XSL006.xml):

 

<?xml version=”1.0″?>

<!– File XSL006.xml
Copyright 2000 R. G. Baldwin
Illustrates defining data as numeric data.  Also
illustrates using a “greater than” conditional
test.

Works with IE5.0
–>

<?xml-stylesheet 
  type=”text/xsl” 
  href=”XSL006.xsl”?>
<top xmlns_dataType=
“urn:schemas-microsoft-com:datatypes”>

<theData>
<title>Java</title>
<author>R.Baldwin</author>
<price dataType_dt=”number”>9.95</price>
</theData>

<theData>
<title>Python</title>
<author>R.Baldwin</author>
<price dataType_dt=”number”>15.42</price>
</theData>

<theData>
<title>XML</title>
<author>R.Baldwin</author>
<price dataType_dt=”number”>19.60</price>
</theData>

</top>

 The following is the XSLT file (XSL006.xsl):

<?xml version=’1.0′?>
<!– File XSL006.xml
Copyright 2000 R. G. Baldwin Illustrates defining data as numeric data.  Also
illustrates using a “greater than” conditional test. Works with IE5.0
–>
<xsl:stylesheet 
xmlns:xsl=”http://www.w3.org/TR/WD-xsl”>

<xsl:template match=”/”>

<HTML>
<BODY>

<table BORDER=”2″ 
CELLSPACING=”5″ 
CELLPADDING=”5″ 
WIDTH=”330″ 
BGCOLOR=”#FFFF00″>

<!– Create table titles –>
<tr>
  <td><b>Title</b></td>
  <td><b>Author</b></td>
  <td><b>Price</b></td>
</tr>

<!– Do a for-each loop –>
<xsl:for-each select=”top/theData”>

<!– Perform conditional test
Note the period in the syntax –>
<xsl:if test=”price[.>9.95]”>

<!– Create new table row –>
<tr>
<!– Create and populate table cells –>
  <td><xsl:value-of select=”title”/></td>
  <td><xsl:value-of select=”author”/></td>
  <td><xsl:value-of select=”price”/></td>
</tr>

</xsl:if>

</xsl:for-each>

</table>

</BODY>
</HTML>

</xsl:template>
</xsl:stylesheet>

The output HTML produced by applying the XSLT file to the XML file follows:

<HTML><BODY>
<table BORDER=”2″ CELLSPACING=”5″ 
CELLPADDING=”5″ WIDTH=”330″ 
BGCOLOR=”#FFFF00″>
<tr>
<td><b>Title</b></td>
<td><b>Author</b></td>
<td><b>Price</b></td>
</tr>
<tr>
<td>Python</td><td>R.Baldwin</td><td>15.42</td>
</tr>
<tr><td>XML</td><td>R.Baldwin</td><td>19.60</td>
</tr>
</table>
</BODY></HTML>


Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without  express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin (baldwin.richard@iname.com)is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin’s Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories