0
votes

I try to delete empty nodes, but not all of them, for example I have tags:

1.<abc></abc> - it should be delete
2.<cde/> - it should not be delete.
3.<fgh/> - it SHOULD be delete.

So, i would like to delete empty nodes in point 2 (explicit name "CDE")

So in other words: when tag like 'cde' and tag is null then delete. : D

Is it possible?

I found the code which delete all the empty nodes:

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="*[descendant::text() or descendant-or-self::*/@*[string()]]">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*[string()]">
    <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

EDIT:

XML look like:

<system/>
            <smth></smth>
            <smth2>?</smth2>
            <smth3>?</smth3>
            <smth4></smth4>
            <smth4></smth4>
            <smth5/>

XML can contain a lot of tags like - SO i have remove them dynamically. Some of tags which looks like I WANT to remove. But in this XML should stay.

So XML output should looks:

 <system/>
                <smth2>?</smth2>
                <smth3>?</smth3>




I found the solution: http://xsltransform.net/nc4NzRx/3. : )
1
Your requirements seem to be contradicting. - michael.hor257k
Why? I just DONT want to delete explicit tag. In this case name: CDE. - bazylwro
But your questions says "2.<cde/> - it should not be delete." and then later on "i would like to delete empty nodes in point 2 (explicit name "CDE")" so the first cited sentence contradicts the second cited one. - Martin Honnen
Oh right! Of course.My mistake. I DONT want to delete empty nodes in point 2. If my xml looks like: <system/> <correlationId></correlationId> <enforceExecution>?</enforceExecution> Output should look like: <system/> <enforceExecution>?</enforceExecution> - bazylwro
I am afraid this is still not clear. You can either (a) delete all empty nodes except explicitly-named ones, or (b) delete only explicitly-named empty nodes. -- P.S. Please don't post code in comments; edit your question instead. - michael.hor257k

1 Answers

0
votes

If you want to copy everything but delete empty cde elements then use a stylesheet with the following two templates

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

<xsl:template match="cde[not(node())]"/>