Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要一个相对简单的正则表达式的XSLT1.0解决方案吗_Xslt_Xslt 1.0 - Fatal编程技术网

需要一个相对简单的正则表达式的XSLT1.0解决方案吗

需要一个相对简单的正则表达式的XSLT1.0解决方案吗,xslt,xslt-1.0,Xslt,Xslt 1.0,我的xml中有一个字段如下所示(一些示例): VTL0180 0D 0I0 VTL0180-C 02A 0I0 VTL1180B 0I0 我需要把它变成这样: <Violation>VTL180.0D</Violation> <Violation>VTL180-C.02A</Violation> <Violation>VTL1180.B</Violation> VTL180.0D VTL180-C.02A VTL1180

我的xml中有一个字段如下所示(一些示例):

VTL0180 0D 0I0
VTL0180-C 02A 0I0
VTL1180B 0I0
我需要把它变成这样:

<Violation>VTL180.0D</Violation>
<Violation>VTL180-C.02A</Violation>
<Violation>VTL1180.B</Violation>
VTL180.0D
VTL180-C.02A
VTL1180.B
基本上,我需要从该块中获取第一个字段,并从数字块中删除前导零(如果存在),然后将第一个字段与第二个字段合并为一个句点。我有点像XSLT noob,但在2.0中,我相信我可以用
分析字符串
和一个不特别复杂的正则表达式来实现这一点,但是,我不能在1.0中使用任何可以工作的东西,我有点被迫使用这里已经存在的东西


当然,我们非常感谢您的帮助。

这一转变:

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

应用于此XML文档时

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

VTL0180 0D 0I0
VTL0180-C 02A 0I0
VTL1180B 0I0
生成所需的正确结果

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

VTL0180.0D
VTL0180-C.02A
VTL1180.B

好问题(+1)。有关完整的解决方案,请参见我的答案。:)关闭,但似乎没有删除0180上的前导零。这会让情况变得更糟吗?只需将
vFinal
声明中的
子字符串($vWithDots,“.”)替换为
子字符串(,,1,3),子字符串(,,5,7)
,因为在第三种情况下它会切断1。然而,我最后做的只是检查“VTL0”(以及字段可以开始的其他3种可能的内容),如果它以这种方式开始,我将执行与您建议的类似的子字符串。谢谢你的帮助!“这绝对是对的。@Morinar:如果你的第一个模式是三个字母和四个数字,你可以这样做:
子字符串(,1,3),数字(子字符串(,4,7))
。如果有多于或少于三个字母或四位数字:
translate(前面的子字符串($vWithDots,,),'1234567890',''),number(translate(前面的子字符串($vWithDots,,),'qwertyuiopasdfghjklzxcvnm','')