xml 发现以元素“element”开头的无效内容。此时不需要子元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9343438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:09:58  来源:igfitidea点击:

Invalid content was found starting with element 'element'. No child element is expected at this point

xmlvalidationxsd

提问by Malyo

I'm working on my xml skills, but validator errors my XSD at certain deep rooted places. The erorrs are:

我正在研究我的 xml 技能,但是验证器在某些根深蒂固的地方错误地发现了我的 XSD。错误是:

  • 18: 11 cvc-complex-type.2.4.d: Invalid content was found starting with element 'aktor'. No child element is expected at this point.
  • 37: 11 cvc-complex-type.2.4.d: Invalid content was found starting
    with element 'utwor'. No child element is expected at this point.
  • 18: 11 cvc-complex-type.2.4.d: 发现以元素“aktor”开头的无效内容。此时不需要子元素。
  • 37:11 cvc-complex-type.2.4.d:发现
    以元素“utwor”开头的无效内容。此时不需要子元素。

Here's XSD:

这是 XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="filmy">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="film">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="tytul"/>
                        <xs:element name="gatunek"/>
                        <xs:element name="czasTrwania"/>
                        <xs:element name="premiera"/>
                        <xs:element name="produkcja"/>
                        <xs:element name="rezyser"/>
                        <xs:element name="scenariusz"/>

                        <xs:element name="obsada">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="aktor">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="imie"/>
                                                <xs:element name="nazwisko"/>
                                                <xs:element name="dataUrodzenia"/>
                                                <xs:element name="postac"/>
                                            </xs:sequence>
                                        </xs:complexType>
                                    </xs:element>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>

                        <xs:element name="soundtrack">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="utwor">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="wykonawca"/>
                                                <xs:element name="tytulUtworu"/>
                                                <xs:element name="gatunekMuzyczny"/>
                                            </xs:sequence>
                                        </xs:complexType>
                                    </xs:element>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>

Here's XML:

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<filmy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <film>
        <tytul>Fight Club</tytul>
        <gatunek>Thriller, Psychologiczny</gatunek>
        <czasTrwania>2h11m</czasTrwania>
        <premiera>11 luty 1999</premiera>
        <produkcja>Niemcy, USA</produkcja>
        <rezyser>David Fincher</rezyser>
        <scenariusz>Jim Uhls</scenariusz>
        <obsada>
            <aktor>
                <imie>Edward</imie>
                <nazwisko>Norton</nazwisko>
                <dataUrodzenia>1969</dataUrodzenia>
                <postac>Narrator</postac>
            </aktor>
            <aktor>
                <imie>Brad</imie>
                <nazwisko>Pitt</nazwisko>
                <dataUrodzenia>1963</dataUrodzenia>
                <postac>Tyler Durden</postac>
            </aktor>
            <aktor>
                <imie>Helena</imie>
                <nazwisko>Boham Carter</nazwisko>
                <dataUrodzenia>1966</dataUrodzenia>
                <postac>Marla Singer</postac>
            </aktor>
        </obsada>
        <soundtrack>
            <utwor>
                <wykonawca>The Pixies</wykonawca>
                <tytulUtworu>Where is my mind</tytulUtworu>
                <gatunekMuzyczny>Rock</gatunekMuzyczny>
            </utwor>
            <utwor>
                <wykonawca>The Pixies</wykonawca>
                <tytulUtworu>Where is my mind</tytulUtworu>
                <gatunekMuzyczny>Rock</gatunekMuzyczny>
            </utwor>
            <utwor>
                <wykonawca>The Pixies</wykonawca>
                <tytulUtworu>Where is my mind</tytulUtworu>
                <gatunekMuzyczny>Rock</gatunekMuzyczny>
            </utwor>
        </soundtrack>
    </film>
</filmy>

I don't understand the problem, it says no child element is expected, is there a limit for child number? Can i expand it?

我不明白这个问题,它说没有子元素,子元素数量有限制吗?我可以扩展它吗?

I've found answer to my question, elements that could be multiplied, has to have maxOccurs="unbounded" attribute.

我找到了我的问题的答案,可以相乘的元素必须具有 maxOccurs="unbounded" 属性。

回答by Malyo

I've found answer to my question, elements that could be multiplied, has to have maxOccurs="unbounded" attribute.

我找到了我的问题的答案,可以相乘的元素必须具有 maxOccurs="unbounded" 属性。

回答by Rooster

Just adding another answer here as the accepted one didn't completely help me.

只是在这里添加另一个答案,因为接受的答案并没有完全帮助我。

So if you have a structure like the question asker here, you can't put the maxOccurs="unbounded"on the outer most element wrapper as it will be considered a global element.

所以如果你有一个像这里的提问者这样的结构,你不能把 放在maxOccurs="unbounded"最外面的元素包装器上,因为它会被认为是一个全局元素。

You have to instead put it on all the potential elements like so:

你必须把它放在所有潜在的元素上,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="filmy">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="film">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element maxOccurs="unbounded" name="tytul"/>
                        <xs:element maxOccurs="unbounded" name="gatunek"/>
                        <xs:element maxOccurs="unbounded" name="czasTrwania"/>
                        <xs:element maxOccurs="unbounded" name="premiera"/>
                        <xs:element maxOccurs="unbounded" name="produkcja"/>
                        <xs:element maxOccurs="unbounded" name="rezyser"/>
                        <xs:element maxOccurs="unbounded" name="scenariusz"/>

...etc

...等等

回答by dortique

The answer accepted for this question, will indeed solve the problem, but is not the "right" solution.

这个问题接受的答案确实会解决问题,但不是“正确”的解决方案。

The reason, adding unbounded, works, is that the xsd now allow multiple sequences, i.e.

添加 unbounded 有效的原因是 xsd 现在允许多个序列,即

  <filmy>
    <sequence>
      <element_3>
    </sequence>  
    <sequence>
      <element_2>
    <sequence>
    <sequence>
      <element_1>
    <sequence>
  </filmy>

What you SHOULD do to solve the problem, is add the elements of the sequence, to the content, in the correct order, i.e. something like this:

为了解决这个问题,你应该做的是将序列的元素以正确的顺序添加到内容中,即像这样:

  filmy.getContent().add(element1)
  filmy.getContent().add(element2)
  filmy.getContent().add(element3)

which will give you:

这会给你:

  <filmy>
    <sequence>
      <element_1>
      <element_2>
      <element_3>
    <sequence>
  </filmy>

i.e. only ONE sequence, and thus no need for unbounded.

即只有一个序列,因此不需要无界。