[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: nodes having same values in same order

Subject: RE: nodes having same values in same order
From: Jarno.Elovirta@xxxxxxxxx
Date: Fri, 11 Apr 2003 11:58:31 +0300
xslt compare node values
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.