config show For example, to read that one with xpath I do: //command[@name='confi" /> config show For example, to read that one with xpath I do: //command[@name='confi" /> config show For example, to read that one with xpath I do: //command[@name='confi"/>

How to get all the text inside an XML tag using XPath?

1.6k views Asked by At

I have this sort of tags in an XML:

<command name="config">config show</command>

For example, to read that one with xpath I do:

//command[@name='config']

but this outs two strings: "config" and "show". I would like to be able to get a unique string: "config show".

I tried /text() and /node() but didn't work. Any idea?

2

There are 2 answers

0
kjhughes On BEST ANSWER

For

<command name="check_config">config show</command>

this XPath,

//command[@name='check_config']

selects all command elements in the document with a @name attribute value equal to 'config'; in this case, only the element shown above:

<command name="check_config">config show</command>

Should this element be evaluated in the context that automatically converts to a string, or should you force such a conversion via the string() function,

string(//command[@name='check_config'])

it will be converted to the string value of that element,

config show

which is a single string, not two.

0
Natiya On

In my case, what it worked was:

//command[@name='check_config']/text()

I think I was doing something wrong in the syntax when I tried the function text() before! :)