xpath: comparing two node sets

Discussion in 'XML' started by Andy Fish, Mar 9, 2005.

  1. Andy Fish

    Andy Fish Guest

    Hi,

    I just found this template in someone else's xslt (it's Microsoft's
    "word2html" stylesheet to convert WordProcessingML to HTML)

    <xsl:template match="WX:sect">
    <xsl:variable name="thisSect" select="."/>
    <div>
    <xsl:for-each select="//WX:sect">
    <xsl:if test=".=$thisSect">
    <xsl:attribute name="class">Section<xsl:value-of
    select="position()"/></xsl:attribute>
    .....

    It seems that the author is looping through all the WX:sect nodes looking
    for the context node, in order to extract the position. However, I don't
    understand the <xsl:if> test. According to the xpath spec:

    "If both objects to be compared are node-sets, then the comparison will be
    true if and only if there is a node in the first node-set and a node in the
    second node-set such that the result of performing the comparison on the
    string-values of the two nodes is true".

    For this code to work, the "=" operator needs to compare two node sets to
    see if they are pointing at the same object (like == in Java), which is a
    very different thing. But the output looks correct.

    Can anyone shed some light on this?

    TIA

    Andy
     
    Andy Fish, Mar 9, 2005
    #1
    1. Advertising

  2. Andy Fish

    Soren Kuula Guest

    Andy Fish wrote:

    >Hi,
    >
    >I just found this template in someone else's xslt (it's Microsoft's
    >"word2html" stylesheet to convert WordProcessingML to HTML)
    >
    ><xsl:template match="WX:sect">
    > <xsl:variable name="thisSect" select="."/>
    > <div>
    > <xsl:for-each select="//WX:sect">
    > <xsl:if test=".=$thisSect">
    > <xsl:attribute name="class">Section<xsl:value-of
    >select="position()"/></xsl:attribute>
    >....
    >
    >It seems that the author is looping through all the WX:sect nodes looking
    >for the context node, in order to extract the position. However, I don't
    >understand the <xsl:if> test. According to the xpath spec:
    >
    >"If both objects to be compared are node-sets, then the comparison will be
    >true if and only if there is a node in the first node-set and a node in the
    >second node-set such that the result of performing the comparison on the
    >string-values of the two nodes is true".
    >
    >

    Yes, it doesn't work, strictly speaking.

    I simplified the stylesheet (namwspaces away, and a little guessing):

    <?xml version='1.0'?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput method="xml" indent="yes"/>

    <xsl:template match="doc">
    <toc>
    <xsl:apply-templates/>
    </toc>
    </xsl:template>

    <xsl:template match="sect">
    <xsl:variable name="thisSect" select="."/>
    <div>
    <xsl:for-each select="//sect">
    <xsl:if test=".=$thisSect">
    <xsl:attribute name="class">Section<xsl:value-of
    select="position()"/></xsl:attribute>
    </xsl:if>
    </xsl:for-each>

    <stupid-summary>
    <xsl:value-of select="."/>
    </stupid-summary>

    </div>
    </xsl:template>
    </xsl:stylesheet>

    With the input doc:
    <doc>
    <sect>
    <title>About foo</title>
    <para>foo is green</para>
    </sect>
    <sect>
    <title>About bar</title>
    </sect>
    <sect>
    <title>About baz</title>
    </sect>
    <sect>
    <title>About foo</title>
    <para>Correction: foo is red</para>
    </sect>
    <sect>
    <title>About bar</title>
    </sect>
    </doc>


    it transforms (xsltproc) to:
    <?xml version="1.0"?>
    <toc>
    <div class="Section1"><stupid-summary>
    About foo
    foo is green
    </stupid-summary></div>
    <div class="Section5"><stupid-summary>
    About bar
    </stupid-summary></div>
    <div class="Section3"><stupid-summary>
    About baz
    </stupid-summary></div>
    <div class="Section4"><stupid-summary>
    About foo
    Correction: foo is red
    </stupid-summary></div>
    <div class="Section5"><stupid-summary>
    About bar
    </stupid-summary></div>
    </toc>

    The thing compared is the text value of the node sets (as you wrote).
    They are the same for the 2nd and 5th sect elements -- so two attributes
    with the same name were output -- the last one won; there is a section
    5 after section 1.

    Of course, having the same title child but some different (text values
    of) other children (1st and 4th sect elements) WILL distinguish the text
    values of the nodes.

    Soren
     
    Soren Kuula, Mar 9, 2005
    #2
    1. Advertising

  3. "Andy Fish" <> writes:

    > Hi,
    >
    > I just found this template in someone else's xslt (it's Microsoft's
    > "word2html" stylesheet to convert WordProcessingML to HTML)
    >
    > <xsl:template match="WX:sect">
    > <xsl:variable name="thisSect" select="."/>
    > <div>
    > <xsl:for-each select="//WX:sect">
    > <xsl:if test=".=$thisSect">
    > <xsl:attribute name="class">Section<xsl:value-of
    > select="position()"/></xsl:attribute>
    > ....
    >
    > It seems that the author is looping through all the WX:sect nodes looking
    > for the context node, in order to extract the position. However, I don't
    > understand the <xsl:if> test. According to the xpath spec:
    >
    > "If both objects to be compared are node-sets, then the comparison will be
    > true if and only if there is a node in the first node-set and a node in the
    > second node-set such that the result of performing the comparison on the
    > string-values of the two nodes is true".
    >
    > For this code to work, the "=" operator needs to compare two node sets to
    > see if they are pointing at the same object (like == in Java), which is a
    > very different thing. But the output looks correct.
    >
    > Can anyone shed some light on this?
    >
    > TIA
    >
    > Andy


    the code you posted compares the string value of the node (ie the
    concatenation of all the descendent text nodes), and the string value of
    every other sect node in the document, it will do this for every sect in
    the document even if the first one tests equal as as it's written it
    needs to use the last such found.

    This is likely to be slow/expensive.

    If node identity is intended the test should be
    <xsl:if test="count(.|$thisSect)=1">

    But even then a search on // seems a very strange way to calculate this
    number I think probably

    <xsl:template match="WX:sect">
    <div class="Section{count(preceding::WX:sect)}">
    ....

    or

    <xsl:template match="WX:sect">
    <div>
    <xsl:attribute name="class">Section<xsl:number level="any"/></xsl:attribute>
    ....

    was intended, but hard to tell just from the sample you posted.

    > But the output looks correct.

    even if you have two sect elements with the same text content?

    David
     
    David Carlisle, Mar 9, 2005
    #3
  4. Andy Fish

    Andy Fish Guest

    Thanks to both for these replies - I see what's going on now.

    I'll replace it with count(preceeding...). hopefully that might speed it up
    a bit too...

    Andy

    "David Carlisle" <> wrote in message
    news:...
    > "Andy Fish" <> writes:
    >
    >> Hi,
    >>
    >> I just found this template in someone else's xslt (it's Microsoft's
    >> "word2html" stylesheet to convert WordProcessingML to HTML)
    >>
    >> <xsl:template match="WX:sect">
    >> <xsl:variable name="thisSect" select="."/>
    >> <div>
    >> <xsl:for-each select="//WX:sect">
    >> <xsl:if test=".=$thisSect">
    >> <xsl:attribute name="class">Section<xsl:value-of
    >> select="position()"/></xsl:attribute>
    >> ....
    >>
    >> It seems that the author is looping through all the WX:sect nodes looking
    >> for the context node, in order to extract the position. However, I don't
    >> understand the <xsl:if> test. According to the xpath spec:
    >>
    >> "If both objects to be compared are node-sets, then the comparison will
    >> be
    >> true if and only if there is a node in the first node-set and a node in
    >> the
    >> second node-set such that the result of performing the comparison on the
    >> string-values of the two nodes is true".
    >>
    >> For this code to work, the "=" operator needs to compare two node sets to
    >> see if they are pointing at the same object (like == in Java), which is a
    >> very different thing. But the output looks correct.
    >>
    >> Can anyone shed some light on this?
    >>
    >> TIA
    >>
    >> Andy

    >
    > the code you posted compares the string value of the node (ie the
    > concatenation of all the descendent text nodes), and the string value of
    > every other sect node in the document, it will do this for every sect in
    > the document even if the first one tests equal as as it's written it
    > needs to use the last such found.
    >
    > This is likely to be slow/expensive.
    >
    > If node identity is intended the test should be
    > <xsl:if test="count(.|$thisSect)=1">
    >
    > But even then a search on // seems a very strange way to calculate this
    > number I think probably
    >
    > <xsl:template match="WX:sect">
    > <div class="Section{count(preceding::WX:sect)}">
    > ...
    >
    > or
    >
    > <xsl:template match="WX:sect">
    > <div>
    > <xsl:attribute name="class">Section<xsl:number
    > level="any"/></xsl:attribute>
    > ...
    >
    > was intended, but hard to tell just from the sample you posted.
    >
    >> But the output looks correct.

    > even if you have two sect elements with the same text content?
    >
    > David
     
    Andy Fish, Mar 10, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,675
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,537
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Doug

    Comparing node sets

    Doug, Jul 25, 2003, in forum: XML
    Replies:
    1
    Views:
    522
    Marrow
    Jul 25, 2003
  4. John Salerno

    comparing values in two sets

    John Salerno, May 14, 2006, in forum: Python
    Replies:
    11
    Views:
    570
    Gerard Flanagan
    May 15, 2006
  5. Terry L. Ridder
    Replies:
    4
    Views:
    140
    Quantum Mechanic
    Oct 14, 2003
Loading...

Share This Page