Avatar of kalyangkm
kalyangkm
Flag for United States of America asked on

Filter XML records based on if a segment exists in that XML record or not.

Hi Folks,

I have a simple requirement to filter out XML records based on a segment, the following example has 3 EmpJob records and I need to filter out records based on the Segment <Item>. so if any Record has the <Item> segment only that record needs to be in the output.


Input XML:


<?xml version="1.0" encoding="UTF-8"?>
<EmpJob>
    <EmpJob>         <lastModifiedDateTime>2021-07-26T16:16:53.000</lastModifiedDateTime>         <businessUnitNav>             <FOBusinessUnit>                 <name_defaultValue>XXXX Group</name_defaultValue>                 <externalCode>100001</externalCode>                 <startDate>1900-01-01T00:00:00.000</startDate>             </FOBusinessUnit>         </businessUnitNav>         <createdDateTime>2021-07-26T16:16:53.000</createdDateTime>         <employeeClassNav>             <PicklistOption>                 <picklistLabels>                     <PicklistLabel>                         <label>Staff</label>                     </PicklistLabel>                 </picklistLabels>             </PicklistOption>         </employeeClassNav>         <employmentNav>             <EmpEmployment>                 <originalStartDate>2021-08-16T00:00:00.000</originalStartDate>                 <lastModifiedDateTime>2021-07-26T16:16:53.000</lastModifiedDateTime>                 <personIdExternal>5000158</personIdExternal>                 <startDate>2021-08-16T00:00:00.000</startDate>                 <item>                     <TXCMP>fg</TXCMP>                     <LTEXT>CLambshank</LTEXT>                     <STRAS>Bclemson</STRAS>                     <ORT01>NEW YORK</ORT01>                     <STATE>NY</STATE>                     <ZIPCD>10036</ZIPCD>                     <EIDNO>13-3696170</EIDNO>                     <PERNR>00349909</PERNR>                 </item>             </EmpEmployment>         </employmentNav>         <startDate>2021-08-16T00:00:00.000</startDate>     </EmpJob>     <EmpJob>         <lastModifiedDateTime>2021-07-26T16:16:53.000</lastModifiedDateTime>         <businessUnitNav>             <FOBusinessUnit>                 <name_defaultValue>XXXX Group</name_defaultValue>                 <externalCode>100001</externalCode>                 <startDate>1900-01-01T00:00:00.000</startDate>             </FOBusinessUnit>         </businessUnitNav>         <createdDateTime>2021-07-26T16:16:53.000</createdDateTime>         <employeeClassNav>             <PicklistOption>                 <picklistLabels>                     <PicklistLabel>                         <label>Staff</label>                     </PicklistLabel>                 </picklistLabels>             </PicklistOption>         </employeeClassNav>         <employmentNav>             <EmpEmployment>                 <originalStartDate>2021-08-16T00:00:00.000</originalStartDate>                 <lastModifiedDateTime>2021-07-26T16:16:53.000</lastModifiedDateTime>                 <personIdExternal>5000158</personIdExternal>                 <startDate>2021-08-16T00:00:00.000</startDate>             </EmpEmployment>         </employmentNav>         <startDate>2021-08-16T00:00:00.000</startDate>     </EmpJob>     <EmpJob>         <lastModifiedDateTime>2021-07-26T16:16:53.000</lastModifiedDateTime>         <businessUnitNav>             <FOBusinessUnit>                 <name_defaultValue>XXXX Group</name_defaultValue>                 <externalCode>100001</externalCode>                 <startDate>1900-01-01T00:00:00.000</startDate>             </FOBusinessUnit>         </businessUnitNav>         <createdDateTime>2021-07-26T16:16:53.000</createdDateTime>         <employeeClassNav>             <PicklistOption>                 <picklistLabels>                     <PicklistLabel>                         <label>Staff</label>                     </PicklistLabel>                 </picklistLabels>             </PicklistOption>         </employeeClassNav>         <employmentNav>             <EmpEmployment>                 <originalStartDate>2021-08-16T00:00:00.000</originalStartDate>                 <lastModifiedDateTime>2021-07-26T16:16:53.000</lastModifiedDateTime>                 <personIdExternal>5000158</personIdExternal>                 <startDate>2021-08-16T00:00:00.000</startDate>                 <item>                     <TXCMP>TGHH</TXCMP>                     <LTEXT>Borkesh</LTEXT>                     <STRAS>BINSON</STRAS>                     <ORT01>NEW YORK</ORT01>                     <STATE>NY</STATE>                     <ZIPCD>10036</ZIPCD>                     <EIDNO>13-3696170</EIDNO>                     <PERNR>00349909</PERNR>                 </item>             </EmpEmployment>         </employmentNav>         <startDate>2021-08-16T00:00:00.000</startDate>     </EmpJob> </EmpJob>

Open in new window


Desired out should output only 2 records of EMpjob as one of the 3 records in the above xml doesnt have <Item> segment 


Desired Output


<?xml version="1.0" encoding="UTF-8"?>
<EmpJob>
    <EmpJob>         <lastModifiedDateTime>2021-07-26T16:16:53.000</lastModifiedDateTime>         <businessUnitNav>             <FOBusinessUnit>                 <name_defaultValue>XXXX Group</name_defaultValue>                 <externalCode>100001</externalCode>                 <startDate>1900-01-01T00:00:00.000</startDate>             </FOBusinessUnit>         </businessUnitNav>         <createdDateTime>2021-07-26T16:16:53.000</createdDateTime>         <employeeClassNav>             <PicklistOption>                 <picklistLabels>                     <PicklistLabel>                         <label>Staff</label>                     </PicklistLabel>                 </picklistLabels>             </PicklistOption>         </employeeClassNav>         <employmentNav>             <EmpEmployment>                 <originalStartDate>2021-08-16T00:00:00.000</originalStartDate>                 <lastModifiedDateTime>2021-07-26T16:16:53.000</lastModifiedDateTime>                 <personIdExternal>5000158</personIdExternal>                 <startDate>2021-08-16T00:00:00.000</startDate>                 <item>                     <TXCMP>fg</TXCMP>                     <LTEXT>CLambshank</LTEXT>                     <STRAS>Bclemson</STRAS>                     <ORT01>NEW YORK</ORT01>                     <STATE>NY</STATE>                     <ZIPCD>10036</ZIPCD>                     <EIDNO>13-3696170</EIDNO>                     <PERNR>00349909</PERNR>                 </item>             </EmpEmployment>         </employmentNav>         <startDate>2021-08-16T00:00:00.000</startDate>     </EmpJob>     <EmpJob>         <lastModifiedDateTime>2021-07-26T16:16:53.000</lastModifiedDateTime>         <businessUnitNav>             <FOBusinessUnit>                 <name_defaultValue>XXXX Group</name_defaultValue>                 <externalCode>100001</externalCode>                 <startDate>1900-01-01T00:00:00.000</startDate>             </FOBusinessUnit>         </businessUnitNav>         <createdDateTime>2021-07-26T16:16:53.000</createdDateTime>         <employeeClassNav>             <PicklistOption>                 <picklistLabels>                     <PicklistLabel>                         <label>Staff</label>                     </PicklistLabel>                 </picklistLabels>             </PicklistOption>         </employeeClassNav>         <employmentNav>             <EmpEmployment>                 <originalStartDate>2021-08-16T00:00:00.000</originalStartDate>                 <lastModifiedDateTime>2021-07-26T16:16:53.000</lastModifiedDateTime>                 <personIdExternal>5000158</personIdExternal>                 <startDate>2021-08-16T00:00:00.000</startDate>                 <item>                     <TXCMP>TGHH</TXCMP>                     <LTEXT>Borkesh</LTEXT>                     <STRAS>BINSON</STRAS>                     <ORT01>NEW YORK</ORT01>                     <STATE>NY</STATE>                     <ZIPCD>10036</ZIPCD>                     <EIDNO>13-3696170</EIDNO>                     <PERNR>00349909</PERNR>                 </item>             </EmpEmployment>         </employmentNav>         <startDate>2021-08-16T00:00:00.000</startDate>     </EmpJob> </EmpJob>

Open in new window

XML* XSLT

Avatar of undefined
Last Comment
Gertone (Geert Bormans)

8/22/2022 - Mon
Gertone (Geert Bormans)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="EmpJob/EmpJob[not(employmentNav/EmpEmployment/item)]"></xsl:template>
    
    
</xsl:stylesheet>

Open in new window

Gertone (Geert Bormans)

This stylesheet has 2 templates
  • one template does the reconstruction of the source XML (this is called the identity template)
  • the second template does the filtering. Any EmpJob that does not have an item in the employmentNav/EmpEmployment is matched but not copied to the output
This is a very common pattern in XSLT development. Build a one on one reconstruction with and identity transformation. But "overrule" the reconstruction and some specific points by having a more detailed match in the template (which takes priority)
ASKER CERTIFIED SOLUTION
Gertone (Geert Bormans)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
kalyangkm

ASKER
precise as always
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Gertone (Geert Bormans)

welcome ;-)