Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: comparing nodes in xslt

From: inquirydog@-------.--- (----------)
To: NULL
Date: 10/1/2004 3:38:00 PM
Hi-

        Thanks for the reply.  I looked into it a bit and found that
in XSLT 2 you can define xpath functions using xslt elements.  This
certainly makes a solution like this more reasonable.  It does still
leave certain problems like counting repeat nodes in a list....  For
instance, if you wanted to eliminate doubles in a list like this

<list>
        <element name="1" />
        <element name="2" />
        <element name="1" />
        <element name="1" />
        <element name="1" />
        <element name="2" />
</list>

converting to

<list>

        <element name"1" count="4" />
        <element name="2" count="2" />

</list>

you need to group by element type and then count the number of
elements.  Except for very constrained cases this seems to me to be
impossible to do in xslt (yes I know about the muenchian method- it
only works in a constrained case).  To do this grouping you need to
first be able to check for node equality (which I think you can do in
xslt 2) and then do the grouping based on the equality (which is
better supported in xslt 2 but still can't do what I describe in this
email).

                                    thanks
                                            -I


> Hi,
> 
> I'm not quite sure this reply will be of any use to you.
> This solution only works with nodes with the same ordering.
> As far as I know, there are not (yet) Xpath expressions to deal with
> node comparison (or operations).
> 
> to compare e.g. node[1] and node[2], use :
> 
> <xsl:variable name="identical">
> <xsl:call-template name="check_identical">
>  <xsl:with-param name="comp1"><xsl:copy-of
> select="//node[1]"/></xsl:with-param>
>  <xsl:with-param name="comp2"><xsl:copy-of
> select="//node[2]"/></xsl:with-param>
> </xsl:call-template>
> </xsl:variable>
> <xsl:if test="$identical='true'">nodes completely are
> identical</xsl:if>
> <xsl:if test="$identical='false'">nodes differ in at least one
> aspect</xsl:if>
> 
> These 2 templates should be included/inserted in the stylesheet:
> 
> <xsl:template name="check_identical">
>  <xsl:param name="comp1"/>
>  <xsl:param name="comp2"/>
>  <xsl:variable name="string1">
>   <xsl:call-template name="stringify">
>    <xsl:with-param name="node"><xsl:copy-of
> select="$comp1"/></xsl:with-param>
>   </xsl:call-template>
>  </xsl:variable>
>  <xsl:variable name="string2">
>   <xsl:call-template name="stringify">
>    <xsl:with-param name="node"><xsl:copy-of
> select="$comp2"/></xsl:with-param>
>  </xsl:call-template>
>  </xsl:variable>
>  <xsl:value-of select="$string1=$string2"/>
> </xsl:template>
> 
> The following template 'stringifies' a given node i.e. convert it to
> the ascii equivalent like one would see when opening an xml file with
> a text editor.
> 
> <xsl:template name="stringify">
>  <xsl:param name="node"/> 
>   <xsl:for-each select="$node/*/*|$node/*/text()">
>    <xsl:choose>
>     <xsl:when test="boolean(local-name())">
>     &lt;<xsl:value-of select="local-name()"/>
>     <xsl:variable name="pos" select="position()"/>
>     <xsl:for-each select="@*">
>     <xsl:text> </xsl:text><xsl:value-of
> select="local-name()"/>="<xsl:value-of select="."/>"
>     </xsl:for-each>
>     <xsl:call-template name="stringify">
>      <xsl:with-param name="node"><xsl:copy-of
> select="."/></xsl:with-param>
>     </xsl:call-template>
>     &lt;/<xsl:value-of select="local-name()"/>&gt;
>     </xsl:when>
>    <xsl:otherwise><xsl:value-of
> select="normalize-space(.)"/></xsl:otherwise>
>    </xsl:choose>
>   </xsl:for-each>
> </xsl:template>
> 
>      Joris Gillis


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent