1

我的 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 中任何可以工作的东西,我是排序被迫使用这里已经存在的东西。

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

4

1 回答 1

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>
于 2010-08-16T22:18:34.207 回答