[英]How to write an XML Schema (XSD) for this XML?
Rules for Schema: 架构规则:
<?xml version="1.0"?>
<Slams ...>
<slam name="Australia" year="2012">
<winner>Djokovic</winner>
<runnerUp>Nadal</runnerUp>
<score>5-76-46-26-77-5</score>
<surface>Rebound Ace</surface>
<semiFinalist>Federer</semiFinalist>
<semiFinalist>Murray</semiFinalist>
</slam>
<slam name="French Open" year="2012">
<winner>Nadal</winner>
<runnerUp>Djokovic</runnerUp>
<score>6-46-32-67-5</score>
<surface>Clay</surface>
<semiFinalist>Federer</semiFinalist>
<semiFinalist>Ferrer</semiFinalist>
</slam>
<slam name="Wimbledon" year="2012">
<winner>Federer</winner>
<runnerUp>Murray</runnerUp>
<score>4-67-56-36-4</score>
<surface>Grass</surface>
<semiFinalist>Djokovic</semiFinalist>
<semiFinalist>Tsonga</semiFinalist>
</slam>
<slam name="US Open" year="2012">
<winner>Murray</winner>
<runnerUp>Djokovic</runnerUp>
<score>7-67-52-63-66-2</score>
<surface>Hard Court</surface>
<semiFinalist>Berdych</semiFinalist>
<semiFinalist>Ferrer</semiFinalist>
</slam>
</Slams>
This is what I have got so far but I'm not sure if it is correct. 到目前为止,这是我所得到的,但是我不确定是否正确。
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="Australia">
<xs:complexType>
<xs:sequence>
<xs:element name="winner" type="xs:string"/>
<xs:element name="runnerUp" type="xs:string"/>
<xs:element name="score" type="xs:string"/>
<xs:element name="surface" type="xs:string"/>
<xs:element name="semiFinalist" type="xs:string"/>
<xs:element name="semiFinalist" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="French Open">
<xs:complexType>
<xs:sequence>
<xs:element name="winner" type="xs:string"/>
<xs:element name="runnerUp" type="xs:string"/>
<xs:element name="score" type="xs:string"/>
<xs:element name="surface" type="xs:string"/>
<xs:element name="semiFinalist" type="xs:string"/>
<xs:element name="semiFinalist" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Wimbledon">
<xs:complexType>
<xs:sequence>
<xs:element name="winner" type="xs:string"/>
<xs:element name="runnerUp" type="xs:string"/>
<xs:element name="score" type="xs:string"/>
<xs:element name="surface" type="xs:string"/>
<xs:element name="semiFinalist" type="xs:string"/>
<xs:element name="semiFinalist" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="US Open">
<xs:complexType>
<xs:sequence>
<xs:element name="winner" type="xs:string"/>
<xs:element name="runnerUp" type="xs:string"/>
<xs:element name="score" type="xs:string"/>
<xs:element name="surface" type="xs:string"/>
<xs:element name="semiFinalist" type="xs:string"/>
<xs:element name="semiFinalist" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Problems to fix: 要解决的问题:
#1 #1
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
to 至
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
#2 #2
Define a single slam
element, with an name
attribute, not disparate elements for each such named possibility. 定义一个具有
name
属性的单独的slam
元素,而不是为每个这样的命名可能性使用完全不同的元素。 Also add a year
attribute. 还添加
year
属性。
#3 #3
Your XML (as far as you've shown) doesn't use namespaces, so remove that these lines from your XSD xs:schema
element: 您的XML(据您所显示的)不使用名称空间,因此请从XSD
xs:schema
元素中删除以下行:
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
#4 #4
Place slam
element within Slams
element declaration, and use maxOccurs="unbounded"
. 将
slam
元素放入Slams
元素声明中,并使用maxOccurs="unbounded"
。 Use maxOccurs="2"
(or 3 or unbounded
) for semiFinalist
rather than repeating the element declaration in slam
. 对
semiFinalist
使用maxOccurs="2"
(或3或unbounded
),而不要在slam
重复元素声明。
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Slams">
<xs:complexType>
<xs:sequence>
<xs:element name="slam" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="winner" type="xs:string"/>
<xs:element name="runnerUp" type="xs:string"/>
<xs:element name="score" type="xs:string"/>
<xs:element name="surface" type="xs:string"/>
<xs:element name="semiFinalist" type="xs:string"
maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="year" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This will get you an XSD that validates your XML. 这将为您提供一个验证您的XML的XSD。
Left as an exercise for the reader: Tune it to meet the given Rules for Schema . 留给读者练习:调整它以适应给定的Schema规则 。
元素名称是标签的名称:在您的情况下,您会将其与属性名称的值混淆
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.