*/
Know a good article or link that we're missing? Submit it!

Other Views

corner
*/

How to Display XML Attributes in HTML Using XSLT

How to display XML attributes in HTML using XSLT?

XML attributes can be thought of as HTML element attributes, but in the case of XML, attributes and their values may be defined.

Create an XML document.

<?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 an 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>


Attributes are referred to in the same way as elements are, but the attribute's name is prefixed by a @ symbol.

<xsl:template match="/">


The example looks for the root element <books>. The tranformation starts from there.

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


A row is created for each <book> element found inside the root of <books>.

<xsl:value-of select="@ISBN" /> <xsl:value-of select="@author" />


The ISBN and author attributes' is taken from XML and displayed in a table.

Back to XML FAQ
corner
© 1996-2008 CommunityHeaven LLC. 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 our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.