Xpath substring-before 只获取一个元素 [英] Xpath substring-before gets only one element
问题描述
<row transactionDateTime="2012-06-04 02:42:44" transactionID="2519826732" quantity="5000" typeName="Caldari Navy Mjolnir Rocket" typeID="27321" price="988.72" clientID="92026367" clientName="N Clover" stationID="60003760" stationName="Jita IV - Moon 4 - Caldari Navy Assembly Plant" transactionType="buy" transactionFor="personal" journalTransactionID="5956192510"/>
你好
以上是我试图从中提取数据的 XML 文件示例.我要提取的数据是;
Above is a sample of the XML file I am trying to extract data from. The piece of data I want to extract is;
transactionDateTime="2012-06-04"
现在 - 我在使用以下 Xpath 查询时没有问题;
Now - I am having no problems getting this with the following Xpath query;
substring-before(//row[@transactionfor='personal']/attribute::transactiondatetime, ' ')
这正是我想要的——2012-06-04",但它只给我一个元素!如果我删除子字符串-before 它将获得所有元素,但包括时间.
This gets me exactly what I want - "2012-06-04", but it only gets me one element! If I drop the substring-before it will get me all elements, but include the time.
任何帮助都会很棒!
推荐答案
substring-before(//row[@transactionfor='personal']/attribute::transactiondatetime, ' ')
在 XPath 1.0 中,如果节点集作为参数传递,其中需要单个值(字符串),则节点集中的第一个节点将用作参数.规则是应用函数 string()
到传递的节点集,根据定义 string($nodeset)
与 string($node-set[1])
相同.
In XPath 1.0 if a node-set is passed as argument where a single value (string) is expected, the first node in the node-set is used as the argument. The rule is to apply the function string()
to the passed node-set and by definition string($nodeset)
is the same as string($node-set[1])
.
在 XPath 2.0 中,上述表达式会导致错误.
In XPath 2.0 the above expression should cause an error.
当节点数大于 1 时,不可能使用单个 XPath 表达式生成应用于所有选定节点的函数的结果.
因此,每个选定的节点都必须在第二步中进行处理(可能在托管 XPath 的编程语言中指定的循环中).
Therefore, each selected node must be processed in a second step (probably in a loop specified in the programming language that is hosting XPath).
在 XPath 2.0 中,可以使用函数调用作为表达式的最后一个定位步骤.
因此,可以使用此 XPath 2.0 表达式生成一系列 所有 想要的字符串:
Thus it is possible to produce a sequence of all wanted strings with this XPath 2.0 expression:
//row[@transactionfor='personal']
/attribute::transactiondatetime
/substring-before(., ' ')
这篇关于Xpath substring-before 只获取一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!