3

This is my html file

<div>
   <div></div>
   <div></div>
   <!--Comment1-->
   <div>1</div>
   <div>2</div>
   <div>3</div>
   <!--Comment2-->
   <div></div>
   <div></div>
   <div></div>
</div>

and I want to select divs between Comment 1 and Comment 2

With this xPath = "/div/comment()" I can select <!--Comment1--><!--Comment2-->

But I want to select this

   <div>1</div>
   <div>2</div>
   <div>3</div>
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
dani
  • 341
  • 1
  • 7
  • 18
  • I just changed your divs, because you had them `
    ` so if you have tried something and it aint worked double check that first.
    – Val Nov 08 '11 at 13:16
  • here is an article, with a plugin, http://www.bennadel.com/blog/1563-jQuery-Comments-Plug-in-To-Access-HTML-Comments-For-DOM-Templating.htm but not sure how it works, – Val Nov 08 '11 at 13:18
  • here is another answer, http://stackoverflow.com/questions/1623734/selecting-html-comments-with-jquery – Val Nov 08 '11 at 13:19
  • Thanks for changing the divs I have just write manually so they are wrong. Actually It's a really big file!! – dani Nov 08 '11 at 13:21
  • I'm trying to do it with lxml (python) not javascript! Thanks anyway – dani Nov 08 '11 at 13:22

2 Answers2

7
//*[preceding-sibling::comment() and following-sibling::comment()]

Or stricter version:

//*[preceding-sibling::comment()[. = 'Comment1'] 
    and following-sibling::comment()[. = 'Comment2']]
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
1

In XPath 1.0 one can use the general (Kayessian) method for intersection of two node-sets $ns1 and $ns2:

$ns1[count(.|$ns2) = count($ns2)]

When we replace $ns1 and $ns2 with their specific selecting expressions for this particular case, we get:

/*/comment()[1]/following-sibling::div
            [count(. | /*/comment()[2]/preceding-sibling::div)
            =
             count(/*/comment()[2]/preceding-sibling::div)
            ]

XSLT-based verification:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "/*/comment()[1]/following-sibling::div
            [count(. | /*/comment()[2]/preceding-sibling::div)
            =
             count(/*/comment()[2]/preceding-sibling::div)
            ]
     "/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<div>
   <div></div>
   <div></div>
   <!--Comment1-->
   <div>1</div>
   <div>2</div>
   <div>3</div>
   <!--Comment2-->
   <div></div>
   <div></div>
   <div></div>
</div>

The exact wanted nodes are selected and output:

<div>1</div>
<div>2</div>
<div>3</div>

In XPath 2.0 one uses the intersect operator:

  /*/comment()[1]/following-sibling::div
intersect
 /*/comment()[2]/preceding-sibling::div
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431