无法解析“MyType”元素“Test”的类型定义

发布于 2025-01-09 21:17:25 字数 1524 浏览 5 评论 0原文

我正在尝试使用给定的 XSD 验证 XML。但我不断收到以下错误。

cvc-elt.4.2:无法将“MyType”解析为元素的类型定义 “测试”。

XML

<notifications xmlns="http://soap.sforce.com/2005/09/outbound">
  <Notification>
    <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="MyType">
       <Id>a2L1g000000OzM7EAK</Id>
     </Test>
  </Notification>
</notifications>

XSD

<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://soap.sforce.com/2005/09/outbound" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="notifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Notification">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Test">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Id" type="xs:string" />
                  </xs:sequence>
                  <xs:anyAttribute processContents="skip"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I'm trying to validate a XML with a given XSD. But I'm continuously getting the following error.

cvc-elt.4.2: Cannot resolve 'MyType' to a type definition for element
'Test'.

XML

<notifications xmlns="http://soap.sforce.com/2005/09/outbound">
  <Notification>
    <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="MyType">
       <Id>a2L1g000000OzM7EAK</Id>
     </Test>
  </Notification>
</notifications>

XSD

<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://soap.sforce.com/2005/09/outbound" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="notifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Notification">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Test">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Id" type="xs:string" />
                  </xs:sequence>
                  <xs:anyAttribute processContents="skip"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花坊 2025-01-16 21:17:25

xsi:type 给出的类型必须可以从 XSD 中指定的类型派生,但您的 XSD 甚至没有定义 MyType 类型。

全局定义一个 MyType 类型,并确保它可以从 Test 元素的类型派生。

另请参阅


根据 OP 评论更新:

您能否告诉我我的 XSD 需要如何更新?我真的不关心这个类型属性。所以只是想让 XSD 验证成功。请注意,我无法更改 XML 结构,因为它来自外部系统。

此 XSD 对于未更改的 XML 是有效的:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           targetNamespace="http://soap.sforce.com/2005/09/outbound"
           xmlns:ob="http://soap.sforce.com/2005/09/outbound"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="notifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Notification">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Test" type="ob:MyType"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="MyType">
    <xs:sequence>
      <xs:element name="Id" type="xs:string" />
    </xs:sequence>
    <xs:anyAttribute processContents="skip"/>
  </xs:complexType>
</xs:schema>

请注意,上述 XSD 使 XML 中的 xsi:type 属性变得多余。演示 xsi:type 用法的示例 XSD 如下:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           targetNamespace="http://soap.sforce.com/2005/09/outbound"
           xmlns:ob="http://soap.sforce.com/2005/09/outbound"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="notifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Notification">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Test" type="ob:TestType"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="TestType">
    <xs:sequence>
      <xs:element name="Id" type="xs:string" />
    </xs:sequence>
    <xs:anyAttribute processContents="skip"/>
  </xs:complexType>
  <xs:complexType name="MyType">
    <xs:complexContent>
      <xs:extension base="ob:TestType"/>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>

对于第二个 XSD,XML 中的 xsi:type 属性导致 Test 的类型 元素从默认的 TestType 更改。这里的效果也是无关紧要的,但可以扩展MyType以不同于TestType。限制是 MyType 仍必须派生自 TestType

另请参阅

The type given by xsi:type has to be derivable from the type specified in the XSD, yet your XSD doesn't even define a MyType type.

Globally define a MyType type and make sure it's derivable from the type of the Test element.

See also


Update per OP comment:

Can you kindly let me know how my XSD needs to be updated? I really don't care about this type attribute. So simply want to make the XSD validation a success. Please note that I can't change the XML structure as it's coming from an external system.

This XSD would be valid against the unchanged XML:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           targetNamespace="http://soap.sforce.com/2005/09/outbound"
           xmlns:ob="http://soap.sforce.com/2005/09/outbound"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="notifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Notification">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Test" type="ob:MyType"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="MyType">
    <xs:sequence>
      <xs:element name="Id" type="xs:string" />
    </xs:sequence>
    <xs:anyAttribute processContents="skip"/>
  </xs:complexType>
</xs:schema>

Note that the above XSD renders the xsi:type attribute in the XML redundant. An example XSD which demonstrates use of xsi:type is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           targetNamespace="http://soap.sforce.com/2005/09/outbound"
           xmlns:ob="http://soap.sforce.com/2005/09/outbound"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="notifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Notification">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Test" type="ob:TestType"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="TestType">
    <xs:sequence>
      <xs:element name="Id" type="xs:string" />
    </xs:sequence>
    <xs:anyAttribute processContents="skip"/>
  </xs:complexType>
  <xs:complexType name="MyType">
    <xs:complexContent>
      <xs:extension base="ob:TestType"/>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>

With this second XSD, the xsi:type attribute in the XML causes the type of the Test element to change from its default of TestType. Here too the effect is inconsequential, but one could expand the MyType to differ from TestType. The constraint is that it MyType must still derive from TestType.

See also

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文