Reputation: 177
Given this portion of an xml, i need to remove any tag that has xsi:nil="true"
I'm currently using this XSL to remove the blank nodes as well so need to retain that as well. Attached below. Thanks.
XML:
<ns1:EmployeeOfferAndCoverageGrp xmlns:ns1="http://tempuri.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:AnnualOfferOfCoverageCd>1G</ns1:AnnualOfferOfCoverageCd>
<ns1:AnnlShrLowestCostMthlyPremAmt xsi:nil="true" />
<ns1:MonthlyShareOfLowestCostMonthlyPremGrp>
<ns1:JanuaryAmt>0</ns1:JanuaryAmt>
<ns1:FebruaryAmt>0</ns1:FebruaryAmt>
<ns1:MarchAmt>0</ns1:MarchAmt>
<ns1:AprilAmt>0</ns1:AprilAmt>
<ns1:MayAmt>0</ns1:MayAmt>
<ns1:JuneAmt>0</ns1:JuneAmt>
<ns1:JulyAmt>0</ns1:JulyAmt>
<ns1:AugustAmt>0</ns1:AugustAmt>
<ns1:SeptemberAmt>0</ns1:SeptemberAmt>
<ns1:OctoberAmt>0</ns1:OctoberAmt>
<ns1:NovemberAmt>0</ns1:NovemberAmt>
<ns1:DecemberAmt>0</ns1:DecemberAmt>
</ns1:MonthlyShareOfLowestCostMonthlyPremGrp>
</ns1:EmployeeOfferAndCoverageGrp>
XSL
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="node()|SDLT">
<xsl:if test="count(descendant::text()[string-length(normalize-space(.)) > 0] | @*[string-length(.) > 0])">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="@*">
<xsl:copy />
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Views: 68
Reputation: 338406
This should get you pretty far. Replace the namespace URI for ns1
with the proper value.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://tempuri.org/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[@xsi:nil = 'true']" />
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 29052
Given the fact that you'd have to add namespaces to your XML as well as your XSLT like
xmlns:xsi="http://some.thing" and xmlns:ns1="some.other.thing"
you'd just have to add the following line to your stylesheet:
<xsl:template match="*[@xsi:nil = 'true']" />
Without namespaces, you could omit the 'xsi:' from the above expression.
Result:
<ns1:EmployeeOfferAndCoverageGrp xmlns:xsi="http://some.thing" xmlns:ns1="some.other.thing">
<ns1:AnnualOfferOfCoverageCd>1G</ns1:AnnualOfferOfCoverageCd>
<ns1:MonthlyShareOfLowestCostMonthlyPremGrp>
<ns1:JanuaryAmt>0</ns1:JanuaryAmt>
<ns1:FebruaryAmt>0</ns1:FebruaryAmt>
<ns1:MarchAmt>0</ns1:MarchAmt>
<ns1:AprilAmt>0</ns1:AprilAmt>
<ns1:MayAmt>0</ns1:MayAmt>
<ns1:JuneAmt>0</ns1:JuneAmt>
<ns1:JulyAmt>0</ns1:JulyAmt>
<ns1:AugustAmt>0</ns1:AugustAmt>
<ns1:SeptemberAmt>0</ns1:SeptemberAmt>
<ns1:OctoberAmt>0</ns1:OctoberAmt>
<ns1:NovemberAmt>0</ns1:NovemberAmt>
<ns1:DecemberAmt>0</ns1:DecemberAmt>
</ns1:MonthlyShareOfLowestCostMonthlyPremGrp>
</ns1:EmployeeOfferAndCoverageGrp>
Upvotes: 1