Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: comparing nodes in xslt >Thread Next - Re: comparing nodes in xslt Re: comparing nodes in xsltTo: 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())">
> <<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>
> </<xsl:value-of select="local-name()"/>>
> </xsl:when>
> <xsl:otherwise><xsl:value-of
> select="normalize-space(.)"/></xsl:otherwise>
> </xsl:choose>
> </xsl:for-each>
> </xsl:template>
>
> Joris Gillis
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
