【问题标题】:Is it possible to get the full xpath within a schematron rule?是否可以在 schematron 规则中获取完整的 xpath?
【发布时间】:2018-04-04 15:32:09
【问题描述】:

我想在抽象的 schematron 规则中输出一些上下文。

示例 XML

...
<xpath>
    <to>
        <search>Content</search>
    </to>
</xpath>

Schematron

<sch:rule context="$element">
    <sch:report test="true()">
         <sch:value-of select="$element"/>
    </sch:report>
</sch:rule>

<sch:pattern id="tests-1" is-a="test">
    <sch:param name="element" value="//xpath//to//search"/>
</sch:pattern>
...

输出Content,有没有办法获取传入的xpath值//xpath//to//search

【问题讨论】:

    标签: xml xpath schematron


    【解决方案1】:

    如果您使用 Schematron 的基于 XSLT 的参考实现来生成 SVRL 输出,那么 SVRL 将包含成功报告的上下文的 XPath,如下所示:

    <svrl:successful-report ... location="[this is where the XPath will be]">
    

    如果这不能满足您的需求并且您仍然需要获取节点的 XPath,并且您正在使用基于 XSLT 的参考实现,那么您可以利用在实现中定义的 XSL 模板,像这样:

    <sch:pattern>
      <sch:rule context="//xpath//to//search">
        <sch:report test="true()">
          <xsl:apply-templates select="." mode="schematron-get-full-path"/>
        </sch:report>
      </sch:rule>
    </sch:pattern>
    

    为此,您必须在将 Schematron 模式编译为 XSLT 时使用 allow-foreign 参数,如果您使用的是 Saxon,则如下所示:

    [path/to/saxon/]Transform
     –xsl:iso-schematron-xslt2\iso_svrl_for_xslt2.xsl
     –s:[SchematronFile2.sch]
     –o:[SchematronFile.xsl]
     allow-foreign=true
    

    这种方法会使您的 Schematron 模式依赖于基于 XSLT 的 Schematron 参考实现。其他实现可能没有必要的 XSL 模板。

    【讨论】:

      【解决方案2】:

      为此,您需要一个抽象的 Schematron 模式。所以这个

      <sch:pattern id="test" abstract="true">
          <sch:rule context="$element">
              <sch:report test="true()">
                  <sch:value-of select="$element"/>
              </sch:report>
          </sch:rule>
      </sch:pattern>
      
      <sch:pattern id="tests-1" is-a="test">
          <sch:param name="element" value="//xpath//to//search"/>
      </sch:pattern>
      

      应该可以。

      编辑:请注意,您的示例表达式应该可以工作,但不是每个 XPath 表达式都允许在 @contextsch:rule 中使用(实际上它被称为 XSLT 模式)。

      允许的表达式:

      //node
      node/to/path
      node/to/@attribute
      node[following-sibling::any/xpath or function-call()]
      node|otherNode
      

      禁止表达:

      /node/following-sibling::other/node
      //node/ancestor::node
      function-call()
      to/be or not/to/be
      

      在此处阅读更多信息:https://www.w3.org/TR/xslt20/#patterns

      EDIT2:如果您想将未计算的 XPath 表达式作为错误消息,您可以使用:

      <sch:report test="true()">
          <sch:value-of select=" '$element' "/>
      </sch:report>
      

      这是可行的,因为抽象模式的参数将在 XPath 表达式被计算之前被替换。

      请注意,这仅在其自身的 XPath 表达式不包含引号时才有效(例如 node[@attribute = 'value'])。

      【讨论】:

      • 这正是我在原始问题中的内容,只有您添加了报告。这只会产生元素,而不是 xpath。我很好奇是否可以以某种方式检索上下文元素的实际 xpath。这样一来,Schematron 发现的任何问题都将更容易被最终用户发现和补救。
      • 抱歉,看来我没有正确理解您的问题。你想作为错误消息'//xpath//to//search'而不是'Content'吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      相关资源
      最近更新 更多