Jump to content

Limit Attribute Output


kvnmck18

Recommended Posts

Say you have an xml like this:

<whatever><hmph sub="This is a subject"  cat="One" text="I don't have a text to that!" /><hmph sub="92 mph was fast"  cat="One" text="I think I have a car" /><hmph sub="9dfsdf3452 fsdfst"  cat="Two" text="Idfdf fdsfsdfa fasdfsdfr" /></whatever>

Image that XML being longer (and logical)...So say if you wanted to list the @cat... but you did not want repeats in the @catWhen you do a:

<xsl:for-each select="//hmph"><xsl:value-of select="@cat"/> <br/></xsl:for-each>

You result in:OneOneTwoWhat do you put to limit it to just one so it's like:OneTwo....

<xsl:for-each select="//hmph"><xsl:if test="position()=1"><xsl:value-of select="@cat"/> <br/></xsl:if></xsl:for-each>

...this gives you just:One....

<xsl:for-each select="//hmph"><xsl:if test="position()=3"><xsl:value-of select="@cat"/> <br/></xsl:if></xsl:for-each>

...this gives you just:TwoIs there an if test that is something like "no-repeat"?

Link to comment
Share on other sites

I've hearead of something called the "muncian method" (or something like that) of removing duplicates, but I don't understand it completely yet, because it uses xsl:key which is my weak spot. Without it, there's the EXSLT distinct() function, as well as the XSLT 2.0 distinct-values() function. I know that libxslt (available with PHP5) supports distinct(), but I don't know if Salbotron (PHP4) does too. And distinct-values() is out of the question. Only Saxon supports it and it is avaiable only for Java and .NET.

Link to comment
Share on other sites

The 'Muenchian' method uses a simple node-set union to determine if an item is unique in a node-set. This example will do what you wish. Note that the | operator is a union of node-sets and not an OR for this application.For a more complete explanation of this, I'll refer you to Jeni Tennison: http://www.jenitennison.com/xslt/grouping/muenchian.html

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:output method="html"/><xsl:key name="cats"  match="hmph" use="@cat"/>	<xsl:template match="/">		<xsl:for-each select="//hmph[count(. | key('cats', @cat)[1]) = 1]">			<xsl:value-of select="@cat"/> <br/>		</xsl:for-each>	</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...