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

Here should go questions about transforming XML with XSLT and FOP.
amitsaini54
Posts: 2
Joined: Fri Jan 18, 2008 12:15 pm
Location: Bangalore

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

Post 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
Dan
Posts: 501
Joined: Mon Feb 03, 2003 10:56 am

Post 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
amitsaini54
Posts: 2
Joined: Fri Jan 18, 2008 12:15 pm
Location: Bangalore

Post by amitsaini54 »

Thanks... :P
Post Reply