Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read same element in XML using XSLT

Status
Not open for further replies.

RPredrag

ISP
Feb 18, 2002
14
US
Hello!

I am pretty new to this and strugling with basic stuff.
Here is my problme.
I have this XML document

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<!DOCTYPE BOOKS[
<!ELEMENT BOOKS (BOOK+)>
<!ELEMENT BOOK (TITLE+,AUTHOR+,PUBLISHED+,DESCRIPTION*)>
<!ELEMENT AUTHOR (#PCDATA)>
<!ELEMENT PUBLISHED (#PCDATA)>
<!ELEMENT DESCRIPTION (#PCDATA)>
]>
<BOOKS>
<BOOK>
<TITLE>XML Step by Step Tutorial</TITLE>
<AUTHOR>Yu Yang</AUTHOR>
<AUTHOR>LI JANG</AUTHOR>
<PUBLISHED>2002</PUBLISHED>
<DESCRIPTION>Excelent XML tutorial for beginners.</DESCRIPTION>
</BOOK>
<BOOK>
<TITLE>SE Java</TITLE>
<AUTHOR>Mike Murach</AUTHOR>
<PUBLISHED>2006</PUBLISHED>
<DESCRIPTION>It is a beginner step by step instructional Java book.</DESCRIPTION>
</BOOK>
</BOOKS>

And this xsl

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="html"/>
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>BOOKS</TITLE>
</HEAD>
<BODY BGCOLOR="LightGray">
<p align="center"><font size="20" color="Teal">BOOKS</font></p>
<TABLE border="1" align="center" bgcolor="LightGray">
<TR bgcolor="Teal">
<TH align="center"><FONT SIZE="+2">BOOK</FONT></TH>
<TH align="center"><FONT SIZE="+2">AUTHOR</FONT></TH>
<TH align="center"><FONT SIZE="+2">PUBLISHED</FONT></TH>
<TH align="center"><FONT SIZE="+2">DESCRIPTION</FONT></TH>
</TR>
<xsl:for-each select="//BOOK">

<TR>
<TD align="left"><FONT COLOR="DarkSlateBlue" SIZE="+1"><xsl:value-of select="TITLE"/></FONT></TD>

<TD align="left"><FONT COLOR="DarkSlateBlue" SIZE="+1">
<xsl:value-of select="AUTHOR"/>
<!-- this does not work -->
<BR/>
<xsl:value-of select="AUTHOR"/>
-->
</FONT></TD>

<TD align="center"><FONT COLOR="DarkSlateBlue" SIZE="+1"><xsl:value-of select="PUBLISHED"/></FONT><BR/></TD>
<TD align="left"><FONT COLOR="DarkSlateBlue" SIZE="+1"><xsl:value-of select="DESCRIPTION"/></FONT><BR/></TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

This XSL read just first AUTHOR
I do not know how I can read second element AUTHOR from xml document using this XSL structure. Any help is appreciated.
Thanks a lot.
 
A quick fix with limited scope of applicability would be this which has at least the merit to show you the "position" notion of xpath.
[tt]
<!-- this does not work -->
<BR/>
<xsl:value-of select="AUTHOR[red][2][/red]"/>
[red]<!--[/red] -->[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top