on 2022 Apr 26 12:18 PM
Hi Community,
I guess someone have already an similar use case / requirement and can easily help. I have following payload in XML:
<customerlist>
<data>
<name1>Meier</name1>
<name2>Hans</name2>
</data>
<data>
<name1><name1>
<name2>Fischer</name2>
</data>
<data>
<name1>Müller<name1>
<name2></name2>
</data>
<data/>
</data/>
</customerlist>
My goal is to delete only the complete empty "data" elements of the list. Empty "name1" or "name2" should be stay untouched.
Thanks
Best regards
DL
Request clarification before answering.
Hi Dominik
You can solve this using the technique I described in this blog post: Thinking in XSLT: Filtering XML elements
In your specific case, the stylesheet would look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="data[not(*)]"/>
</xsl:stylesheet>
Regards,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi dlange_87
Yes, it's a valid stylesheet. Your input XML, however, is not valid, which is why your test fails. Here's a cleaned up version of your XML without the errors:
<?xml version="1.0" encoding="UTF-8"?>
<customerlist>
<data>
<name1>Meier</name1>
<name2>Hans</name2>
</data>
<data>
<name1/>
<name2>Fischer</name2>
</data>
<data>
<name1>Müller</name1>
<name2/>
</data>
<data/>
</customerlist>
Regards,
Morten
Hi dlange_87
If my answer solved or helped solve the problem, please keep in mind to accept/upvote.
Regards,
Morten
Hello Morten,
Hope you are doing fine. I used your XSLT script in removing the empty nodes in XML but I am unable to remove empty segments.
For example: This is the XML
<?xml version="1.0" encoding ="utf-8"?>
<Record>
<Employee>
<Name>Robin</Name>
<Designation>Engineer</Designation>
</Employee>
<Employee>
<Name>Samuel</Name>
<Designation>Manager</Designation>
</Employee>
</Record>
Now when I remove Samuel and Manager, I am getting the output as:
<?xml version="1.0" encoding ="utf-8"?>
<Record>
<Employee>
<Name>Robin</Name>
<Designation>Engineer</Designation>
</Employee>
<Employee>
</Employee>
</Record>
However , I want no segment if there are empty nodes. Below is the expected output:
<?xml version="1.0" encoding ="utf-8"?>
<Record>
<Employee>
<Name>Robin</Name>
<Designation>Engineer</Designation>
</Employee>
</Record>
Please help here if we can achieve this output.
regards,
Subhadeep
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
22 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.