开发者

How can I select sibling nodes in XML using XPath?

开发者 https://www.devze.com 2023-03-18 22:22 出处:网络
Assuming the following XML: <?xml version=\"1.0\" encoding=\"UTF-8\"?> <root> <info> <code>

Assuming the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <info>
        <code>
            ABC
        </code>
        <desc>
            A
        </desc>
    </info>
    <info>
        <code>
    开发者_运维百科        DEF
        </code>
        <desc>
            A
        </desc>
    </info>
    <info>
        <code>
            XYZ
        </code>
        <desc>
            B
        </desc>
    </info>
</root>

How can I select all of the code elements that have a desc value of 'A'?

I tried the following XPath, and it gave me nothing:

/root/info[desc='A']


Like @Jim Garrison said, you'll need to use normalize-space() on <desc> but you can pretty much keep the xpath that you had with the addition of /code (and normalize-space())

/root/info[normalize-space(desc)='A']/code


Try this:

/root/info/code[normalize-space(../desc/text())='A']

I.e. tell it which nodes you want (/root/info/code), filtered by the condition. You need the normalize-space() because there's whitespace on both sides of the values in the source document.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号