首页 > 解决方案 > 如何将 Schematron 添加到 xsd 文件?

问题描述

我有几个定义 xml (order.xml) 的 xsd 文件:

订单.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://www.test.com/po" 
            xmlns:po="http://www.test.com/po"
            xmlns:head="http://www.test.com/header"
            xmlns:prod = "http://www.test.com/product"
            xmlns:cust = "http://www.test.com/customer">

<xsd:import namespace="http://www.test.com/product" schemaLocation="product.xsd" />
<xsd:import namespace="http://www.test.com/customer" schemaLocation="customer.xsd" />
<xsd:import namespace="http://www.test.com/header" schemaLocation="header.xsd" />
<xsd:complexType name="itemType">
<xsd:sequence>
  <xsd:element name="Item" minOccurs="1" maxOccurs="unbounded" >
    <xsd:complexType>
       <xsd:sequence>
         <xsd:element name="ItemDescription" type="prod:prodType" />
         <xsd:element name="NumberOrdered" type="xsd:integer" />
       </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="potype">
<xsd:sequence>
  <xsd:element name="Header"   type="head:headerType"  />
  <xsd:element name="Items"    type="po:itemType" />
  <xsd:element name="Customer" type="cust:customerType" />
</xsd:sequence>
</xsd:complexType>

<xsd:element name="PurchaseOrder" type="po:potype" />

</xsd:schema>

客户.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://www.test.com/customer" >

<xsd:complexType name="customerType">
  <xsd:sequence>
    <xsd:element name="Name"    type="xsd:string" />
    <xsd:element name="Address" type="xsd:string" />
    <xsd:element name="Phone"   type="xsd:string" />
    <xsd:element name="email"   type="xsd:string" />
  </xsd:sequence>
  <xsd:attribute name="type"    type="xsd:integer" />
</xsd:complexType>
</xsd:schema>

最后是 order.xml

<?xml version="1.0" encoding="UTF-8"?>
                    <po:PurchaseOrder xmlns:po="http://www.test.com/po">
                       <Header>
                            <Id>1</Id>
                            <date>2004-01-29</date>
                            <description>purchase order</description>
                            <value>20</value>
                            <status>shipped</status>
                       </Header>
                       <Items>
                            <Item>
                                 <ItemDescription color="red" weight="5">
                                          <Name>Widget C</Name>
                                          <SKU>1</SKU>
                                          <Price>30</Price>
                                          <Comment>no comment</Comment>
                                 </ItemDescription>
                                 <NumberOrdered>1</NumberOrdered>
                            </Item>
                       </Items>
                       <Customer type="5">
                             <Name>Manoj K Sardana</Name>
                             <Address>ring road, bangalore</Address>
                             <Phone>918051055109</Phone>
                             <email>msardana@in.ibm.com</email>
                       </Customer>
                    </po:PurchaseOrder>

我想为类型* 添加一个 schematron,这是Customer属性之一。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.ocle.org/dsdl/schematron" queryBinding="xslt2">
    <pattern>
        <rule context="Customer">
            <assert test"@type > 10">Type is too big</assert>
            <report test"@type > 100">Type is too big</report>
        </rule>
    </pattern>
</schema>

但是我无法将其添加为按顺序 xsd 命名空间定义的其他 header.xsd、customer.xsd、product.xsd。我不能确定正确的语法。

任何解决方案都应该受到赞赏。我的解决方法应该是什么?我应该怎么办?

标签: xmlschematron

解决方案



推荐阅读