使用XML Schema

时间:2016-06-05 12:47:02

标签: xml xsd xsd-validation xml-validation

我需要XML Schema的帮助。我想写一些条件,其中元素的最大值取决于其他元素的值

我的XML:

<databaza>
<dvd>
    <id>01</id>
    <type>DVD-R</type>
    <number_of_movies>2</number_of_movies>
    <movie>
        <movie_id>1</movie_id>
        <movie_name>X-man</movie_name>
        <number_of_characters>2</number_of_characters>
        <character>
            <character_id>1</character_id>
            <character_name>Andy Dufresne</character_name>
            <main_character>YES</main_character>
            <performer>Tim Robbins</performer>      
        </character>
        <character>
            <character_id>2</character_id>
            <character_name>Rede</character_name>
            <main_character>YES</main_character>
            <performer>Morgan Freeman</performer>       
        </character>
    </movie>
    <movie>
        <movie_id>2</movie_id>
        <movie_name>Forrest Gump</movie_name>
        <number_of_characters>4</number_of_characters>
        <character>
            <character_id>1</character_id>
            <character_name>Forrest Gump</character_name>
            <main_character>YES</main_character>
            <performer>Tom Hanks</performer>        
        </character>
        <character>
            <character_id>2</character_id>
            <character_name>Jenny Curran</character_name>
            <main_character>YES</main_character>
            <performer>Robin Wright</performer>     
        </character>
        <character>
            <character_id>3</character_id>
            <character_name>Bubba</character_name>
            <main_character>YES</main_character>
            <performer>Mykelti Williamson</performer>       
        </character>
        <character>
            <character_id>4</character_id>
            <character_name>Dan Taylor</character_name>
            <main_character>YES</main_character>
            <performer>Gary Sinise</performer>      
        </character>
    </movie>
</dvd>
</databaza>

我需要一些条件,说明<movie>的数量取决于<number_of_movies>,然后<character>的数量取决于<number_of_characters>

我怎样才能写出<character_id>从1到最大<number_of_characters>的条件以及<movie_id>的相同条件?

1 个答案:

答案 0 :(得分:0)

主要建议:不要明确跟踪电影数量或字符数,然后您不必保持数字与关联元素的数量一致。让它隐含。如果需要,XML的消费者可以获得计数,如果不需要则不会设置它。

如果由于某种原因你绝对必须有一个明确的计数,那么你就会意识到你不能在XSD 1.0中强制执行这样的约束。您在XSD 1.1中需要断言:

XSD 1.1

以下是如何让XSD强制movie计数等于number_of_movies元素给出的值:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" 
  elementFormDefault="qualified" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
  vc:minVersion="1.1">

  <xs:element name="dvd">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="number_of_movies" type="xs:integer"/>
        <xs:element name="movie" maxOccurs="unbounded" />
      </xs:sequence>
      <xs:assert test="count(movie) = number_of_movies"/>
    </xs:complexType>
  </xs:element>
 </xs:schema>

然后这个XML将是有效的,

<?xml version="1.0" encoding="UTF-8"?>
<dvd>
    <number_of_movies>1</number_of_movies>
    <movie/>
</dvd>

并且此XML无效,

<?xml version="1.0" encoding="UTF-8"?>
<dvd>
    <number_of_movies>1</number_of_movies>
    <movie/>
    <movie/>
</dvd>

按要求。