[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Nodes and Strings


Subject: Re: [xsl] Nodes and Strings
From: andrew welch <andrew.j.welch@xxxxxxxxx>
Date: Tue, 26 Jul 2005 09:04:24 +0100

> >I have two sets that I need to compare but the values aren't the same.
> >Example:
> >
> ><setA>
> >     <element>value1</element>
> >     <element>value2</element>
> >     <element>value3</element>
> >     <element>value4</element>
> >     <element>value5</element>
> ></setA>
> >
> ><setB>
> >     <element>aStringBeforeValue.value1</element>
> >     <element>aStringBeforeValue.value2</element>
> >     <element>aStringBeforeValue.value3</element>
> ></setB>
> >
> >expected result (for intersection between the sets):
> >
> ><setC>
> >     <element>value1</element>
> >     <element>value2</element>
> >     <element>value3</element>
> ></setC>
> >
> >I want to get the intersection (difference, whatever) between two sets
> >that haven't the same string values for their elements... (I didn't
> >found a function like substring, for example, that receives as
> >parameter
> >a node list... only for strings... So, I'm resolving this problem with
> >a
> >little bit long and confused code.)
> >Is it possible?

Here's a potential XSLT 2.0 solution:

<xsl:template match="/">
  <setC>
      <xsl:copy-of select="for $a in //setA/element return
		                    for $b in //setB/element return
		                     if (contains($b, $a)) then $a else ''"/>
  </setC>
</xsl:template>

I think there is probably a better one, one which doesn't involve the
if then else, and which won't produce duplicates (if that is the
requirement).

A 1.0 solution would be a pair of nested for-each's which check each
element in set A against each element in set B - the same as above but
in long hand really.

cheers
andrew


Current Thread
Keywords