[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Finding distinct nodes using a function in XSLT 2.0

Subject: Finding distinct nodes using a function in XSLT 2.0
From: "Scott Lynch" <slynch@xxxxxxxxxx>
Date: Tue, 17 Apr 2007 16:15:57 -0400
 Finding distinct nodes using a function in XSLT 2.0
 This probably isn't the easiest way to this (suggestions welcome), but
I'm collecting data from two similarly structured source documents and
wanting to build a set of distinct nodes based on a certain attribute
value. Example XML:

doc1.xml
<foo>
  <bar id="a" />
  <bar id="b" />
  <bar id="c" />
  <bar id="d" />
  <bar id="g" />
</foo>

doc2.xml
<foo>
  <bar id="a" />
  <bar id="b" />
  <bar id="c" />
  <bar id="d" />
  <bar id="e" />
  <bar id="f" />
</foo>

Desired resulting node set:

<foo>
  <bar id="a" />
  <bar id="b" />
  <bar id="c" />
  <bar id="d" />
  <bar id="e" />
  <bar id="f" />
  <bar id="g" />
</foo>

I'm required to provide a template function (or equivalent set of
templates which returns a node set for use by various for-each
constructs in other processors) which accepts the xpath (e.g. /foo/bar)
and an index attribute (e.g. "id") as parameters and returns a node set
as seen above.

I've managed to write a function which generates a sorted union (I think
I need them sorted to find the distinct values, correct? sorry, newbie
here..), but have yet to find a way to trim the results set so that it
contains only distinct members. Here's what I have so far:

  <xsl:variable name="compare_file" select="document('doc2.xml')"/>
  <xsl:variable name="current_file" select="document('doc1.xml')"/>

  <xsl:function name="con:distinct_rows">
    <!-- Selects the distinct rows from the source docs and -->
    <!-- comparison doc                                     -->
    <xsl:param name="path"/>
    <xsl:param name="key"/>
    <xsl:variable name="source_rows"
select="$current_file/saxon:evaluate($path)"/>
    <xsl:variable name="compare_rows"
select="$compare_file/saxon:evaluate($path)"/>
    <xsl:variable name="sorted_union_rows">
      <xsl:for-each select="$source_rows | $compare_rows">
        <xsl:sort data-type="text" select="./saxon:evaluate($key)"/>
        <xsl:sequence select="."/>
      </xsl:for-each>
    </xsl:variable>
    <xsl:sequence select="$sorted_union_rows"/>
    <!-- ??? distinct node selector instead ??? -->
  </xsl:function>


Once I have the sorted union, I've tried various following/previous
sibling selectors but have yet to find a command which generates the
distinct node set (I can find the unique nodes based on the "id" but
that's not really helping me).

Any suggestions?

thanks a lot!
Scott Lynch

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.