如何在 XML 模式中定义用户定义数据类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13410842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define a user define data type in XML schema?
提问by Leo03
I have defined two complex element types - Developer and App.
我定义了两种复杂的元素类型 - Developer 和 App。
Developer childs - ID ,Name, Email
开发者孩子 - ID、姓名、电子邮件
App childs - ID, Name, Developer
应用子项 - ID、名称、开发者
Here the Developerin App complex element refers to Developer/ID.
这里的Developerin App 复杂元素是指 Developer/ID。
How to define this relationship in xml schema. I am using XML spy2013 editor.
如何在 xml 模式中定义这种关系。我正在使用 XML spy2013 编辑器。
I have tried specifying name in the declaration of simple type Developer->ID. And using this name in App->Developer datatype. But it gives error..
我尝试在简单类型 Developer->ID 的声明中指定名称。并在 App->Developer 数据类型中使用此名称。但它给出了错误..
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2013 (x64) (http://www.altova.com) by Piyush (USC) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="appinfo">
<xs:complexType>
<xs:sequence>
<xs:element name="Developer">
<xs:complexType>
<xs:all>
**<xs:element name="ID">**
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="5"/>
<xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-zA-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Email">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[^@]+@[^\.]+\..+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Company" type="xs:string"/>
<xs:element name="Website" type="xs:string"/>
<xs:element name="Phone">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="13"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="App">
<xs:complexType>
<xs:all>
<xs:element name="ID">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="5"/>
<xs:pattern value="[0-9][0-9][0-9][0-9][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-zA-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Developer" ***type="xs:anySimpleType"/>***
<xs:element name="Price">
<xs:simpleType>
<xs:restriction base="xs:float">
<xs:minInclusive value="0.00"/>
<xs:maxInclusive value="6.99"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="TabletCompatible" type="xs:boolean" minOccurs="0"/>
<xs:element name="Category">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Travel"/>
<xs:enumeration value="Productivity"/>
<xs:enumeration value="Game"/>
<xs:enumeration value="Music"/>
<xs:enumeration value="Education"/>
<xs:enumeration value="Lifestyle"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Platform">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Android"/>
<xs:enumeration value="iOS"/>
<xs:enumeration value="Blackberry"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="AppStat">
<xs:complexType>
<xs:all>
<xs:element name="AppID" type="xs:anySimpleType"/>
<xs:element name="Statistics">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Platform" type="xs:anySimpleType"/>
<xs:element name="Downloads" type="xs:positiveInteger"/>
<xs:element name="Rating">
<xs:simpleType>
<xs:restriction base="xs:float">
<xs:minInclusive value="0.0"/>
<xs:maxInclusive value="5.0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="LastChecked" type="xs:date"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
回答by MiMo
The way to use a common simple type for the developer id is to declare it as a named type at the beginning:
对开发者 id 使用通用简单类型的方法是在开始时将其声明为命名类型:
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:simpleType name="developerID">
<xs:restriction base="xs:string">
<xs:length value="5"/>
<xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
. . .
and then use it:
然后使用它:
. . .
<xs:element name="Developer" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name="ID" type="developerID">
. . .
<xs:element name="App" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
. . .
<xs:element name="Developer" type="developerID"/>
But this is not enough to create a contraint so that appinfo/App/Developercan contain only one of the developers' ids declared in appinfo/Developer/ID. To do that is necessary to create a unique key definition using xs:keyand reference it using xs:keyref(see here).
但这不足以创建一个约束,使其appinfo/App/Developer只能包含appinfo/Developer/ID. 为此,必须使用创建唯一的键定义xs:key并使用它来引用它xs:keyref(请参阅此处)。
Here is the complete XSD:
这是完整的 XSD:
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:simpleType name="developerID">
<xs:restriction base="xs:string">
<xs:length value="5"/>
<xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="appinfo">
<xs:complexType>
<xs:sequence>
<xs:element name="Developer" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name="ID" type="developerID">
</xs:element>
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-zA-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Email">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[^@]+@[^\.]+\..+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Company" type="xs:string"/>
<xs:element name="Website" type="xs:string"/>
<xs:element name="Phone">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="13"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="App" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name="ID">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="5"/>
<xs:pattern value="[0-9][0-9][0-9][0-9][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-zA-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Developer" type="developerID"/>
<xs:element name="Price">
<xs:simpleType>
<xs:restriction base="xs:float">
<xs:minInclusive value="0.00"/>
<xs:maxInclusive value="6.99"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="TabletCompatible" type="xs:boolean" minOccurs="0"/>
<xs:element name="Category">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Travel"/>
<xs:enumeration value="Productivity"/>
<xs:enumeration value="Game"/>
<xs:enumeration value="Music"/>
<xs:enumeration value="Education"/>
<xs:enumeration value="Lifestyle"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Platform">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Android"/>
<xs:enumeration value="iOS"/>
<xs:enumeration value="Blackberry"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:all>
</xs:complexType>
<xs:keyref name="developerIDref" refer="developerID">
<xs:selector xpath="."/>
<xs:field xpath="Developer"/>
</xs:keyref>
</xs:element>
<xs:element name="AppStat">
<xs:complexType>
<xs:all>
<xs:element name="AppID" type="xs:anySimpleType"/>
<xs:element name="Statistics">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Platform" type="xs:anySimpleType"/>
<xs:element name="Downloads" type="xs:positiveInteger"/>
<xs:element name="Rating">
<xs:simpleType>
<xs:restriction base="xs:float">
<xs:minInclusive value="0.0"/>
<xs:maxInclusive value="5.0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="LastChecked" type="xs:date"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="developerID">
<xs:selector xpath="Developer"/>
<xs:field xpath="ID"/>
</xs:key>
</xs:element>
</xs:schema>

