实现schematron规则

时间:2013-07-11 12:38:19

标签: schematron

我是schematron的新手,我有以下问题。这是来自xhtml文件的片段:

   
<div class="provision">
  <!-- provision (SECTION) -->
  <header>
    <!-- headers -->
    <div class="desig">
      <!-- if provision then header/div@class=desig/span@class=num required -->
      <span class="num">1.</span>
    </div>
    <h1 class="title">prov-title</h1>
  </header>
  <p class="prov">para-prov-level</p>
  <p class="prov">para-prov-level</p>
  <div class="glossary">
    <!-- glossary -->
    <!-- glossary requires dl -->
    <dl>
      <!-- dl should have dt, and at least one dd -->
      <dt> term </dt>
      <dd>definition</dd>
      <dt> term </dt>
      <dd>definition</dd>
    </dl>
  </div>
  <div class="level-1-grp">
    <!-- SUBSECTION -->
    <header>
      <div class="desig">
        <span class="num">(1)</span>
      </div>
    </header>
    <p class="level-1">para-level-1</p>
    <div class="level-2-grp">
      <header>
        <div class="desig">
          <span class="num">(a)</span>
        </div>
      </header>
      <p class="level-2">para-level-2</p>
    </div>
    <div class="level-2-grp">
      <header>
        <div class="desig">
          <span class="num">(b)</span>
        </div>
      </header>
      <p class="level-2">para-level-2</p>
    </div>
    <p class="level-1">para-level-1</p>
  </div>
  <p class="prov">para-prov-level</p>
</div>

我想创建一个规则进行检查 如果有provision,则需要header/div@class=desig/span@class=numglossary需要dl。我一直在尝试这样但它不起作用:

<iso:pattern id="structure-provision">
  <iso:title>Testing for Provision structure...</iso:title>
  <iso:rule context="*:div[@class='provision']">
    <iso:assert test="self::*:header/div[@class ='desig']/*:span[@class='num']">if provision then header/div@class=desig/span@class=num required</iso:assert>
  </iso:rule>
</iso:pattern>

<!--glossory structure -->

<iso:pattern id="structure-glossory">
  <iso:title>Testing for Provision structure...</iso:title>
  <iso:rule context="*:div[@class='glossory']">
    <iso:assert test="self::*:dl">glossary requires dl</iso:assert>
    <iso:assert test="self::*:dl/dt">dl should have a dt</iso:assert>
  </iso:rule>

</iso:pattern>

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:3)

你错过了第一个iso中的div元素的命名空间:assert。

英语不是很精确,所以我用两种方式解释你的要求。

1)如果有规定则(header / div @ class = desig / span @ class = num required AND glossary require dl)。 “如果是X,那么A和B”。如果是这种情况,则只应使用单一模式。

<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
    <sch:pattern id="structure-provision">
        <sch:title>Provisioning structure</sch:title>
        <sch:rule context="*:div[@class='provision']">
            <sch:assert test="*:header/*:div[@class ='desig']/*:span[@class='num']">
                The provision structure requires header/div@class=desig/span@class=num
            </sch:assert>
            <sch:assert test="*:div[@class='glossary']/*:dl">
                Glossary requires dl
            </sch:assert>
        </sch:rule>
    </sch:pattern>
</sch:schema>

2)(如果有条款,则header / div @ class = desig / span @ class = num required)AND(词汇表需要dl)。 “如果是X,那么A.另外,如果是Y,那么B.”如果是这种情况,则应使用两种模式。

<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
    <sch:pattern id="provision-structure">
        <sch:title>Provisioning structure</sch:title>
        <sch:rule context="*:div[@class='provision']">
            <sch:assert test="*:header/*:div[@class ='desig']/*:span[@class='num']">
                The provision structure requires header/div@class=desig/span@class=num
            </sch:assert>
        </sch:rule>
    </sch:pattern>

    <sch:pattern id="glossary-structure">
        <sch:title>Provisioning structure</sch:title>
        <sch:rule context="*:div[@class='glossary']">
            <sch:assert test="*:dl">
                Glossary requires dl
            </sch:assert>
            <sch:assert test="*:dl/*:dt">
                dl requires dt
            </sch:assert>
        </sch:rule>
    </sch:pattern>
</sch:schema>

我建议将您的需求分成几个规则/模式,以使事情更加健壮和有条理。例如,将所有规则与单个模式中的配置结构相关联。如果术语表规则应该在一个供应结构中,那么应该强制执行术语表规则,那么术语表应该有自己的模式。

此外,我将为元素定义和使用命名空间,并在schematron规则中使用它。我认为它是xh​​tml或类似? xpath表达式将变为:

<sch:rule context="xhtml:div[@class='provision']">
    <sch:assert test="xhtml:header/xhtml:div[@class ='desig']/xhtml:span[@class='num']">
        Rule text here
    </sch:assert>
</sch:rule>