开发者

Need an XSLT 1.0 solution for what would be a relatively simple regular expression

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 开发者_如何学JAVAand 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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜