Xpath 返回值列表 [英] Xpath to return list of values

查看:49
本文介绍了Xpath 返回值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XPATH /OMRequest/Record/Customer/PhoneList/Phone/text()PhoneList 中只有一个 Phone 时效果很好.但是,我想要一个返回 PhoneList 中的所有 Phone 的 XPath.

I have the following XPATH /OMRequest/Record/Customer/PhoneList/Phone/text() which works well when there is only one Phone in PhoneList. However, I would like an XPath that returns all Phone within the PhoneList.

示例 XML 如下:

<OMRequest>
   <Record>
        <Customer>
            <PhoneList>
                <Phone>000000000</Phone>
                <Phone>000000000</Phone>
            </PhoneList>
            <CustomerId>424792716</CustomerId>
            <AccountNumber>1</AccountNumber>
            <ECPDId/>
            <ContractId/>
        </Customer>
    </Record>
</OMRequest>

推荐答案

单独,/OMRequest/Record/Customer/PhoneList/Phone/text() 将已经选择多个这样的文本节点(如果它们存在)在文档中.

Alone, /OMRequest/Record/Customer/PhoneList/Phone/text() will already select multiple such text nodes if they exist in the document.

如果您只获得第一个,那么您可能会将节点集传递给另一个函数,该函数仅在节点集的第一个节点上运行(使用 XPath 1.0 术语和构造).

If you're only getting the first one, then you're likely passing the nodeset to another function, which is only operating on the first node of the nodeset (using XPath 1.0 terminology and constructs).

例如,在 XPath 1.0 中,

For example, in XPath 1.0,

string(/OMRequest/Record/Customer/PhoneList/Phone/text())

只取所选文本节点的第一个的字符串值:

only takes the string value of the first of the selected text nodes:

7702958254

其他函数以及来自托管语言的其他调用的行为也类似.

Other functions, and other calls from hosting languages, can behave similarly.

如果要对所有选定的文本节点进行操作,在 XPath 1.0 中,您必须在 XPath 之外的宿主语言中进行迭代.XPath 2.0 具有序列而不是节点集,并且具有用于在 XPath 本身内操作序列的附加选项.

If you want to operate on all selected text nodes, in XPath 1.0 you'll have to iterate in the hosting language outside of XPath. XPath 2.0 has sequences rather than nodesets and has additional options for operating on sequences within XPath itself.

这篇关于Xpath 返回值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆