【问题标题】:Schematron regexp:test fails with broad expressionSchematron 正则表达式:测试因广泛表达而失败
【发布时间】:2016-02-05 20:01:37
【问题描述】:

我要疯了……

test 无法使用广泛匹配的任何正则表达式,例如 '^.+$'(显示在示例文件中),但适用于特定的 '^C.+$'

我也试过test="string-length(.) > 0",但失败了。

请帮忙。

这是 XML 文件:

<article>
<back><ack>
<title>Acknowledgements</title>
<p>The authors would like to thank <funding-source rid="sp1">CNPq</funding-source> (Process numbers <award-id rid="sp1">303287/2013-6</award-id> and <award-id rid="sp1">303559/2012-8</award-id>), <funding-source rid="sp2">FAPESP</funding-source> (Process number <award-id rid="sp2">2012/12207-6</award-id>) and <funding-source rid="sp3">CAPES</funding-source> (Process number <award-id rid="sp3">12357-13-8</award-id>) for the financial support.</p>
</ack></back></article>

这是失败的 schematron 文件:

<schema xmlns="http://purl.oclc.org/dsdl/schematron"
        queryBinding="exslt"
        xml:lang="en">
  <ns uri="http://www.w3.org/1999/xlink" prefix="xlink"/>
  <ns uri="http://exslt.org/regular-expressions" prefix="regexp"/>
  <pattern id="funding_info">
    <title>Make sure funding-source does not happen inside p</title>

      <assert test="regexp:test(current(), '^.+$')">
          EC-CHECK: Nao deve haver 'funding-source' nem 'award-id' dentro de 'p'
      </assert>
    </rule>
  </pattern>
</schema>

这是 WORKS 的 schematron 文件:

<schema xmlns="http://purl.oclc.org/dsdl/schematron"
        queryBinding="exslt"
        xml:lang="en">
  <ns uri="http://www.w3.org/1999/xlink" prefix="xlink"/>
  <ns uri="http://exslt.org/regular-expressions" prefix="regexp"/>
  <pattern id="funding_info">
    <title>Make sure funding-source does not happen inside p</title>

      <assert test="regexp:test(current(), '^C.+$')">
          EC-CHECK: Nao deve haver 'funding-source' nem 'award-id' dentro de 'p'
      </assert>
    </rule>
  </pattern>
</schema>

【问题讨论】:

  • 在第一个文件中 . 正在丢弃换行符。其次,它匹配C,然后转到everything,因为. 在正则表达式中跟随C
  • 我对@9​​87654333@了解不多,但是如果您能够添加modes,那么添加模式s
  • 试试^[\s\S]+$^[^]+$
  • 工作或失败,如果有的话,第一个应该总是工作。如果第二个有效,那么这不是因为明显的正则表达式结构,而是因为 schematron 对它的解释。这当然是错的。
  • 您没有指定规则的上下文。另外,您能否解释一下为什么使用 exslt queryBinding 而不是标准的 queryBinding 和函数?

标签: regex xml xslt schematron


【解决方案1】:

XSL 中的反斜杠似乎可以用来定义转义序列。当您需要定义一个正则表达式速记字符类时,您需要在特定字符前面加上一个 literal 反斜杠,因此,您需要使用一个 double 反斜杠:

^[\\s\\S]+$

此模式将匹配:

  • ^ - 字符串开头
  • [\\s\\S]+ - 一个或多个空格或非空格字符(因此,它匹配任何字符)
  • $ - 字符串结束。

这也意味着正则表达式风格不是 JavaScript,尽管 this reference 声称 EXSLT 使用 JS 风格。

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多