【问题标题】:XML Schema cvc-complex-typeXML Schema cvc-complex-type
【发布时间】:2023-03-09 19:20:01
【问题描述】:

我一直在试图解决这个问题,此时我不确定我是否弄乱了导致问题的命名空间,或者代码的语法是否有问题,但 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>

【问题讨论】:

    标签: xml xsd namespaces


    【解决方案1】:

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

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

    对于你的问题:它的元素内容是空的,但它仍然可以有属性。

    【讨论】:

    • 当我这样做时,错误是“元素类型“图像”的内容必须观看“(img)”。
    • 这就是您声明的内容 - 检查我编辑的答案以进行修复
    • 如果它仍然不是你想要的,如果你也分享一个示例 XML 会有所帮助
    • 用户说“我必须创建图像元素,包含*一个或多个* img 元素...”所以你想使用+发生指标;不是*
    猜你喜欢
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    相关资源
    最近更新 更多