一个XML Schema,用于多个但类似的XML文件

时间:2009-03-22 10:25:38

标签: xml validation xsd

如何创建一个可以处理多个但类似的XML文件的XML Schema。例如,对于食谱食谱网站,我有一个名为'甜点'的食谱XML,另一个'印度'和另一个'素食',它们几乎都是一样的,唯一改变的是根元素。这是一个文件示例:

<?xml version="1.0" encoding="utf-8"?>
<!-- this one could be dessert, indian or vegetarian -->
<dessert id="dessert01" xmlns="http://www.w3schools.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="schematize.xsd">
    <category>Dessert</category>
    <headline>Caramelised Bananas and Puff Pastry</headline>
    <thumbnail>http://media.lookandtaste.com/files/video_thumbs/caramelized_banana_on_puff_pastry.jpg</thumbnail>
    <intro>Most people are terrified of making desserts so this is one
    that I invented to get you all out of that tricky situation...</intro>
    <media>
        <video><![CDATA[
        <object type="application/x-shockwave-flash" data="http://www.lookandtaste.com//web/jwplayer/player.swf" width="480" height="292" ><param name="movie" value="http://www.lookandtaste.com//web/jwplayer/player.swf" /><param name="bgColor" value="#000000" /><param name="allowfullscreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="FlashVars" value="stretching=exactfit&file=http://videos.lookandtaste.com/New_Vids/Caramelised Bananas and Puff Pastry_01.flv&image=http://www.lookandtaste.com/files/video_thumbs/caramelized_banana_on_puff_pastry.jpg"/></object>
     ]]>
        </video>
    </media>
    <summary>
        <preparation>10 minutes</preparation>
        <time>20 minutes</time>
        <difficulty>Medium</difficulty>
        <healthy>2</healthy>
    </summary>
    <ingredients>
        <ingredient>Puff pastry</ingredient>
        <ingredient>Bananas</ingredient>
        <ingredient>12 cups of sugar</ingredient>
        <ingredient>Knob of butter</ingredient>
        <ingredient>2 tsp of creme fraiche</ingredient>
        <ingredient>1/2 cup of chocolate</ingredient>
    </ingredients>
    <procedures>
        <step>Peel the bananas and chop into chunks. Put the butter and
        bananas in a pan over a medium heat and sprinkle with sugar.
        Caramelise for 4-5 minutes moving the ingredients every few minutes.</step>
        <step>Meanwhile sprinkle flour onto a baking tray and also
        onto a clean flat surface. Roll your pastry on the surface and then
        cut out a circle shape using the top of a bowl as a cutter.</step>
        <step>Put the pastry on the floured baking tray and prick the
        pastry with a fork. Put the pastry into the over for 12-15 minutes at
        180c/360f.</step>
        <step>The bananas should be caramelised now so take them off the
        heat and set them aside.</step>
        <step>Take the pastry out of the oven once it is ready and pile
        the bananas on top with some creme fraiche. Drizzle with melted
        chocolate and serve.</step>
    </procedures>
    <keywords>banana hot caramel dessert</keywords>
</dessert>

...这是我的XML Schema文件:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
    <xs:element name="dessert">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="category"/>
                <xs:element ref="headline"/>
                <xs:element ref="thumbnail"/>
                <xs:element ref="intro"/>
                <xs:element ref="media"/>
                <xs:element ref="summary"/>
                <xs:element ref="ingredients"/>
                <xs:element ref="procedures"/>
                <xs:element ref="keywords"/>
            </xs:sequence>
            <xs:attribute name="id" use="required" type="xs:NCName"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="category" type="xs:NCName"/>
    <xs:element name="headline" type="xs:string"/>
    <xs:element name="thumbnail" type="xs:anyURI"/>
    <xs:element name="intro" type="xs:string"/>
    <xs:element name="media">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="video"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="video" type="xs:string"/>
    <xs:element name="summary">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="preparation"/>
                <xs:element ref="time"/>
                <xs:element ref="difficulty"/>
                <xs:element ref="healthy"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="preparation" type="xs:string"/>
    <xs:element name="time" type="xs:string"/>
    <xs:element name="difficulty" type="xs:NCName"/>
    <xs:element name="healthy" type="xs:integer"/>
    <xs:element name="ingredients">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" ref="ingredient"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="ingredient" type="xs:string"/>
    <xs:element name="procedures">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" ref="step"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="step" type="xs:string"/>
    <xs:element name="keywords" type="xs:string"/>
</xs:schema>

非常感谢指出可能的解决方案或如何处理它。提前谢谢!

4 个答案:

答案 0 :(得分:2)

不只是:

<xs:complexType name="food">...</xs:complexType>
<xs:element type="food" name="dessert"/>
<xs:element type="food" name="indian"/>

答案 1 :(得分:1)

在为根元素编写模式时,需要指定“选择”指示符。由于大多数内容对于所有根元素都是通用的,因此您可以定义一个复杂类型,然后重复使用以避免再次编写它们。请参阅http://www.w3schools.com/schema/schema_complex_indicators.asp

[编辑]

如果您可以控制XML文档结构(即,如果它们未被其他应用程序使用),为什么不定义名为“food”的根元素并指定具有诸如“desert”和“indian”之类的值的可能属性foodType

答案 2 :(得分:0)

这是一种方法:

将架构元素更改为:

<xs:schema xmlns="urn:my.nampespace"
    targetNamespace="urn:my.nampespace"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

将第一个元素更改为:

<xs:element name="dessert" type="recipeType"/>
<xs:element name="indian" type="recipeType"/>
<xs:complexType name="recipeType">
    <xs:sequence>
        <xs:element ref="category"/>
        <xs:element ref="headline"/>
        <xs:element ref="thumbnail"/>
        <xs:element ref="intro"/>
        <xs:element ref="media"/>
        <xs:element ref="summary"/>
        <xs:element ref="ingredients"/>
        <xs:element ref="procedures"/>
        <xs:element ref="keywords"/>
    </xs:sequence>
    <xs:attribute name="id" use="required" type="xs:NCName"/>
</xs:complexType>

现在您可以将xml定义为:

<dessert id="dessert01"
    xmlns="urn:my.nampespace" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="urn:my.nampespace recipe.xsd">

或者:

<indian id="indian01" 
    xmlns="urn:my.nampespace" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="urn:my.nampespace recipe.xsd">

答案 3 :(得分:0)

kulfi怎么样? (素食,印度甜点)

您可能不应该更改根元素,而是添加属性以指示类别。