XML Development

Moderators: None (Apply to moderate this forum)
Number of threads: 257
Number of posts: 456

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
GETTING COUNT AND POSITION using XSLT 1.0 Posted by udaybob on 18 Aug 2009 at 6:53 AM
Hi Guys,

Here is my requirement.

sample XML

<root>
<item>
...
<data name="x">...</data>
<data name="title">uday</data>
<data name="y">...</data>
....
</item>
<item>
...
<data name="x">...</data>
<data name="title">uday</data>
<data name="y">...</data>
....
</item>
<item>
...
<data name="x">...</data>
<data name="title">bhaskar</data>
<data name="y">...</data>
....
</item>
<item>
...
<data name="x">...</data>
<data name="title">bhaskar</data>
<data name="y">...</data>
....
</item>

<item>
...
<data name="x">...</data>
<data name="title">bhaskar</data>
<data name="y">...</data>
....
</item>

Desired o/p XML

<rss>
<item>
..
<position>1</position>
<total>2</total>
....
</item>
<item>
..
<position>2</position>
<total>2</total>
....
</item>
<item>
..
<position>1</position>
<total>3</total>
....
</item>
<item>
..
<position>2</position>
<total>3</total>
....
</item>
<item>
..
<position>3</position>
<total>3</total>
....
</item>
</rss>

for each item in the input xml, we get item in the output.
total represents the no. of items with the same title value
position represents the position of the item with a paticular title.

Iam using xslt 1.0. I guess it is possible by using keys.Can someone help me out please

Thanks you in advance
Uday
Report
Re: GETTING COUNT AND POSITION using XSLT 1.0 Posted by xml_looser on 20 Aug 2009 at 7:56 AM

<?xml version="1.0"?>
<!-- Helmut Hagemann BEERWARE-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output indent="yes" method="xml"/>
	<xsl:key name="only" match="//data[@name='title']" use="."/>
	<xsl:template match="/">
		<rss>
			<xsl:apply-templates select="root"/>
		</rss>
	</xsl:template>
	<xsl:template match="root">
		<xsl:for-each select="item">
			<item>
				<position>
					<xsl:value-of select="position()"/>
				</position>
				<total>
					<xsl:value-of select="count(key('only',data))"/>
				</total>				
			</item>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>



Report
Re: GETTING COUNT AND POSITION using XSLT 1.0 Posted by udaybob on 20 Aug 2009 at 7:47 PM
Thanks you very much



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - 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.
Operated by CommunityHeaven, a BootstrapLabs company.