There is xml of a type:

<node> some_text_1 <sub_node_1> some_text_2 <sub_node_x> ... </sub_node_x> </sub_node_1> some_text_3 <sub_node_2> some_text_4 <sub_node_x> ... </sub_node_x> </sub_node_2> some_text_5 </node> 

How to get the text value "some_text_3" using XPath ?

Only XPath without code on the PL - a solution in one line of the form //node/???/text() .

  • Is it just a text or a set of tags? - These are completely different questions and answers - splash58
  • This is a plain text - Dmitry
  • My answer continues to work :) - splash58
  • The essence of the problem was in the question itself - “between two tags”. The example described one of many scenarios. Your solution works for the specific example presented earlier. Changed the content of the example, so that it completely describes the problem (the essence has not changed). For him, your answer does not work :( - Dmitry
  • I updated the answer - splash58

2 answers 2

 //node/text() 

you will get a set of three lines, since the text is broken by the child nodes

 Text='' Text='some_text_2' Text='' 

UPDATE All I can offer you is

 //node/text()[2] 

but this if it is always the second piece of text.

update2 To commentary to Dmitry's answer

I did not know that text () can be considered as an independent unit, and not a property of the node - thanks to Dmitry. This, indeed, enables the correct solution of the problem. In his answer, the first approximation is given. For the general case, we will think that the text we need can be dissected by other tags, for example,

  </sub_node_1> some_text<br/>3 <sub_node_2> 

His solution will give only some_text . To really find all the text between the given tags, the expression should be a bit more complicated.

 //node/sub_node_1/following-sibling::text()[following-sibling::sub_node_2] 

For the example above, it will give

 Text='some_text' Text='3' 
  • Thanks for the answer! But in the original task inside the tags sub_node_1 and sub_node_2 there may be other text and other nodes. The key point is that the text some_text is needed, without the contents of the specified nodes. - Dmitry
  • Good luck! Do not forget to accept the answer :) - splash58
  • I apologize for the incomplete description of the problem. - Dmitry
  • What is full? - splash58
  • the fragment number is not known in advance, but your answer made it possible to find an acceptable solution (did not know that the indexer could be applied to text ()) - Dmitry

To get the text between the desired tag and the next one after it, you can use the following XPath query:

  //node/sub_node_1/following-sibling::text()[1] 

Interpretation of the request - take the text following the sub_node_1 tag, which is on the same level with it.