简体   繁体   中英

Why isn't my XML validating with the schema?

So I am trying to validate the XML with the schema but I keep getting the error:

"S4s-elt-invalid-content.1: The Content Of 'ingredients_type' Is Invalid. Element 'element' Is Invalid, Misplaced, Or Occurs Too Often."

This is my XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<?xml-stylesheet type="text/xsl" href="recipes.css" ?>

<recipes>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNameSpaceLocation="/recipes.xsd"

    <recipe id="1">
        <recipe_name>Gooseberry crème brûlée tart</recipe_name>

      <prep_time>10</prep_time>

        <cook_time>80</cook_time>

        <difficulty>More Effort</difficulty>

            <ingredient name="gooseberries" amount="450g" />
            <ingredient name="white caster sugar" amount="200g" />
            <ingredient name="eggs" amount="4" />
            <ingredient name="double cream" amount="100ml" />
            <ingredient name="block sweet pastry" amount="500g" />
            <ingredient name="flour" amount="for dusting" />

        <servings>8</servings>
    </recipe>

    <recipe id="2">
        <recipe_name>One-pan jerk roast chicken</recipe_name>

        <prep_time>30</prep_time>

        <cook_time>130</cook_time>

        <difficulty>easy</difficulty>

            <ingredient name="whole chicken" amount="1.5kg" />
            <ingredient name="red onions" amount="2" prep_info="then cut in to wedges leaving the root intact"/>
            <ingredient name="red peppers" amount="2" prep_info="deseeded and chopped in to chunks"/>
            <ingredient name="sweet potatoes" amount="4" prep_info="peeled and cut into chunks"/>
            <ingredient name="can black beans" amount="400 grams" prep_info="drained and rinsed"/>
            <ingredient name="coconut milk" amount="400ml" />
            <ingredient name="cooked rice" prep_info="to serve"/>
            <ingredient name="flatbreads" prep_info="to serve"/>

        <servings>4-6</servings>
    </recipe>

    <recipe id="3">
        <recipe_name>Roast sirloin of beef</recipe_name>

        <prep_time>10</prep_time>

        <cook_time>150</cook_time>

        <difficulty>easy</difficulty>

            <ingredient name="vegetable oil or beef fat" amount="2 tbsp" />
            <ingredient name="sirloin of beef joint" amonut="1-1.5 kg" />
            <ingredient name="red wine" amount="1" unit="glass" />
            <ingredient name="can beef consommé" amount="400g" />

        <servings>6</servings>
    </recipe>

    <recipe id="4">
        <recipe_name>Haddock &amp; leek au gratin with sweetcorn mash</recipe_name>

        <prep_time>10</prep_time>

        <cook_time>50</cook_time>

        <difficulty>easy</difficulty>

            <ingrdient name="potatoes" amount="350g" prep_info="quartered" />
            <ingredient name="can sweetcorn in water" amount="195g" />
            <ingredient name="bag ready washed spinach" amount="240g" />
            <ingredient name="leeks" amount="2" prep_info="thickly sliced" />
            <ingredient name="skimmed milk" amount="300ml" prep_info="plus 3 tbsp" />
            <ingredient name="unsalted butter" amount="15g" />
            <ingredient name="plain flour" amount="15g" />
            <ingredient name="English mustard" amount="0.5tsp" />
            <ingredient name="mature reduced-fat cheese" amount="75g" prep_info="grated" />
            <ingredient name="fillets of skinless haddock" amount="2* 125g" />

        <servings>4</servings>
    </recipe>

    <recipe id="5">
        <recipe_name>Cheesy mushroom omelette</recipe_name>

        <prep_time>5</prep_time>

        <cook_time>10</cook_time>

        <difficulty>easy</difficulty>

            <ingredient name="olive oil" amount="1tbsp" />
            <ingredient name="button or chestnut mooshrooms" amount="handful" />
            <ingredient name="vegetarian cheddar" amount="25g" prep_info="grated" />
            <ingredient name="parsley leaves" amount="handful" prep_info="roughly chopped" />
            <ingredient name="eggs" amount="2" prep_info="beaten" />

        <servings>2</servings>
    </recipe>

    <recipe id="6">
        <recipe_name>Chorizo bean burgers</recipe_name>

        <prep_time>10</prep_time>

        <cook_time>16</cook_time>

        <difficulty>easy</difficulty>

            <ingredient name="mini cook chorizo" amount="100g" prep_info="skins removed" />
            <ingredient name="port sausages" amount="3" prep_info="squeezed from their skins" />
            <ingredient name="smoked paprika" amount="0.25tsp" />
            <ingredient name="lime juice" amount="1tbsp" prep_info="plus wedges to serve" />
            <ingredient name="coriander" prep_info="small bunch" />
            <ingredient name="can mixed beans" amount="200g" prep_info="half a can" />
            <ingredient name="egg" amount="1" prep_info="lightly whisked" />
            <ingredient name="olive olive" amount="1.5tbsp" prep_info="plus extra to drizzle" />
            <ingredient name="ciabatta rolls" amount="2" prep_info="split and toasted" />
            <ingredient name="soured cream, avocado, small handful rocket and chips" prep_info="to serve (optional)" />

        <servings>2</servings>
    </recipe>

    <recipe id="7">
        <recipe_name>Cherry bruschetta</recipe_name>

        <prep_time>5</prep_time>

        <cook_time>4</cook_time>

        <difficulty>easy</difficulty>

            <ingredient name="ciabatta" amount="1 loaf" />
            <ingredient name="extra virgin olive oil" />
            <ingredient name="ricotta" amount="100g" />
            <ingredient name="proscuitto" amount="80g" />

        <servings>12</servings>
    </recipe>

    <recipe id="8">
        <recipe_name>Hoisin mackerel pancakes</recipe_name>

        <prep_time>15</prep_time>

        <cook_time>5</cook_time>

        <difficulty>easy</difficulty>

            <ingredient name="mackerel fillets" amount="3" prep_info="all bones removed, cut into finger-length strips" />
            <ingredient name="hoisin sauce" amount="2tbsp" prep_info="plus extra for dipping" />
            <ingredient name="spring onions" amount="4" />
            <ingredient name="cucumber" amount="0.5" />
            <ingredient name="little gem lettuces" amount="2" />
            <ingredient name="vegetable oil" amount="1tbsp" />
            <ingredient name="chinese pancakes" amount="10" prep_info="(available from Waitrose, Asian Supermarkets or Chinese Takeaways)" />

        <servings>4</servings>
    </recipe>

    <recipe id="9">
        <recipe_name>Fettuccine alfredo</recipe_name>

        <prep_time>15</prep_time>

        <cook_time>10</cook_time>

      <difficulty>easy</difficulty>
            <ingredient name="clotted cream" amount="227g" />
            <ingredient name="butter" amount="25g" prep_info="(about 2 tbsp)" />
            <ingredient name="cornflour" amount="1tsp" />
            <ingredient name="parmesan" amount="100g" prep_info="grated" />
            <ingredient name="freshly grated nutmeg" />
            <ingredient name="fresh fettuccine or tagliatelle" amount="250" />

        <servings>
        </servings>
    </recipe>

    <recipe id="10">
        <recipe_name>Chicken &amp; mushroom puff pie</recipe_name>

        <prep_time>45</prep_time>

        <cook_time>30</cook_time>

        <difficulty>easy</difficulty>

            <ingredient name="vegetable oil" amount="1 tbsp" />
            <ingredient name="skinless boneless chicken thighs" amount="8" />
            <ingredient name="smoked streaky bacon rashers" amount="8" prep_info="cut in to large pieces" />
            <ingredient name="onion" amount="0.5" prep_info="halved and sliced" />
            <ingredient name="baby button mushrooms" amount="250g" />
            <ingredient name="handful thyme sprigs" />
            <ingredient name="plain flour" amount="2 tbsp" />
            <ingredient name="chicken stock" amount="400ml" />
            <ingredient name="milk" amount="200ml" />
            <ingredient name="puff pastry" amount="500g" prep_info="fresh or frozen and defrosted" />
            <ingredient name="egg" amount="1" prep_info="beaten" />

        <servings>
        </servings>
    </recipe>
</recipes>

and this is the XML Schema:

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

<xs:complexType name="ingredients_type">
    <xs:element name="name" type="xs:string" />
    <xs:element name="amount" type="xs:string" />
    <xs:element name="unit" type="xs:string" />
    <xs:element name="prep_info" type="xs:string" />
</xs:complexType>

<xs:complexType name="recipes_type">
    <xs:sequence>
        <xs:element name="recipe_name" type="xs:string" />
        <xs:element name="prep_time" type="xs:positiveInteger" />
        <xs:element name="cook_time" type="xs:positiveInteger" />
        <xs:element name="difficulty" type="xs:string" />
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="Easy" />
                    <xs:enumeration value="Moderate" />
                    <xs:enumeration value="Difficult" />
                </xs:restriction>
            </xs:simpleType>
        <xs:element name="ingredient" type="ingredients_type" />
        <xs:element name="servings" type="xs:string" />
    </xs:sequence>
        <xs:attribute name="id" type="xs:positiveInteger" />
</xs:complexType>

<xs:element name="recipes">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="recipe" type="recipes_type" minOccurs="1" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

Both your XML and XSD have so many problems that I stopped keeping track, but here are some of the issues:

  1. xs:element cannot appear as a child of xs:complexType . One correction is to add the xs:element s to xs:sequence .

  2. It's xsi:noNamespaceSchemaLocation , not xsi:noNameSpaceLocation .

  3. Don't declare components as elements when they're attributes in the XML.

  4. Watch out for spelling, typos, and case issues.

Be aware that when both the XML and the XSD have to be fixed, there's ambiguity as to what was intended in the first place. The below XSD now successfully validates the below XML, but be sure to check what's been fixed to be sure it agrees with your intent.

XML

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<?xml-stylesheet type="text/xsl" href="recipes.css" ?>
<recipes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="try.xsd">
  <recipe id="1">
    <recipe_name>Gooseberry crème brûlée tart</recipe_name>
    <prep_time>10</prep_time>
    <cook_time>80</cook_time>
    <difficulty>Moderate</difficulty>
    <ingredient name="gooseberries" amount="450g" />
    <ingredient name="white caster sugar" amount="200g" />
    <ingredient name="eggs" amount="4" />
    <ingredient name="double cream" amount="100ml" />
    <ingredient name="block sweet pastry" amount="500g" />
    <ingredient name="flour" amount="for dusting" />
    <servings>8</servings>
  </recipe>
  <recipe id="2">
    <recipe_name>One-pan jerk roast chicken</recipe_name>
    <prep_time>30</prep_time>
    <cook_time>130</cook_time>
    <difficulty>Easy</difficulty>
    <ingredient name="whole chicken" amount="1.5kg" />
    <ingredient name="red onions" amount="2" prep_info="then cut in to wedges leaving the root intact"/>
    <ingredient name="red peppers" amount="2" prep_info="deseeded and chopped in to chunks"/>
    <ingredient name="sweet potatoes" amount="4" prep_info="peeled and cut into chunks"/>
    <ingredient name="can black beans" amount="400 grams" prep_info="drained and rinsed"/>
    <ingredient name="coconut milk" amount="400ml" />
    <ingredient name="cooked rice" prep_info="to serve"/>
    <ingredient name="flatbreads" prep_info="to serve"/>
    <servings>4-6</servings>
  </recipe>
  <recipe id="3">
    <recipe_name>Roast sirloin of beef</recipe_name>
    <prep_time>10</prep_time>
    <cook_time>150</cook_time>
    <difficulty>Easy</difficulty>
    <ingredient name="vegetable oil or beef fat" amount="2 tbsp" />
    <ingredient name="sirloin of beef joint" amount="1-1.5 kg" />
    <ingredient name="red wine" amount="1" unit="glass" />
    <ingredient name="can beef consommé" amount="400g" />
    <servings>6</servings>
  </recipe>
  <recipe id="4">
    <recipe_name>Haddock &amp; leek au gratin with sweetcorn mash</recipe_name>
    <prep_time>10</prep_time>
    <cook_time>50</cook_time>
    <difficulty>Easy</difficulty>
    <ingredient name="potatoes" amount="350g" prep_info="quartered" />
    <ingredient name="can sweetcorn in water" amount="195g" />
    <ingredient name="bag ready washed spinach" amount="240g" />
    <ingredient name="leeks" amount="2" prep_info="thickly sliced" />
    <ingredient name="skimmed milk" amount="300ml" prep_info="plus 3 tbsp" />
    <ingredient name="unsalted butter" amount="15g" />
    <ingredient name="plain flour" amount="15g" />
    <ingredient name="English mustard" amount="0.5tsp" />
    <ingredient name="mature reduced-fat cheese" amount="75g" prep_info="grated" />
    <ingredient name="fillets of skinless haddock" amount="2* 125g" />
    <servings>4</servings>
  </recipe>
  <recipe id="5">
    <recipe_name>Cheesy mushroom omelette</recipe_name>
    <prep_time>5</prep_time>
    <cook_time>10</cook_time>
    <difficulty>Easy</difficulty>
    <ingredient name="olive oil" amount="1tbsp" />
    <ingredient name="button or chestnut mooshrooms" amount="handful" />
    <ingredient name="vegetarian cheddar" amount="25g" prep_info="grated" />
    <ingredient name="parsley leaves" amount="handful" prep_info="roughly chopped" />
    <ingredient name="eggs" amount="2" prep_info="beaten" />
    <servings>2</servings>
  </recipe>
  <recipe id="6">
    <recipe_name>Chorizo bean burgers</recipe_name>
    <prep_time>10</prep_time>
    <cook_time>16</cook_time>
    <difficulty>Easy</difficulty>
    <ingredient name="mini cook chorizo" amount="100g" prep_info="skins removed" />
    <ingredient name="port sausages" amount="3" prep_info="squeezed from their skins" />
    <ingredient name="smoked paprika" amount="0.25tsp" />
    <ingredient name="lime juice" amount="1tbsp" prep_info="plus wedges to serve" />
    <ingredient name="coriander" prep_info="small bunch" />
    <ingredient name="can mixed beans" amount="200g" prep_info="half a can" />
    <ingredient name="egg" amount="1" prep_info="lightly whisked" />
    <ingredient name="olive olive" amount="1.5tbsp" prep_info="plus extra to drizzle" />
    <ingredient name="ciabatta rolls" amount="2" prep_info="split and toasted" />
    <ingredient name="soured cream, avocado, small handful rocket and chips" prep_info="to serve (optional)" />
    <servings>2</servings>
  </recipe>
  <recipe id="7">
    <recipe_name>Cherry bruschetta</recipe_name>
    <prep_time>5</prep_time>
    <cook_time>4</cook_time>
    <difficulty>Easy</difficulty>
    <ingredient name="ciabatta" amount="1 loaf" />
    <ingredient name="extra virgin olive oil" />
    <ingredient name="ricotta" amount="100g" />
    <ingredient name="proscuitto" amount="80g" />
    <servings>12</servings>
  </recipe>
  <recipe id="8">
    <recipe_name>Hoisin mackerel pancakes</recipe_name>
    <prep_time>15</prep_time>
    <cook_time>5</cook_time>
    <difficulty>Easy</difficulty>
    <ingredient name="mackerel fillets" amount="3" prep_info="all bones removed, cut into finger-length strips" />
    <ingredient name="hoisin sauce" amount="2tbsp" prep_info="plus extra for dipping" />
    <ingredient name="spring onions" amount="4" />
    <ingredient name="cucumber" amount="0.5" />
    <ingredient name="little gem lettuces" amount="2" />
    <ingredient name="vegetable oil" amount="1tbsp" />
    <ingredient name="chinese pancakes" amount="10" prep_info="(available from Waitrose, Asian Supermarkets or Chinese Takeaways)" />
    <servings>4</servings>
  </recipe>
  <recipe id="9">
    <recipe_name>Fettuccine alfredo</recipe_name>
    <prep_time>15</prep_time>
    <cook_time>10</cook_time>
    <difficulty>Easy</difficulty>
    <ingredient name="clotted cream" amount="227g" />
    <ingredient name="butter" amount="25g" prep_info="(about 2 tbsp)" />
    <ingredient name="cornflour" amount="1tsp" />
    <ingredient name="parmesan" amount="100g" prep_info="grated" />
    <ingredient name="freshly grated nutmeg" />
    <ingredient name="fresh fettuccine or tagliatelle" amount="250" />
    <servings>
    </servings>
  </recipe>
  <recipe id="10">
    <recipe_name>Chicken &amp; mushroom puff pie</recipe_name>
    <prep_time>45</prep_time>
    <cook_time>30</cook_time>
    <difficulty>Easy</difficulty>
    <ingredient name="vegetable oil" amount="1 tbsp" />
    <ingredient name="skinless boneless chicken thighs" amount="8" />
    <ingredient name="smoked streaky bacon rashers" amount="8" prep_info="cut in to large pieces" />
    <ingredient name="onion" amount="0.5" prep_info="halved and sliced" />
    <ingredient name="baby button mushrooms" amount="250g" />
    <ingredient name="handful thyme sprigs" />
    <ingredient name="plain flour" amount="2 tbsp" />
    <ingredient name="chicken stock" amount="400ml" />
    <ingredient name="milk" amount="200ml" />
    <ingredient name="puff pastry" amount="500g" prep_info="fresh or frozen and defrosted" />
    <ingredient name="egg" amount="1" prep_info="beaten" />
    <servings>
    </servings>
  </recipe>
</recipes>

XSD

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="ingredients_type">
    <xs:sequence/>
    <xs:attribute name="name" type="xs:string" />
    <xs:attribute name="amount" type="xs:string" />
    <xs:attribute name="unit" type="xs:string" />
    <xs:attribute name="prep_info" type="xs:string" />
  </xs:complexType>
  <xs:complexType name="recipes_type">
    <xs:sequence>
      <xs:element name="recipe_name" type="xs:string" />
      <xs:element name="prep_time" type="xs:positiveInteger" />
      <xs:element name="cook_time" type="xs:positiveInteger" />
      <xs:element name="difficulty">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="Easy" />
            <xs:enumeration value="Moderate" />
            <xs:enumeration value="Difficult" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="ingredient" type="ingredients_type" maxOccurs="unbounded"/>
      <xs:element name="servings" type="xs:string" />
    </xs:sequence>
    <xs:attribute name="id" type="xs:positiveInteger" />
  </xs:complexType>
  <xs:element name="recipes">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="recipe" type="recipes_type" minOccurs="1" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs: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