1

I would like to filter <end_date/> element from my XML input using XSLT. I have tried different approaches but does not seem to work. Can you suggest me how it can be achieved

Sample Payload:

<?xml version="1.0" encoding="UTF-8"?>
<queryCompoundEmployeeResponse>
    <CompoundEmployee>
        <version_id>2005P0</version_id>
        <person>
            <person_id_external>484284</person_id_external>
            <employment_information>
                <employment_id>864</employment_id>
                <end_date/>
            </employment_information>
        </person>
        <person>
            <person_id_external>484285</person_id_external>
            <employment_information>
                <employment_id>865</employment_id>
                <end_date>2020-12-31</end_date>
            </employment_information>
        </person>
        <person>
            <person_id_external>484286</person_id_external>
            <employment_information>
                <employment_id>866</employment_id>
                <end_date>2021-02-01</end_date>
            </employment_information>
        </person>
    </CompoundEmployee>
</queryCompoundEmployeeResponse>

Expected output (element to be removed for person_id_external 484284):

<?xml version="1.0" encoding="UTF-8"?>
<queryCompoundEmployeeResponse>
    <CompoundEmployee>
        <version_id>2005P0</version_id>
        <person>
            <person_id_external>484284</person_id_external>
            <employment_information>
                <employment_id>864</employment_id><end_date/>
            </employment_information>
        </person>
        <person>
            <person_id_external>484285</person_id_external>
            <employment_information>
                <employment_id>865</employment_id>
                <end_date>2020-12-31</end_date>
            </employment_information>
        </person>
        <person>
            <person_id_external>484286</person_id_external>
            <employment_information>
                <employment_id>866</employment_id>
                <end_date>2021-02-01</end_date>
            </employment_information>
        </person>
    </CompoundEmployee>
</queryCompoundEmployeeResponse>

XSL I tried:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>
 
 <xsl:template match="//CompoundEmployee/person/employment_information/end_date[not(end_date)]"/>
 
 </xsl:stylesheet>
4
  • Do you want to remove any empty end_date element? Or that for a particular person? And show us one attempt and how it failed exactly. Commented Jul 1, 2020 at 9:04
  • Hi Martin, I want to remove any empty end_date element wherever it occurs in payload. Commented Jul 1, 2020 at 9:05
  • "I have tried different approaches but does not seem to work." For example? Commented Jul 1, 2020 at 9:06
  • I added an XSL which doesnt seem to work in my question. but that is my idea Commented Jul 1, 2020 at 9:10

1 Answer 1

1

The template to remove any empty end_date would be simply an empty <xsl:template match="end_date[not(node())]"/>.

Sign up to request clarification or add additional context in comments.

2 Comments

Tried like this with your suggestion and it worked.
<xsl:stylesheet version="2.0" xmlns:xsl="w3.org/1999/XSL/Transform" xmlns:xs="w3.org/2001/XMLSchema"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements=""/> <xsl:template match="node()|@"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <!-- remove all empty endDate in emp_info elements --> <xsl:template match="//CompoundEmployee/person/employment_information/end_date[not(node())]"/> </xsl:stylesheet>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.