Neil Middleton
Neil Middleton

Reputation: 22240

Complex XPath searches using attributes?

I have some XML which I'm trying to search:

<debts>
        <section id="24" description="YYYYY">
        <section id="28" description="xxx">
            <section id="31" description="xxx">
                <account accountNumber="2312323" creditorId="1" amount="1200" proposedAmount="1000" percentage="11" owner="2">
                    <foo/>
                </account>
            </section>
            <section id="32" description="xxx"/>
            <section id="33" description="xxx"/>
        </section>
        <section id="29" description="xxx">
            <section id="34" description="xxx"/>
        </section>
        <section id="30" description="xxx">
            <section id="37" description="xxx"/>
            <section id="38" description="xxx"/>
            <section id="39" description="xxx"/>
        </section>
    </section>
</debts>

Essentially, what I am trying to do, is find all of the account nodes that sit underneath the section YYYYY (which is quite likely not the only node at that level). How can I do this with XPath (specifically I'm using Hpricot on Rails)

Upvotes: 0

Views: 605

Answers (1)

Josh
Josh

Reputation: 6322

try this:

/debts//section[@description='YYYYY']//account

http://www.bit-101.com/xpath/ has a nice tool to test xpath queries.

Upvotes: 3

Related Questions