www.developer.com/xml/article.php/640731

Back to Article

Learning XML Transformations (XSLT) using IE5 : Inserting Attributes Into the Output Stream
By Richard G. Baldwin
December 18, 2000

Let's get down to cases

The following shows another look at the xsl:attribute processing element used in this lesson.

        <xsl:attribute name="title">The price is 
        <xsl:value-of select="price"/></xsl:attribute>

What is the output attribute name? On the strength of the above description from Microsoft, we can see that the name of the attribute in the output HTML code will be title (the value of the name attribute in this processing element). What is the value of the output attribute? The value of the output attribute is the same as the content of this processing element.  Therefore, the value of the output attribute will be the concatenation of

  • The text "The price is" and
  • The contents of the price element  belonging to the XML data element being processed.

The remaining code in the XSLT file 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 literal text plus the price of the book corresponding to that row.  An attribute with the name title (in IE5) produces a type of behavior often referred to as a tool tip. When you point to the row with the mouse pointer and pause for a short period of time, a little message appears on the screen containing the text value of the attribute named title. For example, the text value of the attribute named title in this example is some literal text plus the price information that was extracted from the XML data.  This capability doesn't appear to be supported by Netscape 4.7.

If you need more information on the HTML title attribute, a good place to start looking would be at Joe Burns' HTML Goodies web site. 

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 in the browser window. When you point to any row (other than the top row) with your mouse pointer (and pause), you should see the price of the book displayed in a tool tip. If you view this lesson using IE5, tool tips should work here also, but they don't seem to work in Netscape 4.7.
 
Title Author
Java R.Baldwin
ASP M.Soft
Python R.Baldwin
C++ B.Pointer
XML R.Baldwin

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:attribute processing element to create HTML 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.

Complete Program Listings

The following is the complete listing for the XML file (XSL004.XML):

<?xml version="1.0"?>

<!-- File XSL004.xml
Copyright 2000 R. G. Baldwin
Illustrates creation of attributes in output
Works with IE5.0
-->

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

<theData>
<title>Java</title>
<author>R.Baldwin</author>
<price>$9.95</price>
</theData>

<theData>
<title>ASP</title>
<author>M.Soft</author>
<price>$29.32</price>
</theData>

<theData>
<title>Python</title>
<author>R.Baldwin</author>
<price>$15.42</price>
</theData>

<theData>
<title>C++</title>
<author>B.Pointer</author>
<price>$32.69</price>
</theData>

<theData>
<title>XML</title>
<author>R.Baldwin</author>
<price>$19.60</price>
</theData>

</top>

The following is the complete listing for the XSLT file (XSL004.XSL):

<?xml version='1.0'?>
<!-- File XSL004.xsl
Copyright 2000 R. G. Baldwin
Illustrates creation of attributes in output
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">

<!-- 
Create new table row with title attribute
containing the price of the book.
-->

<tr>
<xsl:attribute name="title">The price is 
<xsl:value-of select="price"/></xsl:attribute>
 

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

</table>

</BODY>
</HTML>

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

The following is the output HTML produced by applying this 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 price is 
$9.95"><td>Java</td><td>R.Baldwin</td></tr>
<tr title="The price is 
$29.32"><td>ASP</td><td>M.Soft</td></tr>
<tr title="The price is 
$15.42"><td>Python</td><td>R.Baldwin</td></tr>
<tr title="The price is 
$32.69"><td>C++</td><td>B.Pointer</td></tr>
<tr title="The price is 
$19.60"><td>XML</td><td>R.Baldwin</td></tr>
</table></BODY></HTML>


About the author

Richard Baldwin 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.

  Go to page: Prev  1  2  



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers