代码之家  ›  专栏  ›  技术社区  ›  Idan

使用Schematron进行XSD验证

  •  5
  • Idan  · 技术社区  · 14 年前

    我正在尝试将Schematron验证添加到我的XSD中。 这是我的新XSD:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        xmlns:sch="http://www.ascc.net/xml/schematron"    
        elementFormDefault="qualified" &gt;
    
     <xs:element name="books"> 
      <xs:complextype>
       <xs:sequence>   ;P 
        <xs:element name="book" type="bookType" maxoccurs="unbounded">
          <xs:annotation>
           <xs:appinfo>
            <sch:pattern id="onLoanTests" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
              <sch:rule context="book">
               <sch:report test="@on-loan and not(@return-date)">
               Every book that is on loan must have a return date
               </sch:report>
              </sch:rule>
            </sch:pattern>
           </xs:appinfo>
          </xs:annotation>
        </xs:element>
       </xs:sequence> 
      </xs:complextype>
     </xs:element>
    
     <xs:complextype name="bookType">
      <xs:sequence>
       <xs:element name="title" type="xs:string" />
       <xs:element name="author" type="xs:string" />
       <xs:element name="publication-date" type="xs:string" />
      </xs:sequence>
      <xs:attribute name="publisher" type="xs:string" use="required" />
      <xs:attribute name="on-loan" type="xs:string" use="required" />
      <xs:attribute name="return-date" type="xs:string" use="optional" />
     </xs:complextype>
    
    </xs:schema>
    

    这是我的测试XML:

    <books>
    <book publisher="ddd" on-loan="sdsd">
      <title>idan title</title> 
      <author>idan author</author> 
      <publication-date>idan date</publication-date> 
    </book>
    </books>
    

    使用我提供的XML不会得到验证错误。

    我假设我会收到消息“每本借出的书都必须有一个归还日期”,并且XML将无效。关于原因的建议?

    更新 我确实通过在OxygenXML编辑器中使用Schematron验证使其工作。 但是,我应该如何在代码中使用? 我需要安装一些特殊的东西吗?链接到另一个库?

    更新2 显然 here 在“处理”部分中,详细介绍了所有需要的步骤。

    1 回复  |  直到 14 年前
        1
  •  8
  •   Nic Gibson    14 年前

    第二次更新可能是对主题的最佳参考。XSD本身不允许您根据Schematron以及模式本身进行验证。这个 xsd:appinfo 元素允许您为不同的模式语言嵌入验证信息,但它专门用于应用程序(因此是名称)。

    这意味着你需要做点什么来启用它。您所引用的文章给出了最佳方法,具体如下:

    1. 使用XSLT提取Schematron XSD中的规则
    2. 使用引用xslt 实施自 schematron.com 转换 模式到XSLT
    3. 验证实例文档 针对XSD
    4. 验证实例文档 通过处理对抗Schematron 在2中创建的XSLT。

    根据您的环境,您可能需要考虑 XProc 实施( calabash calumet )为了实现这条管道。