xsl-list
[Top] [All Lists]

Re: [xsl] compare two nodes (the child elements, not the string values) in XSLT 1.0

2011-07-06 15:45:41
Hi,

A generalization of Michael's suggestion is to use a mode in which each element is processed to write strings, and then compare the strings.

So:

<xsl:variable name="author-string">
  <!-- generating an RTF containing a string -->
  <xsl:apply-templates select="$author" mode="string"/>
</xsl:variable>
<xsl:variable name="person-string">
  <!-- again -->
  <xsl:apply-templates select="$person" mode="string"/>
</xsl:variable>
<xsl:if test="$author-string = $person-string"> ...

You can then manage how each string is generated, for purposes of comparison, using templates matching in the mode. (You will find you need to do some of this in order, for example, to normalize whitespace.)

To compare an author to more than one person, wrap your calls in a template that writes a result string you can test for. So:

<xsl:template name="compare-person">
  <xsl:param name="author" select="/.."/>
  <xsl:param name="person" select="."/>
  <xsl:variable name="author-string">
    <xsl:apply-templates select="$author" mode="string"/>
  </xsl:variable>
  <xsl:variable name="person-string">
    <xsl:apply-templates select="$person" mode="string"/>
  </xsl:variable>
  <xsl:if test="$author-string = $person-string"> TRUE </xsl:if>
</xsl:template>

Then:

<xsl:template match="author">
  <xsl:variable name="author" select="."/>
  <xsl:variable name="compare-result">
    <xsl:for-each select="$persons">
      <!-- generating an RTF containing a string,
           possibly including 'TRUE' -->
      <xsl:call-template name="compare-person">
        <xsl:with-param name="author" select="$author">
      </xsl:call-template>
    <xsl:for-each>
  </xsl:variable>
  <xsl:if test="contains($compare-result,'TRUE')"> ...

Note this doesn't tell you which of your persons is (are) the same as your author. For that you have to work harder (there are ways).

This little exercise in XSLT 1.0 may well convince you that life is easier under XSLT 2.0.

Cheers,
Wendell

On 7/6/2011 4:22 PM, Michael Müller-Hillebrand wrote:
Am 06.07.2011 um 20:54 schrieb Wolfhart Totschnig:

I have a seemingly simple xslt problem to which I cannot find the solution. I want to test whether 
the context node, which is an<author>  element with the form (first?, middle?, last), is equal 
to one of a set of<person>  elements. By "equal" I mean having the same child 
elements and values of these elements. At first, I thought it would be as easy as

test="path/person = ."

But then I realized that the "=" operator compares the string values of the 
nodes

Wolfhart,

How about this (not very cool):

test="concat(path/person/first, '|', path/person/first, '|', path/person/first)
     = concat(first, '|', middle, '|', last)"

- Michael Müller-Hillebrand

--
======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--