How can I select sibling nodes in XML using XPath?
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.
精彩评论