954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

XSLT : retrieving value of a tag based on info from another tag.

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 . Some books don't have desc but have references to other tags. The attribute 'id' of the tag refers to the book's id i have to refer and retrieve the 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.

newprogrammer14
Newbie Poster
5 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

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>
iceandrews
Junior Poster
185 posts since May 2010
Reputation Points: 10
Solved Threads: 30
 

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

newprogrammer14
Newbie Poster
5 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

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

newprogrammer14
Newbie Poster
5 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
newprogrammer14
Newbie Poster
5 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
Is there any way we can display the text inside the 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.

newprogrammer14
Newbie Poster
5 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Welcome!

iceandrews
Junior Poster
185 posts since May 2010
Reputation Points: 10
Solved Threads: 30
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You