user6185827
user6185827

Reputation: 41

Select duplicate tag id in XPath

How do I get the duplicate tag id using Xpath. For eg:

<Employees>
    <Employee id="1">
        <age>29</age>
        <name>Pankaj</name>
        <gender>Male
            <sexuality>Heterosexual</sexuality>
        </gender>
        <role>Java Developer</role>
    </Employee>
    <Employee id="2">
        <age>35</age>
        <name>Lisa
            <lastname>Monyet</lastname>
        </name>
        <gender>Female</gender>
        <role>CEO</role>
    </Employee>
    <Employee id="1">
        <age>40</age>
        <name>Tom</name>
        <gender>Male</gender>
        <role>Manager</role>
    </Employee>
</Employees>

Another question is,

Does this XPath query : //Employee/* able to return the tag id alongside the rest of the nodes value. If not, how do I improve this?

Upvotes: 0

Views: 68

Answers (1)

LMC
LMC

Reputation: 12777

Including id in the query just works

xmllint -xpath '//Employee[@id="1"]' test.xml 

Result:

    <Employee id="1">
        <age>29</age>
        <name>Pankaj</name>
        <gender>Male
            <sexuality>Heterosexual</sexuality>
        </gender>
        <role>Java Developer</role>
    </Employee><Employee id="1">
        <age>40</age>
        <name>Tom</name>
        <gender>Male</gender>
        <role>Manager</role>
    </Employee>

Upvotes: 1

Related Questions