user6572294
user6572294

Reputation:

XML Specific Query

I'm currently learning XML and XPATH and I need help in constructing a specific XPATH Query.

Here's the code sample:

<?xml version="1.0" encoding="UTF-8"?>
<Backup_Jobs xmlns="http://www.w3.org">
<Jobs>
    <Job name="Daily_Backup" lastRunTime="2001-10-26+00:00">
        <Job_Items>
            <Item id="1">vm-Centos7</Item>
            <Item id="2">vm-sample</Item>
        </Job_Items>
    </Job>
    <Job name="replicate" lastRunTime="2004-08-01+00:00">
        <Job_Items>
            <Item id="3">VMWare Workstation</Item>
        </Job_Items>
    </Job>
</Jobs>
<Hosts>
    <Host name="vcenter.tech.local" id="1111">
        <S name="CLARiion" size="2XL"/>
        <Vm id="2122" name="vm-Centos7">
            <Drive id="1241" name="sample1" size="500"/>
        </Vm>
        <Vm id="2123" name="vm-sample">
            <Drive id="1123" name="sample2" size="500"/>
        </Vm>
        <Vm id="2124" name="VMWare Workstation">
            <Drive id="1124" name="sample3" size="950"/>
        </Vm>
    </Host>
</Hosts>
</Backup_Jobs>

Here's the task:

Suppose we know the name of a [Job]; This Job contains an [Item] which has a name of a virtual machine. We want to get the name of the [Host] which has a [Vm] element which attribute is the name of a virtual machine we knew earlier.

I'm still learning, so any form of advice/solution is hugely appreciated!

Upvotes: 2

Views: 33

Answers (1)

splash58
splash58

Reputation: 26143

Replace Daily_Backup with the value for which you want to execute query

//Host[Vm[@name=//Job[@name="Daily_Backup"]/Job_Items/Item]]/@name

Upvotes: 1

Related Questions