I want to map an input XML to another XML, simply writing values from the input to different tags in the output.
As a simple example, the following:
<root1>
<a1>valA<a1>
<b1>valB<b2>
</root1>
Needs to become:
<root2>
<a2>valA</a2>
<b2>valB</b2>
</root2>
Currently I have the following in my XSLT:
<xsl:apply-templates match="root1" />
<xsl:template match="root1">
<a2>
<xsl:value-of select="a1" />
</a2>
<b2>
<xsl:value-of select="b2" />
</b2>
</xsl:template>
The problem is that I don't want empty tags in my output. If valA and valB are empty, I would get:
<root2>
<a2></a2>
<b2></b2>
<root2>
But I want to omit the empty tags. I would have thought that there could be an attribute to xsl:output for this, but there isn't... I came across this question on SO: XSLT: How to exclude empty elements from my result? - but the answer is indirect, it specifies a second stylesheet to strip empty output elements after the first transformation.
I need this to be done with one stylesheet. Surely there must be something more concise then doing:
<xsl:if test="string-length(a1) != 0">
<a2>
<xsl:value-of select="a1" />
</a2>
</xsl:if>
or even:
<xsl:template match="a1[string-length(.) != 0]">
<a2>
<xsl:value-of select="." />
</a2>
</xsl:template>
repeated for each element??
No comments:
Post a Comment