xsl-list
[Top] [All Lists]

[xsl] Finding distinct nodes using a function in XSLT 2.0

2007-04-17 13:16:18
 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



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--