Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Node with Empty Element

Status
Not open for further replies.

xslnew1

Technical User
May 28, 2007
10
DE
Hi @all,

may somebody can help me out.
I would like to remove a node which has empty element, something like this:

<start>
<node 1>
<NonEmpty> Some Text </NonEmpty>
<node 2>
<Empty>
</Empty>
</node 2>
</node 1>
</start>

into :
<start>
<node 1>
<NonEmpty> Some Text </NonEmpty>
</node 1>
</start>

many thanks in advance,

cheers

 
[0] Why can't you open it with notepad or any text file editor and highlight-delete? This is obviously not what you've meant for asking. So why can't you be more professionally precise?
[0.1] I can only guess you want to do it with xsl. Why I guess so, I haven't a clue.

[1] >I would like to remove a node which has empty element, something like this:[...]
[1.1] This is again not very precise. At the face of it, "node 1" contains empty element(s) as well as its descendant...
[1.2] You haven't consider the effect of attributes. That I will leave it to your future alertness.
[1.3] Figuratively "node 1", "node 2" etc are not good illustration. They are _not_ valid tag name. (Do you know why?)

[2] To answer your question without regard to [1.2], you can add a template with sufficiently high priority (9 below is arbitrary) to any of your existing xsl document.
[tt] [blue]<xsl:template match="node()[normalize-space(.)='']" priority="9" />[/blue][/tt]
 
Hi,

sorry for that !

I bit more precise example ;-)
<part>
<test>
Some Text
</test>
<section>
Again some text
</section>
<Empty Tag>
</Empty Tag>
</part>

This my xsl file:


<xsl:template
match="node()[ descendant-or-self::*[@* != ''] or descendant-or-self::*[string-length(normalize-space(.)) &gt; 0]]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
It works fine and deletes the empty tags, however I would also like to remove Element section aswell.

thx in advance
 
Hi again,

I am sorry but I whish to delete the whole node part, if there is an empty tag within that node:

<part>
<section>
some text
</section>
<Empty Tag>
</Empty Tag>
</part>
rgds
 
>I whish to delete the whole node part, if there is an empty tag within that node:[...]
This is again ambiguous! Make up your mind, and try and add this template then.
[tt] <xsl:template match="node()/node()[normalize-space()='']" priority="9" />[/tt]
ps: If you still haven't learned, [tt]<Empty Tag>[/tt] is again a wrong name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top