Reputation: 3550
I've got a field in my xml that looks like this (some examples):
<ViolationCharged>VTL0180 0D 0I0</ViolationCharged>
<ViolationCharged>VTL0180-C 02A 0I0</ViolationCharged>
<ViolationCharged>VTL1180 B 0I0</ViolationCharged>
I need to turn it into something that looks like this:
<Violation>VTL180.0D</Violation>
<Violation>VTL180-C.02A</Violation>
<Violation>VTL1180.B</Violation>
Basically, I need to take the first field from that block and remove the leading zeros (if they exist) from the number block, then combine that first field with the second one with a period. I'm a bit of an XSLT noob, but with 2.0, I believe I can do this with analyze-string
and a not exceptionally complicated regular expression, however, I can't wrap my head around anything in 1.0 that will work and I'm sort of forced to use what's already in place here.
Any help is of course much appreciated.
Upvotes: 1
Views: 241
Reputation: 243529
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:variable name="vNormalized" select="normalize-space()"/>
<xsl:variable name="vWithDots" select="translate($vNormalized, ' ', '.')"/>
<xsl:variable name="vFinal" select=
"concat(substring-before($vWithDots, '.'),
'.',
substring-before(substring-after($vWithDots, '.'), '.'))"/>
<xsl:value-of select="$vFinal"/>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<t>
<ViolationCharged>VTL0180 0D 0I0</ViolationCharged>
<ViolationCharged>VTL0180-C 02A 0I0</ViolationCharged>
<ViolationCharged>VTL1180 B 0I0</ViolationCharged>
</t>
produces the wanted, correct result:
<t>
<ViolationCharged>VTL0180.0D</ViolationCharged>
<ViolationCharged>VTL0180-C.02A</ViolationCharged>
<ViolationCharged>VTL1180.B</ViolationCharged>
</t>
Upvotes: 3