xml 如何使用 XPath 返回数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5092521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return array with XPath?
提问by Spredzy
I was wondering if XPath could return an array of value.
我想知道 XPath 是否可以返回一个值数组。
I have someting like this :
我有这样的事情:
<Cities>
<City>Paris</City>
<City>Lyon</City>
<City>Marseille</City>
</Cities>
And I'd like to get an array of this form ['Paris', 'Lyon', 'Marseille'].
我想得到一个这种形式的数组 ['Paris', 'Lyon', 'Marseille']。
I am using the xpath UNIX utility.
我正在使用 xpath UNIX 实用程序。
Any idea ?
任何的想法 ?
采纳答案by Paused until further notice.
In addition to post-processing the output of:
除了后处理输出:
xpath -q -e '//City/text()' inputfile
which is:
即:
Paris
Lyon
Marseille
in any number of ways, including:
以多种方式,包括:
xpath -q -e '//City/text()' inputfile |
awk 'BEGIN {sq = "7"; OFS = sq "," sq}
{a[] = 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. 由于它是一个简单的 Perl 脚本,我对其进行了修改,以便您可以更好地控制输出的格式。使用我的版本,您可以:
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.
你可以在这里下载我的版本。
回答by Dimitre Novatchev
This XPath expression:
这个 XPath 表达式:
/*/*/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
计算此 XPath 表达式的结果是字符串
"Lyon"
回答by aharon
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.Valueto get the names.
我知道在 .Net 中你可以做到xmlDocument。xDoc.SelectNodes("//City");它为您提供了一个节点列表,您可以为每个节点NODE.FirstChild.Value获取名称。

