I'm a beginner. I'm learning XPath.
This is the sample structure:

<AAA>
          <BBB/>
          <CCC/>
          <DDD/>
               <AAA/>
               <BBB/>
          </DDD>
          <div class="some">
               <AAA/>
               <BBB/>
               <div id="special">
                      <AAA/>
               </div>
               <div style="clear: both;"/>
               <hr/>
               <div> text </div>
               <div> text </div>
               <div> text </div>
               <hr/>
               <div> text </div>
               <div> text </div>
               <DDD/>
               <EEE/>
          </div>
     </AAA>

What XPath syntax should I write to detect:

1) Select the next six DIV elements after <div id="special"> ?

2) Select any of the next six elements after <div id="special"> ?

My incomplete (failed) trials: //div[@id='special'] + div + div + div + div + div + div

Recommended Answers

All 3 Replies

<AAA>
          <BBB/>
          <CCC/>
          <DDD/><!-- ERROR-->
               <AAA/>
               <BBB/>
          </DDD>
          <div class="some">
               <AAA/>
               <BBB/>
               <div id="special">
                      <AAA/>
               </div>
               <div style="clear: both;"/>
               <hr/>
               <div> text </div>
               <div> text </div>
               <div> text </div>
               <hr/>
               <div> text </div>
               <div> text </div>
               <DDD/>
               <EEE/>
          </div>
     </AAA>

best way to find xpath use a XML EDITOR
ther many find by Internet

for beginners Liquid Xml STudio easy for xpath learning
Predicate [] a filters
[6] equal position 6
* all nodes

//div[./@id="special"]/following-sibling::div[6]
//div[./@id="special"]/following-sibling::*[position()>6]
//div[./@id="special"]/following-sibling::div[6]

This only matches the sixth div.
The correct answer is //div[./@id="special"]/following-sibling::div[position()<=6]

//div[./@id="special"]/following-sibling::*[position()>6]

This is wrong. It should be <= not > The correct answer is //div[./@id="special"]/following-sibling::*[position()<=6]

Masterkey is correct.

Here is a similar xml document to the OPs but modified to differentiate the nodes.

<AAA>
    <BBB/>
    <CCC/>
    <DDD>
         <AAA/>
         <BBB/>
    </DDD>
    <div class="some">
         <AAA/>
         <BBB/>
         <div id="special">
               <AAA/>
         </div>
         <div style="clear: both;"> text 1 </div>
         <hr/>
         <div> text 2 </div>
         <div> text 3 </div>
         <div> text 4 </div>
         <hr/>
         <div> text 5 </div>
         <div> text 6 </div>
         <DDD/>
         <div> text 7 </div>
         <div> text 8 </div>
         <EEE/>
    </div>
</AAA>

and here is a simple stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" />

<xsl:template match="//div[@id='special']">
    <xsl:text>&#10;Less than or equal to 6: </xsl:text>
    <xsl:for-each select="following-sibling::div[position() &lt;= 6]">
        <xsl:value-of select="."/>
    </xsl:for-each>
    <xsl:text>&#10;Greater than 6: </xsl:text>
    <xsl:for-each select="following-sibling::div[position() &gt; 6]">
        <xsl:value-of select="."/>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
</xsl:template>


<xsl:template match="/">
   <xsl:apply-templates select="//div[@id='special']"/>
</xsl:template>

</xsl:stylesheet>

and here is output from transforming the document using this stylesheet

Less than or equal to 6:  text 1  text 2  text 3  text 4  text 5  text 6
Greater than 6:  text 7  text 8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.