如何使用变量节点名称获取节点值? [英] How to get node value using variable node name?

查看:37
本文介绍了如何使用变量节点名称获取节点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 文档,例如:

I have an XML document like:

<data>
    <item type="apple">
        <misc>something</misc>
        <appleValue>23</appleValue>
        <misc2>something else</misc2>
    </item>
    <item type="banana">
        <bananaValue>47</bananaValue>
        <random>something</random>
    </item>
</data>

我可以使用 doc("data.xml")/data/item 获取项目,但我需要从以 Value 结尾的元素中获取文本.所以我想得到23"和47",但我不一定知道元素名称,这意味着我真正知道的是有些元素以Value结尾,我不知道我不知道它是 appleValuebananaValue 等,但我可以查看 type 属性并构建一个字符串.

I can get the items with doc("data.xml")/data/item but I need to get the text from the elements that end with Value. So I'd like to get "23" and "47", but I don't necessarily know the element names, meaning all I really know is there are elements that end in Value, I don't know if it's appleValue, bananaValue, etc. except that I could look at the type attribute and buildup a string.

let $type := (doc("data.xml")/data/item)[1]/@type
doc("data.xml")/data/item/$typeValue

...最后一行是我想要得到的,显然这是不正确的,但我需要根据变量(存储在诸如 $type<之类的变量中)查找名称已知的元素/code>) 和值".

...That last line is what I'm trying to get at, clearly that's not correct but I need to find elements whose name is known based on a variable (stored in a variable such as $type) and "Value".

有什么想法吗?我意识到这个可变元素命名很奇怪/奇怪/坏...但它就是这样,我必须处理它.

Any ideas? I realize this variable element naming is strange/odd/bad...but that's the way it is and I have to deal with it.

推荐答案

我会避免使用 name() 函数,而是使用 node-name()local-name().这样做的原因是 name() 可以根据源代码中使用的名称空间前缀(以及是否使用)为您提供不同的答案.例如,以下三个元素具有完全相同的名称(QName):

I would avoid using the name() function in favor of either node-name() or local-name(). The reason for this is that name() can give you different answers depending on what (and whether) namespace prefixes are used in the source. For example, the following three elements have the same exact name (QName):

<appleValue xmlns="http://example.com"/>
<x:appleValue xmlns:x="http://example.com"/>
<y:appleValue xmlns:y="http://example.com"/>

但是,name() 函数将为您提供不同的答案(appleValuex:appleValue>y:appleValue,分别).因此,您最好通过使用 local-name()(对于上述所有三种情况返回字符串 appleValue)或明确指定命名空间来忽略命名空间(即使它是空的,如 Oliver 所示),使用 node-name() (返回正确的 QName 值,而不是字符串).在这种情况下,由于您没有使用名称空间(并且即使您稍后添加,代码仍然可以工作),我会稍微赞成使用 local-name() 作为如下:

However, the name() function will give you a different answer for each one (appleValue, x:appleValue, and y:appleValue, respectively). So you're better off either ignoring the namespace by using local-name() (which returns the string appleValue for all three of the above cases) or explicitly specifying the namespace (even if it's empty, as Oliver showed), using node-name() (which returns a proper QName value, rather than a string). In this case, since you're not using namespaces (and since even if you added one later, the code will still work), I'd be slightly in favor of using local-name() as follows:

doc("data.xml")/data/item/*['Value' eq substring-after(local-name(),../@type)]

要详细说明避免使用 name() 函数(和例外)的原因,请参阅 "名称函数的危险".

For elaboration on reasons to avoid the name() function (and exceptions), see "Perils of the name function".

这篇关于如何使用变量节点名称获取节点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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