代码之家  ›  专栏  ›  技术社区  ›  izb

为什么xsd说我的元素不完整?

  •  5
  • izb  · 技术社区  · 15 年前

    我有一个此表单的XSD:

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.example.org/example"
        xmlns:tns="http://www.example.org/example" elementFormDefault="qualified">
    
        <complexType name="bType">
        </complexType>
    
        <complexType name="aType">
            <choice maxOccurs="unbounded">
                <element name="a" type="tns:aType" />
                <element name="b" type="tns:bType" />
            </choice>
        </complexType>
    
        <element name="topelement">
            <complexType>
                <sequence>
                    <element name="a" type="tns:aType" maxOccurs="1" />
                </sequence>
            </complexType>
        </element>
    </schema>
    

    以及我希望与之匹配的XML文件,例如:

    <?xml version="1.0" encoding="UTF-8"?>
    <topelement xmlns="http://www.example.org/example"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.example.org/example example.xsd ">
      <a> <!-- Error on this line. -->
        <a/>
        <b/>
        <b/>
        <a/>
      </a>
    </topelement>
    

    不幸的是,XSD说这是无效的,错误如下:

    cvc-complex-type.2.4.b: The content of element 'a' is not complete. One of '{"http://www.example.org/example":a, "http://www.example.org/example":b}' is expected.  example.xml line 5
    

    据我所知,我已经做了所有我需要做的事情来完成标签。我用“A”和“B”标签的无限选择填充它。有人能看看出了什么问题吗?

    为了澄清这一点,我希望TopElement下只有一个“A”标记,在它下面是“A”和“B”标记的混合。

    3 回复  |  直到 15 年前
        1
  •  5
  •   Rookie Programmer Aravind    15 年前

    在发表这个答案之前,我没有注意到你自己的答案。不管怎样,我不想浪费我的努力/时间。所以我不会删除这篇文章。除了同样的答案,我还有……写了一些要点,请通过……

    复合型 aType 定义它总是有 <a/> <b/> 作为子元素..它意味着…Where元素 <A/GT; 看来它一定有个孩子 <A/GT; <B/> …这不是真的..根据您的输入XML。

    所以我编写的XSD代码就是为了克服这些错误(注意代码中的“minoccurs”属性)。因为你得不到错误..

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.example.org/example"
        xmlns:tns="http://www.example.org/example" elementFormDefault="qualified">
      <element name="topelement">
        <complexType>
          <sequence>
            <element name="a" type="tns:aType" minOccurs="0" maxOccurs="1" />
          </sequence>
        </complexType>
      </element>
    
    
      <complexType name="bType">
      </complexType>
    
      <complexType name="aType">
        <sequence>
          <choice maxOccurs="unbounded">
            <element name="a" type="tns:aType" minOccurs="0"/>
            <element name="b" type="tns:bType" minOccurs="0"/>
          </choice>
        </sequence>
      </complexType>
    </schema>
    

    所以根据我的代码……标签 <A/GT; 可能有也可能没有任何子元素。
    如果不想更改XSD文件..那么XML必须 <A/> 标签或 <B/GT; 标记为的子级 <A/GT; … 像这样:

    <topelement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.org/example" xsi:schemaLocation="http://www.example.org/example example.xsd">
        <a>
        <a>
          <b/>
        </a>
        <b/>
        <b/>
        <a>
          <a>
           <b/>
          </a>
          <b/>
        </a>
    </topelement>
    

    其中,因为这是无效的:

    <topelement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.org/example" xsi:schemaLocation="http://www.example.org/example example.xsd">
        <a>
        <a>
          <b/>
        </a>
        <a/><!--this is wrong-->
        <b/>
        </a>
    </topelement>
    


    当做: 婴儿专业

        2
  •  3
  •   Andrew Cox    15 年前

    错误在第二个 a 不是第一个第二个 下面需要有一个选择。

        3
  •  1
  •   izb    15 年前

    解决了…错误是误导性的,因为它在抱怨错误的“A”。

    将顶层的“a”重命名为“c”,它仍然抱怨第5行的“a”。

    修复方法是将minoccurs=0添加到choice元素中,这样并非所有“a”元素都需要子元素。