Here is an example of the XML document I am working with:
I need a way to group this document by class (I cant change the xml doc itself). SO I have teh following xsl doc:
As you can see, I want to display a category name and descritpion only when its the first instance of that category.
With this code I get the following:
Test1
This category will be useful
* News for nerds, stuff that matters
* BBCI Link
* fooDisplay
It doesnt display the 'foo category for items 2 and 3. I'm not sure why. Could anyone help me with this? Am i using position() correctly?
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root type='foo'>
<child>
<modifier>BBCI</modifier>
<url>[URL unfurl="true"]http://www.foo.com</url>[/URL]
<category>foo</category>
<categorydescription>The foobar category</categorydescription>
<linkdescription>This is the BBCI Link</linkdescription>
<displaytext>BBCI Link</displaytext>
<description>This is the BBCI Link</description>
<cascadelinks>1</cascadelinks>
</child>
<child>
<modifier>DMPORTLS</modifier>
<url>foo.com</url>
<category>foo</category>
<categorydescription>The foobar category</categorydescription>
<linkdescription>description</linkdescription>
<displaytext>fooDisplay</displaytext>
<description>description</description>
<cascadelinks>1</cascadelinks>
</child>
<child>
<modifier>DMPORTLS</modifier>
<url>[URL unfurl="true"]http://www.slashdot.org</url>[/URL]
<category>Test1</category>
<categorydescription>This category will be useful</categorydescription>
<linkdescription>All things tech...</linkdescription>
<displaytext>News for nerds, stuff that matters</displaytext>
<description>All things tech...</description>
<cascadelinks>1</cascadelinks>
</child>
</root>
I need a way to group this document by class (I cant change the xml doc itself). SO I have teh following xsl doc:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:template match="/">
<xsl:for-each select="/root/child">
<xsl:sort select="category" order="ascending"/>
<xsl:if test="/root/child[position()]/category != /root/child[position()-1]/category or position() = 1">
<h2>
<xsl:value-of select="category" />
</h2>
<xsl:value-of select="categorydescription" />
</xsl:if>
<ul>
<li>
<a>
<xsl:attribute name="href">
<xsl:value-of select="url" />
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="description" />
</xsl:attribute>
<xsl:value-of select="displaytext"/>
</a>
</li>
</ul>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
As you can see, I want to display a category name and descritpion only when its the first instance of that category.
With this code I get the following:
Test1
This category will be useful
* News for nerds, stuff that matters
* BBCI Link
* fooDisplay
It doesnt display the 'foo category for items 2 and 3. I'm not sure why. Could anyone help me with this? Am i using position() correctly?