XSD for XML 具有命名空间和不组合命名空间元素 [英] XSD for XML with namespaced and no namespace elements combined
问题描述
我收到了这种 XML:
I receive this kind of XML:
<?xml version="1.0" encoding="utf-8"?>
<root-element xmlns="SomeSpecificNameSpace">
<some-elements />
<some-other-elements/>
<some-other-other-element/>
<element-with-empty-namespace xmlns=""/>
</root-element>
我必须检查我是否拥有这个 element-with-empty-namespace
,但我不关心内容,所以我尝试了以下操作:
I must make a check that I've this element-with-empty-namespace
, but that I don't care about the content, so I tried the following:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="SomeSpecificNameSpace"
elementFormDefault="qualified"
xmlns="SomeSpecificNameSpace"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MyRootElement">
<xs:complexType>
<xs:sequence>
<!-- Others types here -->
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="element-with-empty-namespace" minOccurs="0" maxOccurs="1" xmlns="">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
</xs:schema
但它似乎不接受我的元素,说它需要一个命名空间为 SomeSpecificNameSpace
的元素.
but it seems it doesn't accept my element, saying that it is expecting an element with the namespace SomeSpecificNameSpace
.
我知道这个结构有点奇怪,但是这个 element-with-empty-namespace
是我们通过 XML 提供的一个值".
I know that this structure is a little bit weird, but this element-with-empty-namespace
is one "value" we provide through XML.
推荐答案
多个命名空间需要多个 XSD.
为了支持您的 XML 输入结构,
To support your XML input structure,
<?xml version="1.0" encoding="utf-8"?>
<root-element xmlns="SomeSpecificNameSpace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="SomeSpecificNameSpace try.xsd">
<some-elements />
<some-other-elements/>
<some-other-other-element/>
<element-with-empty-namespace xmlns=""/>
</root-element>
将您的 XSD 分成两个文件,如下所示...
break your XSD into two files as follows...
在管理 SomeSpecificNameSpace
命名空间的主 XSD 文件中,使用 xs:import
,
In the main XSD file, which governs the SomeSpecificNameSpace
namespace, use xs:import
,
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="SomeSpecificNameSpace"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="element-with-empty-namespace.xsd"/>
<xs:element name="root-element">
<xs:complexType>
<xs:sequence>
<xs:element name="some-elements"/>
<xs:element name="some-other-elements"/>
<xs:element name="some-other-other-element"/>
<xs:element ref="element-with-empty-namespace"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
并导入第二个 XSD,
and import a second XSD,
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="element-with-empty-namespace">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
</xs:schema>
将 element-with-empty-namespace
元素存放在无命名空间中.
that houses the element-with-empty-namespace
element in no namespace.
这篇关于XSD for XML 具有命名空间和不组合命名空间元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!