根据元素值的继承关系获取XML节点 [英] Get XML node based on heritage of element values

查看:79
本文介绍了根据元素值的继承关系获取XML节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<XXX>
    <SHORT-NAME>ELEMENT1</SHORT-NAME>
    <YYY>
        <ZZZ>
            <SHORT-NAME>ELEMENT2</SHORT-NAME>
            <AAA>
                <BBB>
                    <SHORT-NAME>ELEMENT3</SHORT-NAME>
                    <CCC></CCC>
                    <DDD></DDD>
                    <EEE></EEE>
                    <FFF>
                        <SHORT-NAME>ELEMENT4</SHORT-NAME>
                    </FFF>
                    <GGG>
                        <SHORT-NAME>ELEMENT6</SHORT-NAME>
                    </GGG>
                </BBB>
                <HHH>
                    <SHORT-NAME>ELEMENT5</SHORT-NAME>
                </HHH>
            </AAA>
        </ZZZ>
    </YYY>
</XXX>

这个想法是,每个ELEMENT代表一个包装或容器,每个子元素代表一个子元素或它的父包装。这些软件包是根据其SHORT-NAME命名的。一个元素与其子包之间可以有任意数量的元素。该XML文件是提供给我的,因为布局定义明确,因此我无须更改。

The idea is that the ELEMENT(s) each represent a package or container and each subelement represents a subelement or package of it's parent. The packages are named based on their SHORT-NAME. There can be an arbitrary number of elements inbetween an element and it's child packages. This XML file is provided to me and I have no input in changing it as this layout is well defined.

其他解决上述XML中各种元素的方式是基于他们相对于其父母的位置,并以其简称命名。因此,例如,在上面的示例中, / ELEMENT1 / ELEMENT2 / ELEMENT3 / ELEMENT4 是解决< FFF> 元素基于它的SHORT-NAME。另一方面,诸如 / ELEMENT1 / ELEMENT2 / ELEMENT4 之类的方法不是解决ELEMENT4的有效方法,因为从技术上来说ELEMENT4是ELEMENT3的子级。

The way that "others" address various elements within the above XML is based on their location relative to it's parents and addressed by its SHORT-NAME. So for instance, in the above example /ELEMENT1/ELEMENT2/ELEMENT3/ELEMENT4 is a valid way to address the <FFF> element based on it's SHORT-NAME. On the flip side, something like /ELEMENT1/ELEMENT2/ELEMENT4 would NOT be a valid way to address ELEMENT4 because ELEMENT4 is technically a "child" of ELEMENT3.

所以我昨天发布了一个问题,关于如何使用XPath根据其简短名称检索某些元素,并且提出的解决方案是这样的:

So I had posted a question yesterday about how to use XPath to retrieve certain elements based on their SHORT-NAMEs and the solution that was proposed was like this:

//*[SHORT-NAME='ELEMENT1']//*[SHORT-NAME='ELEMENT2']//*[SHORT-NAME='ELEMENT3']//*[SHORT-NAME='ELEMENT4']

在上面的示例中可以正常工作,但是问题在于,如果我改用这样的XPath:

This would work in the above example, however the isse with this is that if instead I used an XPath like this:

//*[SHORT-NAME='ELEMENT1']//*[SHORT-NAME='ELEMENT2']//[SHORT-NAME='ELEMENT4']

它仍将检索ELEMENT4,实际上,如果有人不知道所需元素的确切路径/层次结构,则他们将无法检索该元素。这是关键点。

It would still retrieve ELEMENT4, and in reality if someone doesn't know the exact path/heiarchy of the element they need, they should not be able to retrieve it. This is a key point.

我希望可以通过XPath查询解决。

My hope is that this could be solved with a XPath query.

推荐答案

您可以尝试在下面使用:

You can try to use below:

//*[SHORT-NAME='ELEMENT1']//*[SHORT-NAME='ELEMENT2']//*[SHORT-NAME='ELEMENT3']//*[SHORT-NAME='ELEMENT4' and count(./ancestor-or-self::*[./SHORT-NAME])=4]

这个想法是手动指定路径中提到的节点数(在提供的示例中为 4 )。仅在路径中提到所有具有 SHORT-NAME 子代的祖先的情况下,表达式才会返回所需的节点,因此

The idea is to specify manually the number of nodes mentioned in your path (in provided example it is "4"). Expression will return you desired node only if all ancestors that have SHORT-NAME child were mentioned in path, so

//*[SHORT-NAME='ELEMENT1']//*[SHORT-NAME='ELEMENT2']//*[SHORT-NAME='ELEMENT4' and count(./ancestor-or-self::*[./SHORT-NAME])=3]

不应该返回您需要节点

这篇关于根据元素值的继承关系获取XML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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