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

RE: [xsl] nodes having same values in same order


Subject: RE: [xsl] nodes having same values in same order
From: Jarno.Elovirta@xxxxxxxxx
Date: Fri, 11 Apr 2003 11:58:31 +0300

Hi,

> If I have a structure like the following -
> 
> <xxx>
>   <pp></pp>
>   <qq></qq>
>   <rr></rr>
> </xxx>
> <xxx>
>   <pp></pp>
>   <qq></qq>
> </xxx>
> 
> <yyy>
>   <pp></pp>
>   <qq></qq>
> </yyy>
> <yyy>
>   <pp></pp>
>   <qq></qq>
>   <rr></rr>
> </yyy>
> 
> How do I find out the yyy elements that have exactly
> the same elements(same order and count) as in xxx?

<xsl:template name="compare">
  <xsl:param name="ns1" select="/.." />
  <xsl:param name="ns2" select="/.." />

  <xsl:if test="count($ns1) = count($ns2)">
    <xsl:call-template name="compare-children">
      <xsl:with-param name="ns1" select="$ns1" />
      <xsl:with-param name="ns2" select="$ns2" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="compare-children">
  <xsl:param name="ns1" select="/.." />
  <xsl:param name="ns2" select="/.." />

  <xsl:choose>
    <xsl:when test="$ns1">
      <xsl:choose>
        <xsl:when test="name($ns1[1]) = name($ns2[1])">
          <xsl:call-template name="compare-children">
            <xsl:with-param name="ns1" select="$ns1[not(position() = 1)]" />
            <xsl:with-param name="ns2" select="$ns2[not(position() = 1)]" />
          </xsl:call-template>
        </xsl:when>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>true</xsl:otherwise>
  </xsl:choose>
</xsl:template>

Will "compare" return non-empty string if arguments $ns1 and $ns2 look equal. Note that it uses name() to test equality, so if the namespace prefixes differ, if will return incorrect results; not difficult to refine the equality comparison to handle both namespace and name, or to do deep comparisons. It's Friday, so I reserver the right to post solutions that have been written with my brain already turned off--"compare-children" would be faster if it used an index instead of tailing the arguments etc.

Cheers,

Jarno - Flesh Field: Prophecy (Assemblage 23 Remix)

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



Current Thread