简体   繁体   English

如何为该XML编写XML模式(XSD)?

[英]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). 最后一组可以容纳7-5人(与当年澳大利亚的情况相同)。
  • 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) year是适当的架构年份类型(不是字符串)

XML 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 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 #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重复元素声明。

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. 这将为您提供一个验证您的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.

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