Seleccione divs entre comentarios html en lxml

Este es mi archivo html

<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>

preguntado el 08 de noviembre de 11 a las 13:11

I just changed your divs, because you had them <div><div/> so if you have tried something and it aint worked double check that first. -

here is an article, with a plugin, bennadel.com/blog/… but not sure how it works, -

here is another answer, stackoverflow.com/questions/1623734/… -

Thanks for changing the divs I have just write manually so they are wrong. Actually It's a really big file!! -

I'm trying to do it with lxml (python) not javascript! Thanks anyway -

2 Respuestas

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

Or stricter version:

//*[preceding-sibling::comment()[. = 'Comment1'] 
    and following-sibling::comment()[. = 'Comment2']]

respondido 08 nov., 11:17

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

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

Cuando reemplazamos $ns1 y $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)
            ]

Verificación basada en XSLT:

<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>

cuando esta transformación se aplica en el documento XML proporcionado:

<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>

En XPath 2.0 one uses the intersect operador:

  /*/comment()[1]/following-sibling::div
intersect
 /*/comment()[2]/preceding-sibling::div

respondido 08 nov., 11:18

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.