Re: [xsl] Node comparison

Subject: Re: [xsl] Node comparison
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Sun, 15 Apr 2001 15:26:26 +0100
Hi Kevin,

> Having two different XML's joined on an entity reference I need to
> compare values underneath two different parent nodes without having
> to use a $Position Variable. The reason being is that I may not have
> a one to one relationship. So with an example below I would like to
> use my XSLT to base the values of Parent 2 on the like values in
> Parent 1.

The XML that you gave (as both source and result) isn't well formed.
I guess you actually mean something like:

<Parent num="1">
  <Value num="1">1</Value>
  <Value num="2">0</Value>
  <Value num="3">1</Value>
  <Value num="4">0</Value>
<Parent num="2">
  <Value num="1"> </Value>
  <Value num="2"> </Value>
  <Value num="2"> </Value>
  <Value num="3"> </Value>
  <Value num="4"> </Value>

So, you have an attribute (or something) in the Value elements that
shows what value they should take.  The result should copy the first
Parent element and everything in it:

  <xsl:copy-of select="Parent[@num = '1']" />

It should then create a Parent element with a num attribute equal to
2 (essentially a copy of the second Parent element):

  <Parent num="2">

And within that you want to process each of the Value elements under
the second Parent element.  For each of those Value elements, you want
to get the Value element within the first Parent element whose num
attribute is equal to the num attribute of the Value element that
you're processing (the current node):

  <Parent num="2">
    <xsl:variable name="parent1" select="Parent[@num = '1']" />
    <xsl:for-each select="Parent[@num = '2']/Value">
       <xsl:variable name="num" select="@num" />
       <Value num="{$num}">
          <xsl:value-of select="$parent1/Value[@num = $num]" />

You might find it more efficient to use a key to get the relevant
values.  You could create a key to index into the Value elements
within the first Parent by their num attribute:

<xsl:key name="values" match="Parent[@num = '1']/Value" use="@num" />

You can then get the relevant Value element with the key() function:

  <Parent num="2">
    <xsl:for-each select="Parent[@num = '2']/Value">
       <Value num="{@num}">
          <xsl:value-of select="key('values', @num)" />

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread