Search code examples
xpathmuleanypoint-studio

Select node with same attribute name and but different values


I have the below xml document

<?xml version="1.0" encoding="UTF-8"?>
<xf:Report_Data xmlns:xf="urn:com.xforce.report/Job_History">
   <xf:Report_Entry>
      <xf:EmployeeID>11111</xf:EmployeeID>
      <xf:Name xf:Descriptor="E Botha">
         <xf:ID xf:type="XID">1111111111111</xf:ID>
         <xf:ID xf:type="Employee_ID">22222</xf:ID>
      </xf:Name>
      <xf:RoleAssignedTo xf:Descriptor="Lena Johnson-Bey (51961)">
         <xf:ID xf:type="WID">222222222222</xf:ID>
         <xf:ID xf:type="Employee_ID">222222</xf:ID>
      </xf:RoleAssignedTo>
      <xf:RoleAssignedTo xf:Descriptor="Allison Sisk (21849)">
         <xf:ID xf:type="XID">333333333333</xf:ID>
         <xf:ID xf:type="Employee_ID">33333</xf:ID>
      </xf:RoleAssignedTo>
   </xf:Report_Entry>
</xf:Report_Data>

I need to extract the Employee_ID under the xf:RoleAssignedTo so I should get back 22222 and 33333. I tried the below xpath and I am only getting the first 22222:

/xf:Report_Data/xf:Report_Entry/xf:RoleAssignedTo/xf:ID[@xf:type='Employee_ID']

My goal is get back both 22222 and 33333. In some cases it could be more. I would greatly appreciate any help. I need to use xpath without resorting to xslt.


Solution

  • You can use the following way to extract the value of all Employee_ID using XPATH3:-

     <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" />
    
      <flow name="splitxml2Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/split" doc:name="HTTP"/>
        <set-variable variableName="originalPayload" value="#[message.payloadAs(java.lang.String)]" doc:name="Variable"/>
        <splitter expression="#[xpath3('//*:Report_Data/*:Report_Entry/*:RoleAssignedTo', message.payload, 'NODESET')]" doc:name="Splitter"/>
        <logger level="INFO" message="Employee_ID:- #[xpath3('*:ID[2]')]" doc:name="Logger"/>
        <collection-aggregator failOnTimeout="true" doc:name="Collection Aggregator"/> 
        <set-payload doc:name="Set Payload" value="Done"/>  
      </flow> 
    

    You will get all the value in logger as below:-

    enter image description here

    It will extract as many Employee_ID available in the XML