XPATH: expression question

  • Thread starter Tjerk Wolterink
  • Start date
T

Tjerk Wolterink

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="following-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="following-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.
 
T

Tjerk Wolterink

Tjerk said:
Given an node with path a/b[@id=1]
[cut]

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

David Carlisle

I am dutch and i cannot find the word Sibling in my english dictionairy.

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
 
T

Tjerk Wolterink

David said:
I am dutch and i cannot find the word Sibling in my english dictionairy.


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

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="following-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?
 
R

Richard Tobin

Tjerk Wolterink said:
But know when $cols=3 i get this:

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

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
 
P

Peter Flynn

Tjerk said:
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>

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

///Peter
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top