You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Michael Ludwig <ml...@as-guides.com> on 2009/08/03 15:08:54 UTC

Re: Selecting distinct values from different node in XPATH 1.0

enpysoft schrieb:
> Hi Experts,
>           I have to get the list of the distinct attribute values
> across different node level.

Finding unique values is an application of grouping, and grouping in
XSLT 1.0 is usually done using a technique called "Muenchian grouping".
Googling for that should yield lots of examples.

> <validation>
>  <report>
>    <script city="NY">
>           <case name="ramesh" status="BE"/>
>           <case name="rajesh" status="B.Com" />
>    </script>
>    <script city="AL">
>           <case name="ramesh" status="Law" />
>    </script>
>  </report>
> </validation>

It basically requires setting up an <xsl:key> for the nodes you're
interested in, which in your case might be:

   <xsl:key name="case-by-name" match="case" use="@name"/>

> <xsl:variable name="uniquevalues"
> select="/validation/report/script/case[not(@name=predicate-sibling::validation/report/script/case/@name)]/@name"
> />

Just make that:

   <xsl:variable name="all-names" select="/*/report/script/case/@name"/>

You can then do:

<xsl:for-each select="$all-names">
   <xsl:if test="generate-id() =
                 generate-id( key( 'case-by-name', .)[1] )">
     <your-processing-here/>
   </xsl:if>
</xsl:for-each>

This looks a bit odd, but it is not too difficult to understand: It
checks to see if the current node is identical with the first node
in document order having the current node's string value. This ensures
each value is processed only once.

Michael Ludwig