Rank: Member
Joined: 1/19/2005 Posts: 6 Location: US
|
I've been using the following script to remove any elements without a value or contains only white-space from our XMLs.
Code: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:preserve-space elements="*"/> <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
<xsl:template match="*[not(normalize-space())]"/> </xsl:stylesheet>
This script will even remove empty elements with attributes. I however would like to keep those. So for example running the script on the following XML:
Code: <?xml version="1.0" encoding="UTF-8"?> <Root> <Parent1> <Child1 abc="xyz" >Me</Child1> </Parent1> <Parent2/> <Parent3> <Child3 abc="xyz"/> </Parent3> <Parent4> <Child4> </Child4> </Parent4> </Root>
Results in:
Code: <?xml version="1.0" encoding="UTF-8"?> <Root> <Parent1> <Child1 abc="xyz">Me</Child1> </Parent1> </Root>
But what I want is:
Code: <?xml version="1.0" encoding="UTF-8"?> <Root> <Parent1> <Child1 abc="xyz" >Me</Child1> </Parent1>
<Parent3> <Child3 abc="xyz"/> </Parent3> </Root>
I tried a couple of different modifications to the original script with no luck. Any help would be great. Many thanks in advance.
|
Rank: Advanced Member
Joined: 12/13/2005 Posts: 2,856 Location: Mauritius
|
Try this
<xsl:template match="*[not(normalize-space()) and not(.//@*)]"/>
|