How to return array with XPath? How to return array with XPath? xml xml

How to return array with XPath?


In addition to post-processing the output of:

xpath -q -e '//City/text()' inputfile

which is:

ParisLyonMarseille

in any number of ways, including:

xpath -q -e '//City/text()' inputfile |    awk 'BEGIN {sq = "\047"; OFS = sq "," sq}         {a[$1] = NR}         END {             printf "[" sq;              for (i in a) {                 printf "%s%s", d, i; d = OFS};             print sq "]"         }'

which gives the output:

['Lyon','Marseille','Paris']

you can use my modified version of xpath. Since it's a simple Perl script, I modified it so you have more control over the formatting of the output. With my version, you can do:

xpath -q -b "[" -p "'" -i "," -s "'" -a "]"$'\n' -e '//City/text()'

to get the desired output. Here is the usage message:

Usage:xpath [options] -e query [-e query...] [filename...]        If no filenames are given, supply XML on STDIN.        You must provide at least one query. Each supplementary        query is done in order, the previous query giving the        context of the next one.        Options:        -q quiet        Only output the resulting PATH        -b before       use before instead of nothing.        -p prefix       use prefix instead of nothing.        -s suffix       use suffix instead of linefeed.        -i infix        use infix instead of nothing.        -a after        use after instead of nothing.

You can download my version here.


This XPath expression:

/*/*/text() 

selects exactly the three wanted text nodes.

You can use it with an array-like notation:

/*/*/text()[1]

selects the text-node

"Paris"

,

/*/*/text()[2]

selects the text-node

"Lyon"

,

/*/*/text()[3]

selects the text-node

"Marseille"

If you want just the string value of any of these text nodes, use (for example):

string(/*/*/text()[2])

The result of evaluating this XPath expression is the string

"Lyon"


i know that in .Net you can do it in xmlDocument. xDoc.SelectNodes("//City"); it gives you a list of nodes and for each node you can do NODE.FirstChild.Value to get the names.