XML模式cvc-complex-type

时间:2018-09-24 23:33:04

标签: xml xsd namespaces

我一直在尝试解决此问题,目前我不确定我是否弄乱了导致问题的名称空间,或者代码的语法有问题,但是itemName的所有实例都声明相同,并且都已正确关闭。

试图找出此错误:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'itemName'. One of '{itemName}' is expected.

我的xsd文件:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://example.com/chestershartland/recipe"
xmlns="http://example.com/chestershartland/recipe"
>


        <xs:element name="recipe">
           <xs:complexType>
              <xs:sequence>
                <xs:element name="itemName" type="xs:string" />
                 <xs:element name="ingredients">
                   <xs:complexType>
                     <xs:sequence>
                        <xs:element name="ingredient" type="xs:string" />
                        <xs:element name="ingredient" type="xs:string" />
                        <xs:element name="ingredient" type="xs:string" />
                     </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                  <xs:element name="directions" type="xs:string" />
                </xs:sequence>
            </xs:complexType>

       </xs:element>

XML代码是:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<recipe 
xmlns="http://example.com/chestershartland/recipe"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://example.com/chestershartland/recipe recipe.xsd"
>

  <itemName>Oatmeal Breakfast</itemName>
  <ingredients>
     <ingredient>1/3 c steel cut oats</ingredient>
     <ingredient>1-1/4 c water</ingredient>
     <ingredient>1/4 t salt</ingredient>
  </ingredients>
  <directions>
  <![CDATA[Bring water to a boil. Add salt and oats, stir, and lower heat
     to lowest setting. Cover and let stand 2 hours.
  ]]>
  </directions>
</recipe>

1 个答案:

答案 0 :(得分:0)

避免使用括号,并使用*表示在容器元素上出现0到n次:

...
<!ELEMENT images (img+)>
<!ELEMENT img EMPTY>
...

要提出的问题是:其元素内容为空,但仍可以具有属性。