Luis Guillermo
Luis Guillermo

Reputation: 107

How to select all text inside a node using XPath?

Is there a way to select the text inside any of these div class="cont" using XPath? An XPath that will grab any text inside div class="cont", it can be directly inside div or within a span or p.

<div class="detail-block">
  <div id="description-content-wrapper" class="">
    <h3 class="brdt ">Descripción</h3>
    <div class="cont">
      <p>Este curso permite al alumno obtener una doble titulación gracias al sistema de 
         convalidación de assignaturas entre las dos carrerras.</p>
    </div>
  </div>
</div>
<div class="detail-block">
  <div id="description-content-wrapper" class="textosmas openmore" style="height: 493px;">
    <h3 class="brdt ">Descripción</h3>
    <div class="cont">El Grado en Ingeniería Informática combina un plan de estudios (3 años de 
     docencia + 1 de prácticas) creado con el asesoramiento de IBM, Telefónica, Indra, BQ y Atos 
     y añade la posibilidad de realizar un innovador Diploma en Transformación Digital, 
     impartido por empresas punteras del sector
    </div>
  </div>
</div>

Upvotes: 1

Views: 84

Answers (1)

kjhughes
kjhughes

Reputation: 111696

You can select all of the text nodes beneath the current node via

.//text()

Alternatively, you might prefer to work with node's string value...

XPath 1.0

If you know there'll only be a single node selected by your XPath expression,

normalize-space(//div[@id='description-content-wrapper']/div[@class="cont"])

will return the space-normalized string value of the targeted node.

If there could be multiple such nodes, you'll have to iterate over them in the hosting language as you would with multiple text nodes. However, if your library supports XPath 2.0...

XPath 2.0

This XPath,

//div[@id='description-content-wrapper']/div[@class="cont"]/normalize-space()

will return all of the space-normalized string values of the selected node or nodes.

Upvotes: 1

Related Questions