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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove those empty elements! 1

Status
Not open for further replies.

SiJP

Programmer
May 8, 2002
708
GB
In theory, I have this XML file (and be gentle, I'm not experienced with XML).

<root>
<data>
<date>
<start/>
<end></end>
</date>
<time>12:00</time>
</data>
</root>

As you can see, there are a fair few redundant elements. What I'd like to do is clean up this XML with an XSLT, by getting rid of any elements that have no attributes or values.

So the above xml would then be:
<root>
<data>
<time>12:00</time>
</data>
</root>

The <start/> element and <end></end> element would be ripped out. And because the <date>.. element would subsequently have no child nodes, this needs to go to.

If that make's sense, how do I do this?!

Thanks

------------------------
Hit any User to continue
 
p.s.

I found this code on the web .. but it seems to remove attributes of elements that do have values.

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

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

I do rather need these attributes left in!

Looking forward to some replies :)

------------------------
Hit any User to continue
 
How about this:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="@*|node()">
    <xsl:if test=". != ''">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Ta Jon, but this still strips out all the atrributes.

what was <date time="12:00:00">2003-01-01</date> becomes <date>2003-01-01</date>

Rather like the later code I pasted :/

------------------------
Hit any User to continue
 
No it doesn't. Not with MSXSL parser anyway. What parser are you using? This **SHOULD** work with any parser. I did notice a slight error though. If you have a empty node that has an attribute with a value, it will remove that node (and attribute). To solve that, use:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="@*|node()">
    <xsl:if test=". != '' or ./@* != ''">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 

Jon, your last post's code worked fine - thank you for lending your expertise.

I'm a little disconcerted that I dont understand any xslt, but it looks like I'm going to need to learn!

Cheers,

------------------------
Hit any User to continue
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top