You can accomplish what you want with two specialized templates in an identity transform.
- A template for SSN that uses either
xsl:number
or thenumber()
function to remove the leading zeros - A template that matches any element who's computed value is "null" and copies the element without any of it's content.
Applied in the following stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--identity template, which will copy all content by default-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--For all SSN elements,
copy the matched element and use xsl:number to
remove any leading zeros from the value -->
<xsl:template match="SSN">
<xsl:copy>
<xsl:number value="."/>
</xsl:copy>
</xsl:template>
<!--for any element who's computed value is "null",
copy the element and do not copy it's value(removing "null")-->
<xsl:template match="*[.='null']">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
A shorter version that matches on the text()
nodes instead of their elements:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!--identity template, which will copy all content by default-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--For all text() nodes of SSN elements,
Use xsl:number to remove any leading zeros from the value -->
<xsl:template match="SSN/text()">
<xsl:number value="."/>
</xsl:template>
<!--for any text() node who's value is "null", suppress it from the output-->
<xsl:template match="*/text()[.='null']"/>
</xsl:stylesheet>