Home > Back-end >  Filtering records based on Xpath expression
Filtering records based on Xpath expression

Time:05-19

I am trying to filter worker records based on the 2 digit country code (ISO_3166-1_Alpha-2_Code). I want to filter the records of workers working in country India(IN).

Below is the XML source code:

<wd:Get_Workers_Response xmlns:wd="urn:com.workday/bsvc" wd:version="v32.0">
    <wd:Worker>
        <wd:Employment_Data>
            <wd:Home_Country_Reference>
                <wd:ID wd:type="ISO_3166-1_Alpha-2_Code">AR</wd:ID>
                <wd:ID wd:type="ISO_3166-1_Alpha-3_Code">ARG</wd:ID>
            </wd:Home_Country_Reference>
        </wd:Employment_Data>
        <wd:Personal_Data>
            <wd:Name_Data>
                <wd:First_Name>Rohit</wd:First_Name>
                <wd:Last_Name>Singh</wd:Last_Name>
            </wd:Name_Data>
        </wd:Personal_Data>
    </wd:Worker>
    <wd:Worker>
        <wd:Employment_Data>
            <wd:Home_Country_Reference>
                <wd:ID wd:type="ISO_3166-1_Alpha-2_Code">IN</wd:ID>
                <wd:ID wd:type="ISO_3166-1_Alpha-3_Code">IND</wd:ID>
            </wd:Home_Country_Reference>
        </wd:Employment_Data>
        <wd:Personal_Data>
            <wd:Name_Data>
                <wd:First_Name>Shivam</wd:First_Name>
                <wd:Last_Name>Dubey</wd:Last_Name>
            </wd:Name_Data>
        </wd:Personal_Data>
    </wd:Worker>
</wd:Get_Workers_Response>

I have used the below Xpath expression:

//wd:Employment_Data[wd:Home_Country_Reference/wd:ID = 'IN']

This gives me the below output:

Element='<wd:Employment_Data xmlns:wd="urn:com.workday/bsvc">
            <wd:Home_Country_Reference>
                  <wd:ID wd:type="ISO_3166-1_Alpha-2_Code">IN</wd:ID>
                  <wd:ID wd:type="ISO_3166-1_Alpha-3_Code">IND</wd:ID>
            </wd:Home_Country_Reference>
        </wd:Employment_Data>'

The output of this Xpath expression doesn't contain a Personal_Data node. Is it possible to filter data based on 'IN' country and have both nodes (Employment_Data and Personal_Data) in the output by using Xpath expressions? Or do we have to use XSLT for this transformation?

Expected Output:

<wd:Get_Workers_Response xmlns:wd="urn:com.workday/bsvc" wd:version="v32.0">
    <wd:Employment_Data>
        <wd:Home_Country_Reference>
            <wd:ID wd:type="ISO_3166-1_Alpha-2_Code">IN</wd:ID>
            <wd:ID wd:type="ISO_3166-1_Alpha-3_Code">IND</wd:ID>
        </wd:Home_Country_Reference>
    </wd:Employment_Data>
    <wd:Personal_Data>
        <wd:Name_Data>
            <wd:First_Name>Shivam</wd:First_Name>
            <wd:Last_Name>Dubey</wd:Last_Name>
        </wd:Name_Data>
    </wd:Personal_Data>
</wd:Worker>
</wd:Get_Workers_Response>

CodePudding user response:

You are only selecting the EmploymentData node. Try the following:

//wd:Worker[wd:Employment_Data/wd:Home_Country_Reference/wd:ID = 'IN']

  • Related