如何在XML(XSD)架构中为复杂类型添加限制?

时间:2012-04-05 08:47:53

标签: xml xsd schema restriction

任何人都可以帮我添加对此架构文件的限制(对于OwnerEnglishName)吗?我知道如何使用simpletype,而在复杂类型中我不知道如何做到这一点。有人可以帮忙吗?

非常感谢。

原始XML:

<PACIDemoSignedDoc PaciSDocID="HouseOwnerSignedEndorsement">  
  <OwnerEnglishName OENID="Name"></OwnerEnglishName>
</PACIDemoSignedDoc>

架构(没有限制):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="PACIDemoSignedDoc" type="PACIDemoSignedDocType" />
  <xs:complexType name="PACIDemoSignedDocType">
    <xs:sequence>
      <xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" />
    </xs:sequence>
    <xs:attribute name="PaciSDocID" type="xs:string" />
  </xs:complexType>
  <xs:complexType name="OwnerEnglishNameType">
    <xs:attribute name="OENID" type="xs:string" />
  </xs:complexType>
</xs:schema>

限制代码:

<xs:restriction base="xs:string">
  <xs:minLength value="5"/>
  <xs:maxLength value="100"/>
</xs:restriction>

1 个答案:

答案 0 :(得分:6)

这样做: -

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="PACIDemoSignedDoc" type="PACIDemoSignedDocType" />
    <xs:complexType name="PACIDemoSignedDocType">
        <xs:sequence>
            <xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" />
        </xs:sequence>
        <xs:attribute name="PaciSDocID" type="xs:string" />
    </xs:complexType>
    <xs:complexType name="OwnerEnglishNameType">
        <xs:simpleContent>
            <xs:restriction base="NameType">
                <xs:minLength value="5"/>
                <xs:maxLength value="10"/>
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="NameType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="OENID" type="xs:string" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:schema>

以下是具有此架构的示例可接受的XML

<?xml version="1.0" encoding="UTF-8"?>
<PACIDemoSignedDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" PaciSDocID="gggg">
    <OwnerEnglishName OENID="9999">GGGGG</OwnerEnglishName>
</PACIDemoSignedDoc>