简体   繁体   English

通过XPath从嵌套的子值中获取XML元素

[英]Getting XML element from nested child values via XPath

I have an XML file that looks something like this: 我有一个看起来像这样的XML文件:

<PACKAGES>
    <PACKAGE>
        <SHORT-NAME>Element1</SHORT-NAME>
        <PACKAGES>
            <PACKAGE>
                <SHORT-NAME>Element2</SHORT-NAME>
                <ELEMENTS>
                    <MODULE>
                        <SHORT-NAME>Element3</SHORT-NAME>
                        <DESC>
                        </DESC>
                        <CATEGORY>Item</CATEGORY>
                    </MODULE>
                </ELEMENTS>
            </PACKAGE>
        </PACKAGES>
    </PACKAGE>
</PACKAGES>

I would like the ability to build an XPath query to the MODULE element with the SHORT-NAME of "Element3", based on the SHORT-NAMES of "Element3"'s parents...so something like this: 我希望能够基于“ Element3”的父代的SHORT-NAMES对具有“ Element3”的SHORT-NAME的MODULE元素构建XPath查询,如下所示:

//SHORT-NAME='Element1'.//SHORT-NAME='Element2'.//SHORT-NAME='Element3'

I've tried the above query, but it doesn't seem to work, not a valid query. 我已经尝试了上面的查询,但是它似乎不起作用,不是有效的查询。 I've also tried this: 我也尝试过这个:

//*[text()='Element1']//*[text()='Element2'] etc...

but again, this doesn't seem to be a valid query. 但同样,这似乎不是有效的查询。

I'd like this to be scale-able such that the specific query could be any path, but the path is always based on the text value of SHORT-NAME. 我希望它是可伸缩的,以便特定的查询可以是任何路径,但是该路径始终基于SHORT-NAME的文本值。

So something like: 所以像这样:

/Element1/SubElement2/SubSubElement3/SubSubSubElement4 

could also be queried for. 也可以查询。

An important note: For the above query, I ONLY want Element3 IF it is a child of Element2, who is a child of Element1. 重要说明:对于上面的查询,如果它是Element2的子代,而Element2是Element1的子代,则只需要Element3。 IF Element3 exists elsewhere in the document, I do NOT want that node. 如果Element3在文档中的其他位置存在,则我不希望该节点。

I'm hoping this is possible I'm just not building the right query, but I'm at a loss for how to even search for this topic to try to find the answer. 我希望这是可能的,我只是没有建立正确的查询,但是我对于如何搜索该主题以尝试找到答案一无所知。

EDIT: The answer provided below by Andersson almost 100% works for my use-case. 编辑:下面由安德森(Andersson)提供的答案几乎100%适用于我的用例。 The issue is highlighted below: 该问题突出显示如下:

If I still need to query /Element1/Element2/Element3 but my XML looks like below: 如果我仍然需要查询/ Element1 / Element2 / Element3,但是我的XML如下所示:

<PACKAGES>
    <PACKAGE>
        <SHORT-NAME>Element1</SHORT-NAME>
        <PACKAGES>
            <PACKAGE>
                <SHORT-NAME>Element2</SHORT-NAME>
                <ELEMENTS>
                    <PACKAGE>
                    <SHORT-NAME>RandomElement</SHORT-NAME>
                        <MODULE>
                            <SHORT-NAME>Element3</SHORT-NAME>
                            <DESC>
                            </DESC>
                            <CATEGORY>Item</CATEGORY>
                        </MODULE>
                    </PACKAGE>
                </ELEMENTS>
            </PACKAGE>
        </PACKAGES>
    </PACKAGE>
</PACKAGES>

Element3 is still returned, however Element3's parent is not Element2. 仍然返回Element3,但是Element3的父级不是Element2。 This makes thing more complicated and I suspect I will need to create a function that systematically goes through a search for the next child SHORT-NAME and to check if the for query is satisfied. 这使事情变得更加复杂,我怀疑我将需要创建一个系统地搜索下一个子SHORT-NAME的函数,并检查是否满足for查询。 Unless the XPath query could be modified in such a way to satisfy the above use-case. 除非可以以满足上述用例的方式修改XPath查询。

This XPath, 这个XPath

//*[SHORT-NAME='Element3']

will select all elements, regardless of their names, which have a SHORT-NAME child with a string value of Element3 . 将会选择所有具有其SHORT-NAME子元素(其字符串值为Element3元素(无论其名称如何)。

If you wish to specify the target element's heritage, extend the pattern upward as many levels as necessary: 如果要指定目标元素的传统,请根据需要向上扩展图案的层次:

//*[SHORT-NAME='Element1']//*[SHORT-NAME='Element2']//*[SHORT-NAME='Element3']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM