Domanda

Qualcuno può suggerire un formato di espressione XPath che restituisca un valore stringa contenente i valori concatenati di determinati nodi figlio qualificanti di un elemento, ma ignorandone altri:

<div>
    This text node should be returned.
    <em>And the value of this element.</em>
    And this.
    <p>But this paragraph element should be ignored.</p>
</div>

Il valore restituito dovrebbe essere una singola stringa:

This text node should be returned. And the value of this element. And this.

È possibile in una singola espressione XPath?

Grazie.

È stato utile?

Soluzione

In XPath 1.0:

Puoi usare

/div//text()[not(parent::p)]

per acquisire i nodi di testo desiderati. La stessa concatenazione non può essere eseguita in XPath 1.0, consiglio di farlo nell'applicazione host.

Altri suggerimenti

In XPath 2.0 :

string-join (/ * / node () [not (self :: p)], '')

/div//text()

forza doppia barra per estrarre il testo indipendentemente dai nodi intermedi

Questo aspetto funziona:

Uso come contesto / div / :

text() | em/text()

O senza l'uso del contesto:

/div/text() | /div/em/text()

Se vuoi concatenare le prime due stringhe, usa questo:

concat(/div/text(), /div/em/text())

Se vuoi tutti i bambini tranne p, puoi provare quanto segue ...

    string-join(//*[name() != 'p']/text(), "")

che restituisce ...

This text node should be returned.
And the value of this element.
And this.

Puoi usare anche un ciclo for-each e assemblare i valori in una variabile come questa

<xsl:variable name="newstring">
    <xsl:for-each select="/div//text()">
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:variable>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top