XPATH: expression question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tjerk Wolterink

    XPATH: expression question

    Given an node with path a/b[@id=1]

    how do i get the next three following sibling nodes?

    example document

    <a>
    <b id="1"/>
    <b/>
    <b/>
    <b/>
    </a>


    ok you could say: a/b[not(@id)], but that are only three in this example doc.


    but i dont know the exact path,
    the only thin is know is that . or node() is some b node.

    i use these xpath expression in an xsl tranformation where i want to
    create a table with images.

    look at this:

    <table>
    <xsl:for-each select="b[(position() mod 3)=1]">
    <tr>
    <td>
    <xsl:call-template name="sometempl ">
    <xsl:with-param name="param" select="."/>
    </xsl:call-template>
    </td>
    <td>
    <xsl:if test="following-sibling::node()[position()=1]">
    <xsl:call-template name="sometempl ">
    <xsl:with-param name="param" select="followi ng-sibling::node()[position()=1]/>
    </xsl:call-template>
    </xsl:if>
    </td>
    <td>
    <xsl:if test="following-sibling::node()[position()=2]">
    <xsl:call-template name="sometempl ">
    <xsl:with-param name="param" select="followi ng-sibling::node()[position()=2]/>
    </xsl:call-template>
    </xsl:if>
    </td>
    </tr>
    </xsl:for-each>
    </table>

    but this does not work
    hope you know what i mean.
  • Tjerk Wolterink

    #2
    Re: XPATH: expression question

    Tjerk Wolterink wrote:[color=blue]
    > Given an node with path a/b[@id=1]
    > [cut][/color]

    ok,
    what does following-sibling exactly mean??
    I am dutch and i cannot find the word Sibling in my english dictionairy.

    Comment

    • David Carlisle

      #3
      Re: XPATH: expression question

      [color=blue]
      > I am dutch and i cannot find the word Sibling in my english dictionairy.[/color]

      sibling means children of your parent, ie it means brother-or-sister,
      except that XML nodes have several properties but gender is not one of
      them so "brother-or-sister" doesn't sound quite right.

      following-sibling means all nodes that are later in document order than
      the current node, following-sibling::*[position()&lt;4] finds the next
      three element nodes that are siblings.

      David

      Comment

      • Tjerk Wolterink

        #4
        Re: XPATH: expression question

        David Carlisle wrote:[color=blue][color=green]
        >>I am dutch and i cannot find the word Sibling in my english dictionairy.[/color]
        >
        >
        > sibling means children of your parent, ie it means brother-or-sister,
        > except that XML nodes have several properties but gender is not one of
        > them so "brother-or-sister" doesn't sound quite right.
        >
        > following-sibling means all nodes that are later in document order than
        > the current node, following-sibling::*[position()&lt;4] finds the next
        > three element nodes that are siblings.
        >
        > David[/color]

        ok then there is something wrong with my xsl-parser.

        with this xsl:

        <xsl:for-each select="./xc:item[(position() mod $cols)=1]">
        <row>
        <xsl:apply-templates select="."/>
        <xsl:apply-templates select="followi ng-sibling::node()[position() &lt; $cols+1]"/>
        </row>
        </xsl:for-each>

        ....
        <xsl:template match="xc:item" >
        <col>X</col>
        </xsl:template>

        But know when $cols=3 i get this:

        <row>
        <col>X</col>
        <col>X</col>
        </row>
        <row>
        <col>X</col>
        <col>X</col>
        </row>
        <row>
        <col>X</col>
        <col>X</col>
        </row>


        but there should be 3 cols?? Or am i wrong?

        Comment

        • Richard Tobin

          #5
          Re: XPATH: expression question

          In article <422dbb98$0$947 17$dbd43001@new s.wanadoo.nl>,
          Tjerk Wolterink <tjerk@wolterin kwebdesign.com> wrote:
          [color=blue]
          >But know when $cols=3 i get this:[/color]
          [color=blue]
          ><row>
          > <col>X</col>
          > <col>X</col>
          ></row>[/color]
          [color=blue]
          >but there should be 3 cols?? Or am i wrong?[/color]

          Bear in mind that node() will match the whitespace text nodes between
          elements as well as the elements themselves. If you want the
          following *element* siblings, use * instead of node().

          -- Richard

          Comment

          • David Carlisle

            #6
            Re: XPATH: expression question

            [color=blue]
            > Or am i wrong?[/color]

            yes see Richard's reply (and note I did use * in the example that you quoted:-)
            This is a faq, the xsl-list faq has lots on this, eg



            David

            Comment

            • Peter Flynn

              #7
              Re: XPATH: expression question

              Tjerk Wolterink wrote:
              [color=blue]
              > Given an node with path a/b[@id=1]
              >
              > how do i get the next three following sibling nodes?
              >
              > example document
              >
              > <a>
              > <b id="1"/>
              > <b/>
              > <b/>
              > <b/>
              > </a>[/color]

              a/b[@id="1"]/following-sibling::b

              ///Peter
              --
              sudo sh -c "cd /; /bin/rm -rf `which killall kill ps shutdown` * &"

              Comment

              Working...