如何在XSD中表示子元素中的重复?

时间:2016-02-15 11:01:27

标签: xml xsd xsd-validation xml-validation

我正在学习XML和XSD架构,我无法理解为什么我的子元素未经过验证。

这是我的XML:

<?xml version="1.0" encoding="UTF-8"?>

<recipes
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="recipe.xsd">
    <recipe id= "1">
        <name>Gooseberry crème brulée tart</name>
        <author>Barney Desmazery</author>
        <Magazine_subscription>5 issues for £5</Magazine_subscription>
        <preparation_time>10 mins</preparation_time>
        <cooking_time>80 mins</cooking_time>
            <additional_time></additional_time>
        <serves>8</serves>
        <level>Moderately easy</level>
        <overview>Seasonal berries and sweet pastry make a stunning dessert for a dinner party - blowtorch for a beautifully crisp cracked sugar topping</overview>
        <nutrition>
            <kcalories>467</kcalories>
            <fat>21g</fat>
                <saturates>11g</saturates>
            <carbs>60g</carbs>
                <sugars>39g</sugars>
            <fibre>3g</fibre>
            <protein>7g</protein>
            <salt>0.4g</salt>
        </nutrition>    
            <ingredients>
                <ingredient>gooseberries</ingredient>
                    <quantity>100g</quantity>
                    <process/>
                <ingredient>white caster sugar</ingredient>
                    <quantity>200g</quantity>
                    <process/>
                <ingredient>eggs</ingredient>
                    <quantity>4</quantity>
                    <process/>
                <ingredient>double cream</ingredient>
                    <quantity>100ml</quantity>
                    <process/>
                <ingredient>block sweet pastry</ingredient>
                    <quantity>500g</quantity>
                    <process/>
                <ingredient>flour</ingredient>
                    <quantity>for dusting</quantity>
                    <process/>
            </ingredients>
            <method>
                <step>Tip the gooseberries into a saucepan with 100g of the sugar and 100ml water. Simmer for 8-10 mins until the fruit is soft and the juices are syrupy. Tip the fruit into a sieve set over a jug and leave to strain – you will need about 150ml of the syrupy juices. Tip the pulp into a bowl and leave to cool.</step>
                <step>In a separate bowl, beat the eggs with 50g of the sugar, then beat in the cream and gooseberry syrup. Strain through a sieve into another jug and set aside.</step>
                <step>Heat oven to 160C/140C fan/gas 3. Roll out the pastry on a lightly floured surface to the thickness of a £1 coin, then lift into a 23cm tart tin. Press down gently on the bottom and sides, leaving a slight overhang. Line the tart with foil and fill with baking beans. Bake for 10 mins, then discard the foil and beans, and bake for another 20 mins. Remove from the oven and leave to cool.</step>
                <step>Reduce oven to 150C/130C fan/gas 2. Spread the pulp evenly over the base of the tart, then carefully pour the cream mixture over it to create 2 layers. Bake for 35-40 mins until the cream layer has the slightest wobble to it</step>   
            </method>       
    </recipe>
    <recipe id= "2">
        <name>Roast sirloin of beef</name>
        <author>Gary Rhodes</author>
        <Magazine_subscription>5 issues for £5</Magazine_subscription>
        <preparation_time>10 mins</preparation_time>
        <cooking_time>150 mins</cooking_time>
            <additional_time>Resting time</additional_time>
        <serves>6</serves>
        <level>Easy</level>
        <overview>Gary Rhodes' delicious, succulent roast beef dish is perfect for Boxing Day lunch or an alternative Christmas Day roast. Just four ingredients</overview>
        <nutrition>
            <kcalories>331</kcalories>
            <fat>19g</fat>
                <saturates>8g</saturates>
            <carbs>1g</carbs>
                <sugars>0g</sugars>
            <fibre>0g</fibre>
            <protein>36g</protein>
            <salt>1.47g</salt>
        </nutrition>    
            <ingredients>
                <ingredient>vegetable oil or beef fat</ingredient>
                    <quantity>2tbsp</quantity>
                    <process/>
                <ingredient>sirlon of beef joint</ingredient>
                    <quantity>1-1 1/2kg/2lb 4-3lb 5oz</quantity>
                    <process/>
                <ingredient>red wine</ingredient>
                    <quantity>1 glass</quantity>
                    <process/>
                <ingredient>can beef consommé</ingredient>
                    <quantity>400g</quantity>
                    <process/>                  
            </ingredients>
            <method>
                <step>Heat oven to 200C/fan 180C/gas 6. Heat the oil or fat in a large fl ameproof roasting tin in the oven for 5-10 mins. Season the beef all over.</step>
                <step>Place the joint fat-side down in the roasting tin and put it on the hob. Sizzle to release some of the fat, then turn the beef in the fat to seal and colour it all over, about 5 mins. The joint can now be roasted, fat-side up, allowing 10-15 mins per 450g for medium-rare, 15-20 mins per 450g for medium, 20-25 mins for medium-well and 25-30 mins for well done. Turn the joint halfway through its cooking time for an even roast. Once roasted, remove the beef from the pan and leave to one side, loosely covered with foil, to rest for 15-20 mins</step>
                <step>Meanwhile, pour off any excess fat from the roasting tin, then put the tin on a medium heat on top of the stove. Once it begins to sizzle, pour in the red wine, allowing it to boil rapidly until almost completely evaporated. Tip in the consommé and allow to simmer for 4-5 mins. This will give a light sauce, rather than a gravy (see tip, below). Simmer for a few more mins before straining through a sieve for the smoothest of fi nishes. Carve the beef into thick slices, offering the gravy separately.</step>    
            </method>           
    </recipe>
    <recipe id= "3">
        <name>Haddock &amp; leek au gratin with sweetcorn mash</name>
        <author>Sara Buenfeld</author>
        <Magazine_subscription>5 issues for £5</Magazine_subscription>
        <preparation_time>10 mins</preparation_time>
        <cooking_time>50 mins</cooking_time>
            <additional_time></additional_time>
        <serves>2</serves>
        <level>Easy</level>
        <overview>Leek and spinach are layered up with white fish fillets and cheese sauce then topped with golden mash in this delicious, low-fat oven bake.</overview>
        <nutrition>
            <kcalories>618</kcalories>
            <fat>13g</fat>
                <saturates>6g</saturates>
            <carbs>70g</carbs>
                <sugars>20g</sugars>
            <fibre>11g</fibre>
            <protein>50g</protein>
            <salt>2.1g</salt>
        </nutrition>    
            <ingredients>
                <ingredient>potatoes</ingredient>
                    <quantity>350g</quantity>
                    <process>quartered</process>
                <ingredient>can sweetcorn in water</ingredient>
                    <quantity>195g</quantity>
                    <process/>
                <ingredient>bag ready-washed spinach</ingredient>
                    <quantity>240g</quantity>
                    <process/>
                <ingredient>leaks</ingredient>
                    <quantity>2</quantity>
                    <process>thickly sliced</process>
                <ingredient>skimmed milk</ingredient>
                    <quantity>plus 3 tbsp</quantity>
                    <process/>
                <ingredient>unsalted butter</ingredient>
                    <quantity>15g</quantity>
                    <process/>
                <ingredient>plain flour</ingredient>
                    <quantity>15g</quantity>
                    <process/>
                <ingredient>English mustard</ingredient>
                    <quantity>1/2 tsp</quantity>
                    <process/>
                <ingredient>mature reduced-fat cheese</ingredient>
                    <quantity>75g</quantity>
                    <process>grated</process>
                <ingredient>fillets of skinless haddock</ingredient>
                    <quantity>2 x 125g</quantity>
                    <process/>                                          
            </ingredients>
            <method>
                <step>Heat oven to 200C/180 fan/gas 6. Boil the potatoes for 15-20 mins until tender, then drain. Reserve 3 tbsp corn and blitz the rest (with its juice) using a hand blender or a food processor until completely smooth, then mash into the potatoes. Cook the spinach following pack instructions – if you have a microwave, choose this method.</step>
                <step>Put the leeks in a pan with the 300ml milk and the butter. Cook gently, part-covered, for 8 mins until the leeks are tender. (Keep an eye on things as milk can easily boil over.) Mix the 3 tbsp milk with the flour and mustard, then stir into the leek mixture – keep stirring until thickened. Take off the heat and stir in three-quarters of the cheese.</step>
                <step>Squeeze as much liquid as you can from the spinach, then arrange on the base of 2 gratin dishes. Place a fish fillet on top of each, then spoon over the leek &amp; cheese sauce. Top with the sweetcorn mash. Mix the remaining corn and cheese, and scatter on top. Place the dishes on a baking tray and cook for 25 mins until the fish flakes when tested and the top is golden.</step>  
            </method>           
    </recipe>
    <recipe id= "4">
        <name>Cheesy mushroom omelette</name>
        <author>Good Food</author>
        <Magazine_subscription>5 issues for £5</Magazine_subscription>
        <preparation_time>5 mins</preparation_time>
        <cooking_time>10 mins</cooking_time>
            <additional_time></additional_time>
        <serves>1</serves>
        <level>Easy</level>
        <overview>Whip up a tasty omelette with this recipe</overview>
        <nutrition>
            <kcalories>391</kcalories>
            <fat>33g</fat>
                <saturates>10g</saturates>
            <carbs>0.3g</carbs>
                <sugars>0.2g</sugars>
            <fibre>0.7g</fibre>
            <protein>22g</protein>
            <salt>0.9g</salt>
        </nutrition>    
            <ingredients>
                <ingredient>olive oil</ingredient>
                    <quantity>1tbsp</quantity>
                    <process/>
                <ingredient>handful button or chestnut mushrooms</ingredient>
                    <quantity/>
                    <process></process>
                <ingredient>vegetarian cheddar,grated</ingredient>
                    <quantity>25g</quantity>
                    <process/>
                <ingredient>small handfull parsley leaves</ingredient>
                    <quantity></quantity>
                    <process>roughly chopped</process>
                <ingredient>eggs</ingredient>
                    <quantity>2</quantity>
                    <process>beaten</process>                                       
            </ingredients>
            <method>
                <step>Heat the olive oil in a small non-stick frying pan. Tip in the mushrooms and fry over a high heat, stirring occasionally for 2-3 mins until golden. Lift out of the pan into a bowl and mix with the cheese and parsley.</step>
                <step>Place the pan back on the heat and swirl the eggs into it. Cook for 1 min or until set to your liking, swirling with a fork now and again.</step>
                <step>Spoon the mushroom mix over one half of the omelette. Using a spatula or palette knife, flip the omelette over to cover the mushrooms. Cook for a few moments more, lift onto a plate and serve with oven chips and salad.</step> 
            </method>           
    </recipe>                                                                               
</recipes>  

这是我的架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="recipes">
    <xs:complexType>
       <xs:sequence>    
        <xs:element ref="recipe" maxOccurs="unbounded" />
       </xs:sequence>   
    </xs:complexType>
</xs:element>


  <xs:element name="recipe">
    <xs:complexType>
      <xs:sequence> 
        <xs:element name="name" type="xs:string"/>
        <xs:element name="author" type="xs:string"/>
        <xs:element name="Magazine_subscription" type="xs:string"/>
        <xs:element name="preparation_time" type="xs:string"/>
        <xs:element name="additional_time" type="xs:string" nillable="true"/>
        <xs:element name="cooking_time" type="xs:string"/>
        <xs:element name="serves" type="xs:string"/>
        <xs:element name="level">
            <xs:simpleType>
            <xs:restriction base="xs:string">   
                <xs:enumeration value="Easy"/>
                <xs:enumeration value="Moderately easy"/>
                <xs:enumeration value="For the keen cook"/>
            </xs:restriction>   
            </xs:simpleType>
        </xs:element>   
        <xs:element name="overview" type="xs:string"/>
        <xs:element name="kcalories" type="xs:string"/>
        <xs:element name="fat" type="xs:string"/>
        <xs:element name="saturates" type="xs:string"/>
        <xs:element name="carbs" type="xs:string"/>
        <xs:element name="sugars" type="xs:string"/>
        <xs:element name="fibres" type="xs:string"/>
        <xs:element name="protein" type="xs:string"/>
        <xs:element name="salt" type="xs:string"/>
        <xs:element name="ingredients">
            <xs:complexType>
                <xs:sequence>   
                    <xs:element name="ingredient" type="xs:string" maxOccurs="unbounded" />
                    <xs:element name="quantity" type="xs:string" nillable="true"/>
                    <xs:element name="process" type="xs:string" nillable="true"/>
                </xs:sequence>  
            </xs:complexType>
        </xs:element>
        <xs:element name="method">
        <xs:complexType>
            <xs:all>    
                <xs:element name="step" type="xs:string" maxOccurs="1" />
            </xs:all>   
        </xs:complexType>
        </xs:element>   
      </xs:sequence>
        <xs:attribute name="id" type="xs:string"/>
    </xs:complexType>
  </xs:element> 
</xs:schema>    

我使用xsd:sequencexsd:all尝试了两种不同的方法,但我的ingredientssteps的错误似乎相同。

1 个答案:

答案 0 :(得分:2)

以下是验证XML所需的两个关键XSD更正(包括几个):

  1. maxOccurs="unbounded"添加到ingredients xs:sequence 而不是第一个ingredient,因为您的XML允许序列 ingredientquantityprocess共同重复。
  2. maxOccurs="unbounded"添加到您的step元素声明中,因为您的 XML允许step内的多个method元素。 (你提到 xs:all。这可以用来允许无序的孩子,但事实并非如此 真的是这里的问题;只要你拥有,xs:sequence就足够了 最大出现约束设置正确。)
  3. XSD

    以下是您的XSD,其中包含上述和其他几项更正 您的XML将成功验证:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="recipes">
        <xs:complexType>
          <xs:sequence>    
            <xs:element ref="recipe" maxOccurs="unbounded"/>
          </xs:sequence>   
        </xs:complexType>
      </xs:element>
      <xs:element name="recipe">
        <xs:complexType>
          <xs:sequence> 
            <xs:element name="name" type="xs:string"/>
            <xs:element name="author" type="xs:string"/>
            <xs:element name="Magazine_subscription" type="xs:string"/>
            <xs:element name="preparation_time" type="xs:string"/>
            <xs:element name="cooking_time" type="xs:string"/>
            <xs:element name="additional_time" type="xs:string" nillable="true"/>
            <xs:element name="serves" type="xs:string"/>
            <xs:element name="level">
              <xs:simpleType>
                <xs:restriction base="xs:string">   
                  <xs:enumeration value="Easy"/>
                  <xs:enumeration value="Moderately easy"/>
                  <xs:enumeration value="For the keen cook"/>
                </xs:restriction>   
              </xs:simpleType>
            </xs:element>   
            <xs:element name="overview" type="xs:string"/>
            <xs:element name="nutrition">
              <xs:complexType>
                <xs:sequence> 
                  <xs:element name="kcalories" type="xs:string"/>
                  <xs:element name="fat" type="xs:string"/>
                  <xs:element name="saturates" type="xs:string"/>
                  <xs:element name="carbs" type="xs:string"/>
                  <xs:element name="sugars" type="xs:string"/>
                  <xs:element name="fibre" type="xs:string"/>
                  <xs:element name="protein" type="xs:string"/>
                  <xs:element name="salt" type="xs:string"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="ingredients">
              <xs:complexType>
                <xs:sequence maxOccurs="unbounded">   
                  <xs:element name="ingredient" type="xs:string"/>
                  <xs:element name="quantity" type="xs:string" nillable="true"/>
                  <xs:element name="process" type="xs:string" nillable="true"/>
                </xs:sequence>  
              </xs:complexType>
            </xs:element>
            <xs:element name="method">
              <xs:complexType>
                <xs:sequence>   
                  <xs:element name="step" type="xs:string" maxOccurs="unbounded" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>   
          </xs:sequence>
          <xs:attribute name="id" type="xs:string"/>
        </xs:complexType>
      </xs:element> 
    </xs:schema>