当试图从XML模式中导入共享定义,我可以正确地引用共享类型,但引用共享元素导致验证错误。

这是架构即进口共享定义(example.xsd):

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

    <xs:import namespace="http://shared.com" schemaLocation="shared.xsd"/>

    <xs:element name="example">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="importedElement"/>
                <xs:element ref="importedType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="importedElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="shared:fooElement"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="importedType">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="bar" type="shared:barType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

这些是共享定义(shared.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://shared.com"
    targetNamespace="http://shared.com">

    <xs:element name="fooElement">
        <xs:simpleType>
            <xs:restriction base="xs:integer"/>
        </xs:simpleType>
    </xs:element>

    <xs:simpleType name="barType">
        <xs:restriction base="xs:integer"/>
    </xs:simpleType>

</xs:schema>

现在考虑这个XML实例:

<?xml version="1.0"?>
<example
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                     
  xsi:noNamespaceSchemaLocation="example.xsd">
    <importedElement>
        <fooElement>42</fooElement>
    </importedElement>
    <importedType>
        <bar>42</bar>
    </importedType>
</example>

当验证时,“importedType”工作完全正常,但“importedElement”提供了以下错误:

  

无效内容被发现与起始元件“fooElement”。一个 '{ “ http://shared.com ”:fooElement}' 预期

我猜想,我的烦恼都与命名空间的问题(因此在某种程度上误导“了fooElement但期待fooElement”) - 什么是错在这里任何提示

有帮助吗?

解决方案

您正在引用fooElement,就好像是在没有命名空间,您需要使用正确的名称空间的实例文档:

<?xml version="1.0"?>
<example
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                     
  xsi:noNamespaceSchemaLocation="example.xsd" xmlns:shared="http://shared.com">
    <importedElement>
        <shared:fooElement>42</shared:fooElement>
    </importedElement>
    <importedType>
        <bar>42</bar>
    </importedType>
</example>

修改我应该指出:这之间的差异类型元素;只有后者出现在文件(有一些例外),这就是为什么你导入的类型作品你想,你的元素没有。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top