简体   繁体   English

如何在 XML (XSD) 模式中添加对复杂类型的限制?

[英]How can I add a restriction to a complextype in XML (XSD) schema?

Can anyone help me to add a restriction to this schema file (for OwnerEnglishName)?任何人都可以帮助我对此模式文件添加限制(对于 OwnerEnglishName)吗? I know how to do it with a simpletype, while in a complextype I don't know how to do it.我知道如何使用简单类型来实现,而在复杂类型中我不知道如何实现。 Can anyone help?谁能帮忙?

Thanks a lot.非常感谢。

Original XML:原XML:

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

Schema (without restriction):架构(无限制):

<?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>

The restriction code:限制代码:

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

This will do it:-这将做到:-

<?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>

Here is sample acceptable XML with this 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM