[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
How_To_XML_Display_XML_Attributes_XSLT

How to display XML attributes in HTML using XSLT?

The example XML document has one root element <books> and a number of child elements <book>. Each child element has two attributes ISBN and author.

Attributes are referred to the same way as elements but the attribute's name is preceeded by the @ sign.

Create the XML document and link it to the XSL stylesheet.

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="books06.xsl"?>
<books>
   <book ISBN="0321173481" author="Michael R. Sweet" />
   <book ISBN="0849371643" author="Gerald Farin" />
   <book ISBN="1558606696" author="David Rogers" />
   <book ISBN="1568810849" author="Gerald Farin" />
</books>


Create the XSL stylesheet.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head><title>Books</title>
</head>
<body>
<table width="100%" border="1">
  <THEAD>
	<TR>
	   <TD width="50%"><B>ISBN</B></TD>
 	   <TD width="50%"><B>Author</B></TD>
	</TR>
  </THEAD> 
  <TBODY>
	<xsl:for-each select="books/book">
	<TR>	
	   <TD width="50%"><xsl:value-of select="@ISBN" /></TD>  	
	   <TD width="50%"><xsl:value-of select="@author" /></TD>
	</TR>
	</xsl:for-each>
  </TBODY>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


<xsl:template match="/">


This looks for the root element <books> in this case, and start the tranformation from there.

<xsl:for-each select="books/book">


This creates a row for each <book> element found inside <books>.

<xsl:value-of select="@ISBN" /> <xsl:value-of select="@author" />
This takes the ISBN and author attributes' values from the XML and display them as the contents of the table cells.

XML FAQ

last edited (April 19, 2004) by lillu, Number of views: 2189, Current Rev: 6 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.