Re: [xsl] Finding corresponding node in structurally identical sibling tree?

Subject: Re: [xsl] Finding corresponding node in structurally identical sibling tree?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 26 Apr 2002 11:03:35 +0100
Hi Joel,

> I need to quickly find the matching node to the current node which
> is in a parallel branch with identical structure to the branch I'm
> operating in. It seems like this should be really simple, but the
> only solution I can come up with is a recursive template to assemble
> the name of the path from the branch tag down, and then compare that
> against the result from the same template for each node in the
> second tree. The trees may be of arbitrary depth. Any other ideas?

I think that the easiest way of doing this is to, while processing the
main branch, keep track of the corresponding node in the other branch.
You can do this by passing parameters holding the corresponding
node(s), as in:

<xsl:template match="*">
  <xsl:param name="corresponding" select="/.." />
  ...
  <xsl:for-each select="*">
    <xsl:apply-templates select=".">
      <xsl:with-param name="corresponding"
          select="$corresponding/*[name() = name(current())]" />
    </xsl:apply-templates>
  </xsl:for-each>
  ...
</xsl:template>

Start the process off by passing the branch2 element to the template
for the branch1 element:

<xsl:template match="example">
  <xsl:apply-templates select="branch1">
    <xsl:with-param name="corresponding" select="branch2" />
  </xsl:apply-templates>
</xsl:template>

and when you get to your someevendeeperstuff element, use the
$corresponding parameter to give you the information that you need:

<xsl:template match="someevendeeperstuff">
  <xsl:param name="corresponding" />
  Corresponding value: <xsl:value-of select="$corresponding" />
</xsl:template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread