on 2022 Nov 21 12:52 PM
<?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>
This is the XML input:
<?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>
This is the present output, I am getting using the above script when I remove Samuel and Manager in the input xml:
<?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
Request clarification before answering.
Hi Subhadeep,
The following should work. I already posted once and had to delete it because the original one only worked for Employee nodes that had Name/Designation nodes without text, but this version works when Employee contains no nodes either.
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="node()[child::*[not(text())]]|node()[not(child::*) and contains(text(), ' ')]"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Regards,
Ryan Crosby
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI Ryan
How can we remove child node in XML only if its sub-child is empty?
for eg , Input
<Record>
<Employee>
<Name>Robin</Name>
<Designation></Designation>
</Employee>
<Employee>
<Name></Name>
<Designation></Designation>
</Employee>
</Record>
Output:
<Record>
<Employee>
<Name>Robin</Name>
<Designation></Designation>
</Employee>
</Record>
Hello Ryan,
I need your help in slight modification of the XSLT.
It should work with empty values and not blank values in the nodes:
Currently it is working with
<Name> </Name> ( A blank character within the tags <Name>)
Our functionality is to make it work with empty values:
<Name></Name> ( No blank spaces)
Can you append the code for this requirement, please.
regards,
Subhadeep
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can try the extra following match to see if it works with spaces.
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="node()[child::*[not(text())]]|node()[child::*[contains(text(), ' ')]]|node()[not(child::*) and contains(text(), ' ')]"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Many thanks Ryan, it worked!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Cheers!
User | Count |
---|---|
74 | |
20 | |
9 | |
8 | |
7 | |
5 | |
5 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.