Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: comparing nodes in xslt

From: David Carlisle <davidc@---.--.-->
To: NULL
Date: 10/4/2004 9:35:00 AM
inquirydog@h... (inquirydog) writes:

> Hi-
> 
>         Thanks for the reply.  I looked into it a bit and found that
> in XSLT 2 you can define xpath functions using xslt elements.  This
> certainly makes a solution like this more reasonable.  It does still
> leave certain problems like counting repeat nodes in a list....  For
> instance, if you wanted to eliminate doubles in a list like this
> 
> <list>
>         <element name="1" />
>         <element name="2" />
>         <element name="1" />
>         <element name="1" />
>         <element name="1" />
>         <element name="2" />
> </list>
> 
> converting to
> 
> <list>
> 
>         <element name"1" count="4" />
>         <element name="2" count="2" />
> 
> </list>
> 
> you need to group by element type and then count the number of
> elements.  Except for very constrained cases this seems to me to be
> impossible to do in xslt (yes I know about the muenchian method- it
> only works in a constrained case).  To do this grouping you need to
> first be able to check for node equality (which I think you can do in
> xslt 2) and then do the grouping based on the equality (which is
> better supported in xslt 2 but still can't do what I describe in this
> email).
> 

What do you mean by

> but still can't do what I describe in this email.

?

Your grouping example seems quite simple to do in either XSLT1 or XSLT2,
it might loook a bit simpler in 2 using the explict xsl:for-each-group
mechanism, but it doesn't take much code in either case.

For example in 1.0 it would be:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:key name="x" match="element" use="@name"/>

<xsl:template match="list">
 <list>
  <xsl:for-each select="element[generate-id()=generate-id(key('x',@name))]">
   <element name="{@name}" count="{count(key('x',@name))}"/>
  </xsl:for-each>
 </list>
</xsl:template>

</xsl:stylesheet>



David


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent