December 18, 2014
Hot Topics:

Using XML Attribute Values

  • January 2, 2001
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

  • Preface
  • Introduction
  • A Sample Program
  • The XML File
  • The XSLT File
  • The Output
  • Complete Program Listings

  • Preface

    I have authored numerous online articles on XML.  These articles cover the waterfront from introductory topics to advanced ones. A consolidated index of hyperlinks to all of my XML articles is available at my personal Web site. 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 Web site) or XSL. This article is part of a series that discusses 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 also showed you how to insert attributes into the output stream that were based on data values in the XML file. In this article, I will show you how to use attribute values in the XML file to insert attributes in the output stream.

    A Sample Program

    As in an earlier lesson, I will develop the XML code representing a small database of information on books.  As before, the database will contain Title, Author, and Price information for each book. In this case, however, the XML database will also contain an attribute for each book describing the type of book (programming, gardening, etc.). As before, I will develop the XSLT code required to process that database. 

    When you load the XML file into IE5, the XML will be converted into an HTML table and displayed in the browser window. The HTML table will have two columns:

    • Title
    • Author

    If (using IE5) you point to one of the cells in the Title column with your mouse, a tool tip appears showing the type of the book.  This information is based on the attribute for the book in the XML file. If you point to one of the cells in the Author column, a tool tip appears showing the price of the book.  This information is based on one of the data values in the XML file.  (Information on data values and attribute values can be found in my earlier articles on XML.) 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.

     I will discuss the code in fragments.  Complete listings of both the XML file and the XSLT file are presented near the end of the lesson.  A listing of the HTML actually produced by applying the transformation to the XML file is also shown near the end of the lesson.

    The XML File

    The XML file is named XSL005.xml. This file is so simple, and so similar to the file used in a previous article that I am going to show only one small fragment in this section. For brevity, I removed some of the data. You will see an ellipsis (...) where material has been deleted. You can view the entire file near the end of the lesson.

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="XSL005.xsl"?>

    <top ...

      <theData type="Programming">
        <title>Java</title>
        <author>R.Baldwin</author>
        <price>$9.95</price>
      </theData>

      <theData type="Pets">
      ...
      </theData>
    ...
    </top>

     This 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. More importantly, for this lesson, each element describing a book contains an attribute describing the type of the book.  I highlighted those attributes to make them easy to spot. Now let's take a look at the XSLT file.

    The XSLT File

    This file is named XSL005.xsl. This sample program illustrates:
    • How to use XSLT to transform the contents of a simple XML database into an HTML table.
    • How to provide a tool tip that displays the type of the book for each cell in the Title column.
    • How to provide another tool tip that displays the price of the book for each cell in the Author column .

    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 and Author. I discussed code similar to this in an earlier lesson and won't discuss it further here.

    The following contains the code that is the focus of this lesson, with the important new code highlighted in boldface:


    <xsl:for-each select="top/theData">

    <tr>
    <!-- 
    Create new table row with title attribute
    containing the type of the book.
    -->
    <xsl:attribute name="title">The type is 
    <xsl:value-of select="@type"/></xsl:attribute>

    To begin with, this fragment uses the <xsl:for-each select="top/theData">processing element to create a loop that will process each element named theData that is contained in the element named top.  This isn't new.  You have seen this in a previous lesson. During each iteration of the loop, this code uses the <xsl:attribute name="title"> processing element to create an attribute named title which is an attribute of the <tr> HTML element being inserted into the output stream. You have also seen this in a previous lesson. The value of the title attribute for each <tr> element is specified by using the <xsl:value-of select="@type"/> processing element to extract the value of the type attribute from the XML element describing the book.  This is new to this series of lessons.

    What Microsoft says about this topic:

    XSL provides mechanisms for accessing attributes in the source document, and for generating attributes in the result tree... Attributes in the source document can be accessed in XSL Patterns by preceding the attribute name with the @ symbol.

    Create and populate the table cells

    The following shows code that is new to this lesson, at least in terms of its placement in the template:

      <td><xsl:value-of select="title"/></td>

      <!-- 
      Create new table cell with title attribute
      containing the price of the book.
      -->
      <td>
        <xsl:attribute name="title">The price is 
        <xsl:value-of select="price"/></xsl:attribute>
        <xsl:value-of select="author"/>
      </td>

    I have highlighted the new code in boldface. The boldface code shown uses the <xsl:attribute name="title"> processing element to create an attribute named title for each cell in the HTML table containing data from the author element of the XML file. The value of the attribute includes the value of the price XML element for that particular book. This attribute provided for the cell overrides the attribute provided earlier for the row, so that when you point to an author cell, the price is displayed as a tool tip (in IE5). Note that I could also have placed the attribute for the type of the book on the cells containing the title of the book.

    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 the titles of certain books and the authors of those books. The <tr> element for each row of the table will have an attribute named title.  The value of that attribute will contain some text plus the type of the book corresponding to that row.  It is visible as a tool tip when you point to any cell in the Title column of the table. In addition, each <td> element for the cells in the Author column has a tool tip that provides the price for the book. 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.  (Note that I made no attempt to make it pretty.  I just stuffed it into the box.) 

    If you load this XML file into your IE5 browser, you should see something very similar to the following in the browser window. When you point to any cell under Title, you should see the type of the book displayed in a tool tip.  When you point to any cell under Author, you should see the price of the book displayed.
     

    Title Author
    Java R.Baldwin
    Man's Best Friend R.U.Barking
    ASP M.Soft
    Growing Ivy R.U.Blooming
    Python R.Baldwin
    Famous Wheels R.U.Driving
    C++ B.Pointer
    XML R.Baldwin

    If you view this lesson using IE5, tool tips should work here also, but they don't seem to work in Netscape 4.7. 

    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:attribute processing element to create attributes in the output stream. The values of the attributes in the output stream are based on the values of data elements in the XML file and also on the values of attributes in the XML file.

    Complete Program Listings

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

    <?xml version="1.0"?>

    <!-- File XSL005.xml
    Copyright 2000 R. G. Baldwin Illustrates creation of attributes in output
    based on attributes in the input.  Each data element contains a type attribute that
    is picked up and used to populate a title attribute in the output. Works with IE5.0
    -->

    <?xml-stylesheet type="text/xsl" href="XSL005.xsl"?>
    <top>

    <theData type="Programming">
    <title>Java</title>
    <author>R.Baldwin</author>
    <price>$9.95</price>
    </theData>

    <theData type="Pets">
    <title>Man's Best Friend</title>
    <author>R.U.Barking</author>
    <price>$19.95</price>
    </theData>

    <theData type="Programming">
    <title>ASP</title>
    <author>M.Soft</author>
    <price>$29.32</price>
    </theData>

    <theData type="Gardening">
    <title>Growing Ivy</title>
    <author>R.U.Blooming</author>
    <price>$29.95</price>
    </theData>

    <theData type="Programming">
    <title>Python</title>
    <author>R.Baldwin</author>
    <price>$15.42</price>
    </theData>

    <theData type="Cars">
    <title>Famous Wheels</title>
    <author>R.U.Driving</author>
    <price>$39.95</price>
    </theData>

    <theData type="Programming">
    <title>C++</title>
    <author>B.Pointer</author>
    <price>$32.69</price>
    </theData>

    <theData type="Programming">
    <title>XML</title>
    <author>R.Baldwin</author>
    <price>$19.60</price>
    </theData>

    </top>

    The following is a complete listing of the XSLT file (XSL005.xsl):

    <?xml version='1.0'?>
    <!-- File XSL005.xsl
    Copyright 2000 R. G. Baldwin Illustrates creation of attributes in output
    based on attributes in the input. 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>
    </tr>

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

    <tr>
    <!-- 
    Create new table row with title attribute
    containing the type of the book.
    -->
    <xsl:attribute name="title">The type is 
    <xsl:value-of select="@type"/></xsl:attribute>

      <!-- Create and populate table cells -->
      <td><xsl:value-of select="title"/></td>

      <!-- 
      Create new table cell with title attribute
      containing the price of the book.
      -->
      <td>
        <xsl:attribute name="title">The price is 
        <xsl:value-of select="price"/></xsl:attribute>
        <xsl:value-of select="author"/>
      </td>
    </tr>

    </xsl:for-each>

    </table>

    </BODY>
    </HTML>

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

    The following is a complete listing of the HTML produced by applying the XSLT transformation to the XML file:

    <HTML>
    <BODY>
    <table BORDER="2" CELLSPACING="5" CELLPADDING="5" WIDTH="330" 
    BGCOLOR="#FFFF00">
    <tr><td><b>Title</b></td>
    <td><b>Author</b></td></tr><tr title="The type is 
    Programming"><td>Java</td><td title="The price is 
        $9.95">R.Baldwin</td></tr><tr title="The type is
    Pets"><td>Man's Best Friend</td><td title="The price is 
        $19.95">R.U.Barking</td></tr><tr title="The type is 
    Programming"><td>ASP</td><td title="The price is 
        $29.32">M.Soft</td></tr><tr title="The type is 
    Gardening"><td>Growing Ivy</td><td title="The price is 
        $29.95">R.U.Blooming</td></tr><tr title="The type is 
    Programming"><td>Python</td><td title="The price is 
        $15.42">R.Baldwin</td></tr><tr title="The type is 
    Cars"><td>Famous Wheels</td><td title="The price is 
        $39.95">R.U.Driving</td></tr><tr title="The type is 
    Programming"><td>C++</td><td title="The price is 
        $32.69">B.Pointer</td></tr><tr title="The type is 
    Programming"><td>XML</td><td title="The price is 
        $19.60">R.Baldwin</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 who specializes in Java and XML. He believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web, adding to the many platform-independent benefits that Java applications already provide.

    Richard has participated in numerous consulting projects involving Java and XML.  He frequently provides onsite training in Java and XML at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which have gained a worldwide following among both 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.






    Comment and Contribute

     


    (Maximum characters: 1200). You have characters left.

     

     


    Enterprise Development Update

    Don't miss an article. Subscribe to our newsletter below.

    Sitemap | Contact Us

    Rocket Fuel