需要XSLT 1.0解决方案才能获得相对简单的正则表达式

时间:2010-08-16 22:04:01

标签: xslt xslt-1.0

我的xml中有一个字段看起来像这样(一些例子):

<ViolationCharged>VTL0180     0D    0I0</ViolationCharged>
<ViolationCharged>VTL0180-C     02A    0I0</ViolationCharged>
<ViolationCharged>VTL1180     B    0I0</ViolationCharged>

我需要把它变成这样的东西:

<Violation>VTL180.0D</Violation>
<Violation>VTL180-C.02A</Violation>
<Violation>VTL1180.B</Violation>

基本上,我需要从该块中取出第一个字段并从数字块中删除前导零(如果存在),然后将第一个字段与第二个字段合并为一个句点。我有点像一个XSLT菜鸟,但有了2.0,我相信我可以用analyze-string和一个并不是特别复杂的正则表达式做到这一点,但是,我无法绕过任何可以工作的1.0中的任何东西而且我有点被迫使用已经存在的东西。

当然,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

此转化

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

应用于此XML文档时

<t>
    <ViolationCharged>VTL0180     0D    0I0</ViolationCharged>
    <ViolationCharged>VTL0180-C     02A    0I0</ViolationCharged>
    <ViolationCharged>VTL1180     B    0I0</ViolationCharged>
</t>

生成想要的正确结果

<t>
    <ViolationCharged>VTL0180.0D</ViolationCharged>
    <ViolationCharged>VTL0180-C.02A</ViolationCharged>
    <ViolationCharged>VTL1180.B</ViolationCharged>
</t>
相关问题