Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Use variables to get unique nodes >Thread Next - Re: Use variables to get unique nodes Re: Use variables to get unique nodesTo: NULL Date: 10/1/2004 10:03:00 PM Hi Rolf,
When comparing node-sets it is as well to remember that x != y is not quite
the same as not(x = y).
But anyway, I don't think it's worth trying to do what you want using the
preceding method for obtaining uniques - the Muenchian technique will be
much easier and yield far better performance, e.g.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kDistinctPins" match="Pin" use="@PinName"/>
<xsl:variable name="TestNodes" select="Root/TEST/Pin"/>
<xsl:variable name="MultiplePins"
select="$TestNodes[count(key('kDistinctPins',@PinName)) > 1]"/>
<xsl:variable name="UniqueMultiplePins"
select="$MultiplePins[generate-id() =
generate-id(key('kDistinctPins',@PinName))]"/>
<xsl:template match="/">
<xsl:text>all pins of all test nodes ==> OK </xsl:text>
<xsl:for-each select="$TestNodes">
<xsl:value-of select="@PinName"/>
<xsl:text> </xsl:text>
</xsl:for-each>
<xsl:text> multiple pins ==> OK </xsl:text>
<xsl:for-each select="$MultiplePins">
<xsl:value-of select="@PinName"/>
<xsl:text> </xsl:text>
</xsl:for-each>
<xsl:text> unique pins ==> OK </xsl:text>
<xsl:for-each select="$UniqueMultiplePins">
<xsl:value-of select="@PinName"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Btw, don't overuse the concat() function - especially not for literal
output... you are just doing concatenation where the transformation engine
will already serialize the output.
HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
"Rolf Kemper" <KemperR@e...> wrote in message
news:bbd6fe79.0410010704.2944868d@p......
> Dear Experts,
>
> I got stuck with the following problem and need your help.
>
> What I wnat to do is to get a set of distinct nodes.
> Before the distinct I have selected the multiple occourences already
> sucsessfully. However , the rest does not work as expected.
>
> Hope someone can help on that.
> Rolf
>
> ############################### DATA ################################
>
> My XML DATA:
> <?xml version="1.0" encoding="UTF-8"?>
> <Root>
> <TEST>
> <Pin PinName="A" />
> <Pin PinName="B" />
> <Pin PinName="B" />
> <Pin PinName="C" />
> <Pin PinName="X" />
> </TEST>
> <TEST>
> <Pin PinName="A" />
> <Pin PinName="D" />
> <Pin PinName="C" />
> <Pin PinName="X" />
> <Pin PinName="A" />
> </TEST>
> </Root>
>
>
> My Test XSLT:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:fo="http://www.w3.org/1999/XSL/Format">
> <xsl:output method="text"/>
> <xsl:variable name="NewLine" select="'
'"/>
>
> <xsl:variable name="TestNodes" select="Root/TEST"/>
> <xsl:variable name="MultiplePins" select="$TestNodes/Pin[@PinName =
> preceding::Pin/@PinName]"/>
> <xsl:variable name="UniqueMultiplePins"
> select="$MultiplePins[@PinName != preceding::*/@PinName]"/>
>
> <xsl:template match="/">
> <xsl:value-of select="concat('all pins of all test nodes ==>
> OK ',$NewLine)"/>
> <xsl:for-each select="$TestNodes/Pin">
> <xsl:value-of select="concat(@PinName,$NewLine)"/>
> </xsl:for-each>
> <xsl:value-of select="$NewLine"/>
>
> <xsl:value-of select="concat('multiple pins ==> OK ',$NewLine)"/>
> <xsl:for-each select="$MultiplePins">
> <xsl:value-of select="concat(@PinName,$NewLine)"/>
> </xsl:for-each>
> <xsl:value-of select="$NewLine"/>
>
> <xsl:value-of select="concat('unique pins ==> NOT GOOD !!
> ',$NewLine)"/>
> <xsl:for-each select="$UniqueMultiplePins">
> <xsl:value-of select="concat(@PinName,$NewLine)"/>
> </xsl:for-each>
>
> </xsl:template>
>
> </xsl:stylesheet>
>
> My results (gained by XMLSpy debug mode):
>
> all pins of all test nodes ==> OK
> A
> B
> B
> C
> X
> A
> D
> C
> X
> A
>
> multiple pins ==> OK
> B
> A
> C
> X
> A
>
> unique pins ==> NOT GOOD !!
> B
> A
> C
> X
> A
>
> ( I expected B A C X )
> #################### End #################################
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
