3

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']

2 Answers 2

5

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
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.