BaD aNgEl
BaD aNgEl

Reputation: 75

Traverse XML using XPath

I have this following XML:

<Item type="Workflow Process">
    <id>72F2AEEE86644D16ADE4B8FCEF68638A</id>
    <keyed_name>PR-100001</keyed_name>
    <Relationships>
        <Item type="Workflow Process Activity">
            <id>818175D7A6204275B9EAC45517FFC0DE</id>
            <related_id>
                <Item type="Activity">
                    <id>E0B712EB12914FBD8B5AEDCBDB3F313F</id>
                    <keyed_name>Start</keyed_name>
                </Item>
            </related_id>
        </Item>
    </Relationships>
</Item>

Note: The above XML structure is repeated in my XML file with different values of <id> and <keyed_name>

I have <id> of <Item type="Activity">, I want to know the <id> of parent node <Item type="Workflow Process"> to which that Activitybelongs.

What will be the XPath to achieve it?

Upvotes: 0

Views: 920

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122414

If you already have a reference to the inner Activity and can evaluate an XPath with that as the context node then

ancestor::Item[@type = 'Workflow Process']/id

If you just have the id and you're starting from the top of the tree then as long as all the ids are globally unique within the document the simplest I can think of is

//Item[@type = 'Workflow Process'][.//id = 'id you want']/id

I can't be more specific without knowing the context - is this in XSLT or are you using a DOM library of some sort (which one?).

Upvotes: 1

Related Questions