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

How to display mixed XML content in HTML using  ?XSLT?

XML content is mixed when text and attributes are both present inside an element. The <title> element has mixed content in the example: two attributes: ISBN and author, and the element's text: OpenGL Programming Guide Fourth Edition.

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

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="mixed.xsl"?>
<books>
   <book>  
	<title ISBN="0321173481" author="Michael R. Sweet">
	OpenGL Programming Guide Fourth Edition</title>		
   </book>
   <book>
	<title ISBN="0849371643" author="Gerald Farin">
	Curves and Surfaces for CAGD: A Practical Guide</title>
   </book>
   <book>
	<title ISBN="1558606696" author="David Rogers">
	An Introduction to NURBS: With Historical Perspective</title>
   </book>
   <book>
	<title ISBN="1568810849" author="Gerald Farin">
	NURBS: From Projective Geometry to Practical Use</title>
   </book>
</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="10%"><B>ISBN</B></TD>
      <TD width="20%"><B>Author</B></TD>
      <TD width="40%"><B>Title</B></TD>
   </TR>
  </THEAD> 
  <TBODY>
   <xsl:for-each select="books/book">
   <TR>	
     <xsl:apply-templates select="title" />
      <TD width="40%"><xsl:value-of select="title" /></TD>   
   </TR>
   </xsl:for-each>
  </TBODY>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="title">
<TD width="10%"><xsl:value-of select="@ISBN" /></TD> 
<TD width="20%"><xsl:value-of select="@author" /></TD>
</xsl:template>
</xsl:stylesheet>


Output:


ISBN       Author           Title                           
0321173481 Michael R. Sweet OpenGL Programming Guide Fourth Edition
0849371643 Gerald Farin     Curves and Surfaces for CAGD: A Practical Guide1558606696 David Rogers     An Introduction to NURBS: With Historical Perspective 
1568810849 Gerald Farin     NURBS: From Projective Geometry to Practical Use 


Attributes are referred to the same way as elements with the difference that attribute names are preceeded by the @ sign.

<xsl:apply-templates select="title" />


This creates a new template to process further elements that start from the element specified.

<xsl:template match="title">


This matches all occurences of the specified element.

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


This outputs the text value of all <title> elements found.

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


This outputs the attribute's value.

XML FAQ

last edited (April 19, 2004) by lillu, Number of views: 2636, 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.