Using XSLT, how to remove the empty element, which does not have an attribute or data, in an XML document? -


we want remove empty tag commentline xml

<commentline/> 

input xml :

<?xml version="1.0" encoding="windows-1252"?> <salesorders xsd:nonamespaceschemalocation="sdoc.xsd" xmlns:xsd="http://www.w3.org/2001/xmlschema-instance">      <orders>        <orderheader>         <customerponumber>ab-54354</customerponumber>       </orderheader>        <orderdetails>          <commentline>         <comment>ensure saddle color coded</comment>           <orderlineid>or-1810127</orderlineid>         </commentline>         <commentline>         <comment>edi-001</comment>           <orderlineid>or-1810128</orderlineid>         </commentline>         <stockline>           <customerpoline>9999</customerpoline>           <stockcode>absh-smh-12oz-01</stockcode>          <stockdescription>smh abs balance shampoo 12oz</stockdescription>           <orderqty>1.0</orderqty>       </stockline>         <commentline>         <comment>this test purpose</comment>           <orderlineid>or-1810124</orderlineid>         </commentline>          <commentline>           <comment>edi-save</comment>          <orderlineid>or-1810125</orderlineid>         </commentline>        <commentline/>      </orderdetails>     </orders>   </salesorders> 

tried xml on it:

<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" encoding="windows-1252" indent="yes"/>  <xsl:template match="@xsi:nil[.='true']" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"/>  <xsl:template match="@*|node()">     <xsl:copy copy-namespaces="no">         <xsl:apply-templates select="@*|node()"/>     </xsl:copy> </xsl:template> </xsl:stylesheet> 

expected output

<?xml version="1.0" encoding="windows-1252"?>  -<salesorders xsd:nonamespaceschemalocation="sdoc.xsd" xmlns:xsd="http://www.w3.org/2001/xmlschema-instance">                -<orders>                -<orderheader>               <customerponumber>ab-54354</customerponumber>               </orderheader>                -<orderdetails>                -<commentline>  <comment>ensure saddle color coded</comment>               <orderlineid>or-1810127</orderlineid>                </commentline>               -<commentline>  <comment>edi-001</comment>               <orderlineid>or-1810128</orderlineid>               </commentline>               -<stockline>               <customerpoline>9999</customerpoline>               <stockcode>absh-smh-12oz-01</stockcode>               <stockdescription>smh abs balance shampoo 12oz</stockdescription>               <orderqty>1.0</orderqty> </stockline>                -<commentline>  <comment>this test purpose</comment>               <orderlineid>or-1810124</orderlineid>               </commentline>                -<commentline>               <comment>edi-save</comment>               <orderlineid>or-1810125</orderlineid>               </commentline>                </orderdetails>               </orders>               </salesorders> 

we have remove element in complete input xml.

any appreciated ! valuable time.

this solution remove empty elements without removing parent elements (i.e. if had <orderdetails><commentline/></orderdetails> you'd , empty orderdetails element in result set.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">   <xsl:output method="xml" encoding="windows-1252" indent="yes"/>    <xsl:template match="@*|node()[./node()]">     <xsl:copy>       <xsl:apply-templates select="@*|node()"/>     </xsl:copy>   </xsl:template>  </xsl:stylesheet> 

i.e. i'm telling system return nodes have child nodes (be element or text). <xsl:template match="@*|node()[./node()]">

demo: http://xsltransform.net/jyryyjs

per @filburt's comments, if wanted remove elements become empty once empty child elements removed, follow advise on these links:


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -