【问题标题】:Why isn't my XML validating with the schema?为什么我的 XML 不使用架构进行验证?
【发布时间】:2020-01-13 17:26:26
【问题描述】:

所以我正在尝试使用架构验证 XML,但我不断收到错误:

“S4s-elt-invalid-content.1: 'ingredients_type' 的内容无效。元素 'element' 无效、放错位置或过于频繁。”

这是我的 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>

这是 XML 架构:

<?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>

【问题讨论】:

    标签: xml xsd xsd-validation xml-validation


    【解决方案1】:

    您的 XML 和 XSD 都有很多问题,我不再跟踪了,但这里有一些问题:

    1. xs:element 不能作为 xs:complexType 的子项出现。一项更正是将xs:elements 添加到xs:sequence

    2. xsi:noNamespaceSchemaLocation,不是xsi:noNameSpaceLocation

    3. 当组件是 XML 中的属性时,不要将它们声明为元素。

    4. 注意拼写、拼写错误和大小写问题。

    请注意,当必须修复 XML 和 XSD 时,最初的意图是不明确的。下面的 XSD 现在成功地验证了下面的 XML,但请务必检查已修复的内容以确保它符合您的意图。

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多