正则表达式删除节点开头的模式

标签 regex xslt xslt-2.0

我一直在努力寻找摆脱某些特定标签的最佳解决方案。目前我用一些正则表达式重复查找/替换,但肯定有更好的方法。只是不清楚如何直接在 xslt 中执行此操作。

举个例子:

<local xml:lang="en">[Some Indicator]<div class="tab"/>some more content here</local>

我有很多这些,并且都遵循相同的结构,其中 [Some Indicator] 是一种列表标识符,可以是以下任何一种:

  • 一个或多个数字,有时后面跟一个点
  • 一个字符,有时后跟一个连字符和另一个字符
  • 给定代码点范围内的一个字符(在本例中为 57600 到 58607)
  • 和其他一些在上面的变体

我想摆脱所有这些,而不必手动查找/替换几百次。我一直在尝试 xsl:analyze-string,但它会替换所有内容而不会影响位置。

一些例子:

<some_nodes_above>
<local xml:lang="en">1<div class="tab"/>some more content here</local>
<local xml:lang="en">2.<div class="tab"/>some more content here</local>
<local xml:lang="fr">2-A<div class="tab"/>some more content here</local>
<local xml:lang="de">&#57600;<div class="tab"/>some more content here</local>
</some_nodes_above>

应该变成:

<some_nodes_above>
<local xml:lang="en">some more content here</local>
<local xml:lang="en">some more content here</local>
<local xml:lang="fr">some more content here</local>
<local xml:lang="de">some more content here</local>
</some_nodes_above>

因此,我正在寻找一个 xslt(2) 脚本,其中包含类似“每当您看到一个本地节点后跟一个给定指示器和一个选项卡 div 时,删除指示器和选项卡 div”之类的内容。不是为这个例子寻找一个完整的解决方案,只是让我朝着正确的方向前进。如果我知道它如何适用于一种模式,我可能可以自己找出其余部分

提前致谢。

最佳答案

这个转换:

<xsl:stylesheet version="2.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()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match=
  "local/node()[1]
               [self::text()
          and
            following-sibling::node()[1]
               [self::div and @class eq 'tab']
              and
               (
                matches(., '^(\d\.?)|(.\-.)$')
               or
                 string-length(.) eq 1
                and
                 string-to-codepoints(.) ge 57600
                and
                 string-to-codepoints(.) le 58607
                )
               ]"/>

 <xsl:template match=
  "div[@class eq 'tab'
     and
       preceding-sibling::node()[1]
               [self::text()
              and
               (
                matches(., '^(\d\.?)|(.\-.)$')
               or
                 string-length(.) eq 1
                and
                 string-to-codepoints(.) ge 57600
                and
                 string-to-codepoints(.) le 58607
                )
               ]
      ]"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<some_nodes_above>
    <local xml:lang="en"
     >1<div class="tab"/>some more content here</local>
    <local xml:lang="en"
     >2.<div class="tab"/>some more content here</local>
    <local xml:lang="fr"
     >2-A<div class="tab"/>some more content here</local>
    <local xml:lang="de"
     >&#57600;<div class="tab"/>some more content here</local>
</some_nodes_above>

产生想要的、正确的结果:

<some_nodes_above>
   <local xml:lang="en">some more content here</local>
   <local xml:lang="en">some more content here</local>
   <local xml:lang="fr">some more content here</local>
   <local xml:lang="de">some more content here</local>
</some_nodes_above>

关于正则表达式删除节点开头的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11285096/

相关文章:

Java 对条件先行的支持

python - Pandas : Count the number of occurrence of all matched patterns in a column

xml - 如何创建 boolean 值?

xslt - xsl :for-each? 有什么不好的

xml - 是否可以使用 xslt 来设置交叉 TableView 的样式?

xslt - 计算忽略空白节点的节点

xslt - XSL 查找节点之间的所有节点

regex - 匹配字符串的正则表达式,不以特定字符串结尾

jquery - mvc3 View 中的电子邮件格式验证

xml - 将 HL7 段转换为 XML