Reputation: 295
I am new to XPath so please bear with me. Basic question.
I want to select text, including the elements as well. Example input below:
<node1>
<node2>
<node3>A1</node3>
<node3>A2</node3>
<node3>A3</node3>
<node3>A4</node3>
</node2>
</node1>
I wish to copy the content of node 2 including the tags, which is:
<node2>
<node3>A1</node3>
<node3>A2</node3>
<node3>A3</node3>
<node3>A4</node3>
</node2>
However, if I use the XPath /node1/node2/
, I only get values A1 A2 A3 A4
(not the node2
and node3
tags).
Upvotes: 0
Views: 275
Reputation: 2583
to select node2 elements and everything inside of it use:
xPath:
//node2/descendant-or-self::*
css:
node2,node2 *
to select only inner content without node2 tag by itself:
xPath:
//node2/descendant::*
css:
node2 *
Upvotes: 0
Reputation: 682
./node1/node()
or:
./node1/node2/../node()
When you want the whole HTML node, end the Xpath with /node().
Upvotes: 0
Reputation: 111491
However if I use xpath as
/node1/node2
- I only get valuesA1 A2 A3 A4
(not thenode2
andnode3
tags).
Incorrect. The XPath, /node1/node2
selects node2
element children of node1
, not text nodes or strings. If you are seeing just A1 A2 A3 A4
for this XPath, then you are evaluating the XPath in a context that automatically converts to string values.
You do not need a new XPath; you need to evaluate the results of the XPath that you already have differently. For example, if you're using
<xsl:value-of select="/node1/node2"/>
you should instead be iterating over the nodeset or applying templates to all elements in the nodeset,
<xsl:apply-templates select="/node1/node2"/>
or in XSLT 2.0, returning a sequence,
<xsl:sequence select="/node1/node2"/>
etc, depending upon exactly what you need to do with the selected elements.
Upvotes: 0
Reputation: 375
Correct XPath query to fetch div inner text
Use the text() function:
//node1/node2/text()
Upvotes: -1
Reputation: 3058
The xpath /node1/node2
should return a list of nodes
. Each node
, depending on what language you are using contains attributes such as nodeName
or nodeValue
that you could use to access both the contained text and the name of the tag.
Upvotes: 0