简体   繁体   中英

How to write an XML Schema (XSD) for this XML?

Rules for Schema:

  • four slam elements
  • the score has four tie-break sets; the last set should cater for 7-5 (as was the case in Australia that year).
  • winner and runnerUp are strings; first letter is a capital letter, followed by lowercase letters
  • surface is one of Clay, Rebound Ace, Grass or Hard Court
  • name is one of Australia, French Open, Wimbledon or US Open
  • two semiFinalists, both strings; first letter is a capital letter, followed by lowercase letters
  • year is a proper schema year type (not string)

XML

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

XSD

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

<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

Define a single slam element, with an name attribute, not disparate elements for each such named possibility. Also add a year attribute.

#3

Your XML (as far as you've shown) doesn't use namespaces, so remove that these lines from your XSD xs:schema element:

           targetNamespace="http://www.w3schools.com"
           xmlns="http://www.w3schools.com"

#4

Place slam element within Slams element declaration, and use maxOccurs="unbounded" . Use maxOccurs="2" (or 3 or unbounded ) for semiFinalist rather than repeating the element declaration in slam .

Altogether:

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

Left as an exercise for the reader: Tune it to meet the given Rules for Schema .

元素名称是标签的名称:在您的情况下,您会将其与属性名称的值混淆

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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