Page 1 of 1

canyou solve this prob, i wt to remove specific type element

Posted: Fri Jan 18, 2008 12:27 pm
by amitsaini54
Hi all
Any body is there who can help me….
I have xml file…..


<Project>
<Body>
<TXT><TXT>
<TXT>Hello TXT <TXT>
<Model><Model>
<Model>hello model<Model>
<Body>
</Project>
In this xml file two TXT element one is empty & other has value
I want to remove the empty elpement…

want the output like this


<Project>
<Body>
(don’t want TXT element)
<TXT>Hello TXT <TXT>
<Model><Model>
<Model>hello model<Model>
<Body>
</Project>

I wrote one xslt file
Like this
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
<xsl:if test=". != '' ">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>






By this xslt , I got the output like this

<Project>
<Body>
<TXT>Hello TXT <TXT>
<Model>hello model<Model>
<Body>
</Project>

This xslt file remove both empty element “<TXT>”,”<Model>”
But I want to remove only one <TXT>
Help me….please

Posted: Fri Jan 18, 2008 1:12 pm
by Dan
You can inhibit the output for the empty TXT element by writing an empty template maching it. The rest of the nodes are copied by the less prioritary second template.

Code: Select all


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="TXT[count(node()) = 0]"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
A good XSL tutorial: http://www.zvon.org/xxl/XSLTutorial/Boo ... index.html

Posted: Fri Jan 18, 2008 2:16 pm
by amitsaini54
Thanks... :P