xml 命名空间“xSchema”中的元素“x”在命名空间“xSchema”中具有无效的子元素“y”。预期的可能元素列表:“y”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21009632/
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 11:58:18  来源:igfitidea点击:

The element "x" in namespace "xSchema" has invalid child element "y" in namespace "xSchema". List of possible elements expected: "y"

xmlnamespacesxml-validation

提问by user2843457

I am writing a schema for my XML validation final, and finally got everything (just about) working. But now I'm getting the strangest error in my XML. I'll start by showing my schema since that's where the problem should be located yet it gives me no errors.

我正在为我的 XML 验证最终编写一个模式,最后让一切(几乎)正常工作。但是现在我在我的 XML 中遇到了最奇怪的错误。我将首先展示我的架构,因为那是问题所在的位置,但它没有给我任何错误。

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:os="OrdersSchema"
        targetNamespace="OrdersSchema"
        elementFormDefault="unqualified"
        attributeFormDefault="qualified">
  <element name="orders"    type="os:orders"/>
  <complexType name="orders">
    <sequence>
      <element name="order" type="os:order" maxOccurs="unbounded"/>
    </sequence>
  </complexType>
  <complexType name="order">
    <sequence>
      <element name="deliveryAddress">
        <complexType>
          <sequence>
            <element name='line1' type='os:lineType'/>
            <element name='line2' type='os:lineType'/>
            <element name='line3' type='os:lineType'/>
            <element name='line4' type='os:lineType' minOccurs='0'/>
          </sequence>
          <attribute name="orderId" type="string" use="required" >
          </attribute>
          <attribute name="type" type="os:typeType" use="required"/>
        </complexType>
        <unique name="uniqueOrderIdPerOrder">
          <selector xpath="os:order"/>
          <field xpath="orderId"/>
        </unique>
      </element>
      <element name='items'>
        <complexType>
          <attribute name='productId' type='os:productIdType'/>
          <attribute name='quantity'>
            <simpleType>
              <restriction base='positiveInteger'>
              </restriction>
            </simpleType>
          </attribute>
        </complexType>
      </element>
      <element name='note' minOccurs='0' type='string'/>
    </sequence>
  </complexType>

  <simpleType name='lineType'>
    <restriction base='string'>
      <minLength value='1'/>
    </restriction>
  </simpleType>
  <simpleType name='typeType'>
    <restriction base='string'>
      <enumeration value='standard'/>
      <enumeration value='express'/>
    </restriction>
  </simpleType>
  <simpleType name='productIdType'>
    <restriction base='string'>
      <pattern value='(?i)p[-\s](150|1[0-4][0-9]|[[1-9][0-9]|[1-9])\.[a-z][a-z][a-z][a-z][a-z][a-z]'/>
      <pattern value='A...+[$XYZ]\b'/>
    </restriction>
  </simpleType>



</schema>

Now, my schema gives no errors at all, but when I link my XML file to it, I get an error saying: The element 'orders' in namespace 'OrdersSchema' has invalid child element 'order' in namespace 'OrdersSchema'. List of possible elements expected: 'order'.

现在,我的架构根本没有给出任何错误,但是当我将 XML 文件链接到它时,我收到一条错误消息:命名空间“OrdersSchema”中的元素“orders”在命名空间“OrdersSchema”中的子元素“order”无效。预期的可能元素列表:'order'。

Here's a code that should validate, if anyone wants to try it out:

这是一个应该验证的代码,如果有人想尝试的话:

<?xml version="1.0" encoding="utf-8" ?>
<os:orders xmlns:os="OrdersSchema">
  <os:order orderId="ord0001" type="standard">
    <deliveryAddress>
      <line1>5, Elmstreet</line1>
      <line2>90210 Beverly Hills</line2>
      <line3>California</line3>
    </deliveryAddress>
    <items>
      <item productId="P 150.aabaac" quantity="5" />
    </items>
    <note>Deliver after 5 pm.</note>
  </os:order>
  <os:order orderId="ord0003" type="express">
    <deliveryAddress>
      <line1>Voskenslaan 30</line1>
      <line2>BE9000 Gent</line2>
      <line3>Oost-Vlaanderen</line3>
      <line4>Belgium</line4>
    </deliveryAddress>
    <items>
      <item productId="A3546sdfsdf6546sdf654Z" quantity="10" />
      <item productId="p 149.SLKDOZ" quantity="5" />
      <item productId="P 100.xcvZEr" quantity="15" />
    </items>
  </os:order>
  <os:order orderId="ord0002" type="express">
    <deliveryAddress>
      <line1>Voskenslaan 32</line1>
      <line2>BE9000 Gent</line2>
      <line3>Oost-Vlaanderen</line3>
      <line4>Belgium</line4>
    </deliveryAddress>
    <items>
      <item productId="P-99.ruioze" quantity="15" />
      <item productId="A123qze46548X" quantity="50" />
      <item productId="P 1.sqmfze" quantity="1" />
      <item productId="AoknY" quantity="20" />
    </items>
    <note>This is <b>very urgent</b> !</note>
  </os:order>
</os:orders>

采纳答案by keshlam

The element 'orders' in namespace 'OrdersSchema' 
has invalid child element 'order' in namespace 'OrdersSchema'. 
List of possible elements expected: 'order'. 

In other words, it thinks your schema told the parser to expect a non-namespaced <order>child element, and you provided a namespaced <os:order>in the instance document.

换句话说,它认为您的架构告诉解析器需要一个非命名空间的<order>子元素,并且您<os:order>在实例文档中提供了一个命名空间。

http://www.w3.org/TR/xmlschema-0/#NS

http://www.w3.org/TR/xmlschema-0/#NS

回答by Michael Kay

Your use of

您的使用

elementFormDefault="unqualified"
attributeFormDefault="qualified"

is highly unusual, and is probably the source of the trouble. Unless you really want to do something unusual, these should be the other way around.

非常不寻常,可能是问题的根源。除非你真的想做一些不寻常的事情,否则这些应该是相反的。