xslt - XML Stylesheet to remove all empty child nodes, but keep parent level -
xslt - XML Stylesheet to remove all empty child nodes, but keep parent level -
i have xml, represented example:
<?xml version ="1.0"?> <whatever xmlns ="http://tempuri.org/whatever.xsd"> <glassestypes> <glassestype /> </glassestypes> <expressionofjoy> <fellover>y</fellover> </expressionofjoy> <flights> <flight> <bookings> <booking> <segments> <segment /> </segments> </booking> </bookings> </flight> </flights> <fruit> <apples> <redones> <redone /> </redones> <greenones> <greenone> <name>granny smith</name> <seedless /> </greenone> </greenones> </apples> <pears /> </fruit> </whatever> in [this previous question] asked how utilize xquery remove empty kid tags @ varying levels, leaving parent tag place holder. unfortunately, while able implement solution, have had add together considerably in order correctly format xml , slowing downwards lot.
the reply seems to stop mucking around xml in sql server , create stylesheet transform xml required. have acheived quite lot here , things looking good, have not found right way remove kid elements @ each level while still leaving parent in place. searches have found remove kid elements , parent well.
for instance, tried this:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:template match="node()"> <xsl:if test="normalize-space(string(.)) != ''"> <xsl:copy> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:if> </xsl:template> </xsl:stylesheet> but result leaves no empty nodes @ all. using [freeformatter] check results. here output (as can see, parents of empty nodes still there empty placeholders):
<?xml version ="1.0"?> <whatever xmlns ="http://tempuri.org/whatever.xsd"> <glassestypes /> <expressionofjoy> <fellover>y</fellover> </expressionofjoy> <flights /> <fruit> <apples> <redones /> <greenones> <greenone> <name>granny smith</name> <seedless /> </greenone> </greenones> </apples> <pears /> </fruit> </whatever> i have been looking @ identity templates , how utilize them here, , pages all over place, seem require levels referred name in order accomplish need bit more non-specific think, apply levels of big xml file. suggestions welcome!
edit
the logic behind keeping or removing element each element, if there no info in of kid elements, kid elements should removed.
for instance; interpret logic applied lowest level kid element way highest element contain empty kid elements. glassestype removed empty kid of glassestypes, glassestypes remains has no empty kid elements. likewise segment removed, leaves bookings empty kid element segments, should removed , on flights. seedless, has no empty kid elements, remains.
mark
indeed, need variation on identity transform template, such as:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="*[descendant::text()]"> <xsl:copy> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> edit: in response clarification:
... each element, if there no info in of kid elements, kid elements should removed.
ok, if set that, let's alter template to:
<xsl:template match="*"> <xsl:copy> <xsl:if test="descendant::text()"> <xsl:apply-templates select="node()"/> </xsl:if> </xsl:copy> </xsl:template> xml xslt
Comments
Post a Comment