Hi guys

I'm new to XSLT programming and the question I ask may appear very easy to you guys. Please help me out.

This is the XML code :

<book id="12345">

<title>A<caps>New Voyage</caps>:Africa</title>

<desc>abcdefgh ijklmnop</desc>

</book>

<book id="12346">

<title>A<caps>New Voyage</caps></title>

<see id="12345">ANV</see>

</book>

I have to display the title and the description of each <book>. Some books don't have desc but have references to other tags. The attribute 'id' of the <see> tag refers to the book's id i have to refer and retrieve the <desc> value of that book and display it.

Please help me out. Is this possible using XSLT. The XML file cannot be changed.

Thanks in advance.

This is easily done in XSLT. This transformation will take your input document and produce it. I slightly modified the input document so I could verify it was working correctly. It assume 1 thing however. There will never exist a book node that has both a "desc" node and a "see" node. this is will happen you'll have to adjust the logic for it to work correctly.

Input Document

<books>
	<book id="12345">
		<title>A<caps>New Voyage 1</caps>:Africa</title>
		<desc>abcdefgh ijklmnop</desc>
	</book>
	<book id="12346">
		<title>A<caps>New Voyage 2</caps></title>
		<see id="12345">ANV</see>
	</book>
</books>

XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output indent="yes" method="html"/>

	<xsl:template match="/">
		<html>
			<body>
				<table>
					<xsl:apply-templates select="books"/>
				</table>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="books">		
		<xsl:apply-templates select="book"/>		
	</xsl:template>

	<xsl:template match="book">
		<tr>
			<xsl:apply-templates select="title"/>
			<xsl:apply-templates select="desc"/>
			<xsl:apply-templates select="see"/>
		</tr>
	</xsl:template>
	
	<xsl:template match="title" >
		<td>
			<xsl:value-of select="." />
		</td>
	</xsl:template>

	<xsl:template match="desc">
		<td>
			<xsl:value-of select="."/>
		</td>
	</xsl:template>

	<xsl:template match="see">
		<xsl:variable name="getid" select="@id"/>
		<td>
			<xsl:value-of select="//book[@id = $getid]/desc"/>
		</td>
	</xsl:template>
</xsl:stylesheet>

Output in HTML

<html>
  <body>
    <table>
      <tr>
        <td>ANew Voyage 1:Africa</td>
        <td>abcdefgh ijklmnop</td>
      </tr>
      <tr>
        <td>ANew Voyage 2</td>
        <td>abcdefgh ijklmnop</td>
      </tr>
    </table>
  </body>
</html>

wow. thats great. exactly wat i was looking for. thanks amillion for the help. guess i have a lot to learn

Is there any way we can display the text inside the <caps> tag in caps and all other text as it is ??? Thx .

Is there any way we can display the text inside the <caps> tag in caps and all other text as it is ??? Thx .

Its quite simple when we transform to HTML with CSS. The following code does it fine :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="html"/>

<xsl:template match="/">
<html>
<body>
<table>
<xsl:apply-templates select="books"/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="books">
<xsl:apply-templates select="book"/>
</xsl:template>

<xsl:template match="book">
<tr>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="desc"/>
<xsl:apply-templates select="see"/>
</tr>
</xsl:template>

<xsl:template match="title" >
<td>
<xsl:apply-templates/>
</td>
</xsl:template>

<xsl:template match="title/caps" >
<td>
<span style="font-variant: small-caps;">
<xsl:apply-templates/>
</span>
</td>
</xsl:template>


<xsl:template match="desc">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>

<xsl:template match="see">
<xsl:variable name="getid" select="@id"/>
<td>
<xsl:value-of select="//book[@id = $getid]/desc"/>
</td>
</xsl:template>
</xsl:stylesheet>

Thanks to iceandrews and everyone who has taken his/her time to look at this post.

Is there any way we can display the text inside the <caps> tag in caps and all other text as it is ??? Thx .

Its quite simple when we transform to HTML with CSS. The following code does it fine :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output indent="yes" method="html"/>

	<xsl:template match="/">
		<html>
			<body>
				<table>
					<xsl:apply-templates select="books"/>
				</table>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="books">		
		<xsl:apply-templates select="book"/>		
	</xsl:template>

	<xsl:template match="book">
		<tr>
			<xsl:apply-templates select="title"/>
			<xsl:apply-templates select="desc"/>
			<xsl:apply-templates select="see"/>
		</tr>
	</xsl:template>
	
	<xsl:template match="title" >
		<td>
			<xsl:apply-templates/>
		</td>
	</xsl:template>

	<xsl:template match="title/caps" >
		<td>
                        <span style="font-variant: small-caps;">
			<xsl:apply-templates/>
                        </span>
		</td>
	</xsl:template>


	<xsl:template match="desc">
		<td>
			<xsl:apply-templates/>
		</td>
	</xsl:template>

	<xsl:template match="see">
		<xsl:variable name="getid" select="@id"/>
		<td>
			<xsl:value-of select="//book[@id = $getid]/desc"/>
		</td>
	</xsl:template>
</xsl:stylesheet>

Thanks to iceandrews and everyone who has taken his/her time to look at this post.

This question has already been answered. Start a new discussion instead.