java - Removing empty nodes with empty/no attributes using XSLT -


i want to:

  • remove empty nodes no or empty attributes f.e. <node/>, <node attr1="" attr2=""/>, <node></node>
  • keep nodes, has @ least 1 non-empty attribute f.e. <node attr1="123" attr2="" attr3=""/> , remove empty attributes have <node attr1="123"/>

  • updated keep empty nodes, have @ least 1 non-empty child nodes, or have @ least 1 node attributes

example

before

<a>   <b>     <c attr="1"/>     <d/>   </b> </a> 

after

<a>   <b>     <c attr="1"/>   </b> </a> 

i have following xslt:

<xsl:template match="@*|node()">         <xsl:if test="normalize-space(.) != '' or ./@* != ''">             <xsl:copy>                 <xsl:copy-of select = "@*[.!='']"/>                 <xsl:apply-templates/>             </xsl:copy>         </xsl:if> </xsl:template> 

it works pretty good, if xml has @ least 1 non-empty node! example:

xml:

<ns1:form xmlns:ns1="http://aaa.com">   <ns1:test>     <a></a>     <b attr1=""/>     <c attr="123"/>     <d attr="">       <e attr="12">not empty node</e>     </d>   </ns1:test> </ns1:form> 

after:

<?xml version="1.0" encoding="utf-8"?><ns1:form xmlns:ns1="http://aaa.com">   <ns1:test>     <c attr="123"/>     <d>       <e attr="12">not empty node</e>     </d>   </ns1:test> </ns1:form> 

works fine, skipping non-empty node :

<ns1:form xmlns:ns1="http://aaa.com">   <ns1:test>     <a></a>     <b attr1=""/>     <c attr="123"/>     <d attr="">       <e attr="12"></e>     </d>   </ns1:test> </ns1:form> 

the output is:

<?xml version="1.0" encoding="utf-8"?> 

anyone has idea why works this? code :

    transformerfactory factory = transformerfactory.newinstance();     inputstream in = new fileinputstream(new file("transform.xslt"));      source xslt = new streamsource(in);     transformer transformer = factory.newtransformer(xslt);     source text = new streamsource(new file("input.xml"));      stringreader reader = new stringreader(xml);     source text = new streamsource(reader);     transformer.transform(text, new streamresult(writer)); 

cheers

update

after using xslt output is:

<?xml version="1.0" encoding="utf-8"?><ns1:form xmlns:ns1="http://aaa.com"> <ns1:test>   <c attr="123"/> <d>     <e attr="12">e</e> </d> </ns1:test> </ns1:form> 

i wonder why there empty spaces in output.xml?


generally speaking, empty node node,

1) istelf has no attributes or empty and

2) has no value f.e. <a></a>, <b/> ,

3) has no children carry data (has no children meet requirements 1 , 2) example explaining empty nodes:

<a>   <b attr="1">   </b>   <c>   <d attr="2">     <e> value </e>     <f></f>   </d> </a> 

a not empty because has child b attribute attr="1" (it's enough, has: x, xx, xxx)

b not empty because has non-empty attribute

c not empty because has d node attribute attr="2" (x) , has child e value (xxx)

d not empty becuase has non-empty attribute (xx)

e not empty because has value (xxx)

f empty

you should not make <xsl:apply-templates/> conditional - otherwise stylesheet stop @ first node not satisfy condition, , never children.


btw, if understand conditions correctly, simplify to:

<xsl:template match="*[string() or @*[string()]]">     <xsl:copy>         <xsl:copy-of select = "@*[string()]"/>         <xsl:apply-templates/>     </xsl:copy> </xsl:template> 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -