如何引用其他 XSD 的命名空间中的元素?
How to reference element in other XSD's namespace?
我有一个跨两个 .XSD 文件定义的复杂类型。
Parent.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:Parent"
xmlns:emb="urn:Embedded" targetNamespace="urn:Parent" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:import namespace="urn:Embedded" schemaLocation="Embedded.xsd"/>
<xs:element name="ParentType">
<xs:complexType>
<xs:sequence>
<xs:element name="embedded" type="emb:EmbeddedType"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Embedded.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="urn:Embedded" targetNamespace="urn:Embedded"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:complexType name="EmbeddedType">
<xs:sequence>
<xs:element name="numeric" type="xs:int"></xs:element>
<xs:element name="embedded" type="EmbeddedType"
minOccurs="0" maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
我的 XML 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<ParentType xmlns="urn:Parent" xmlns:emb="urn:Embedded">
<embedded>
<emb:numeric>12</emb:numeric>
<emb:embedded>
<emb:numeric>5</emb:numeric>
</emb:embedded>
</embedded>
</ParentType>
有没有办法改变事物,使两个 embedded
元素具有相同的前缀?由于外部原因,我不能只将 EmbeddedType
复制到 Parent.xsd 中。此外,不加选择地消除所有使用 emb
前缀的解决方案将不起作用。
如果您希望第一个 embedded
与第二个 embedded
一起位于 urn:Embedded
命名空间中,您可以通过 xs:element/@ref
:[=17 将其移动到那里=]
Parent.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:Parent"
xmlns:emb="urn:Embedded"
targetNamespace="urn:Parent"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:import namespace="urn:Embedded" schemaLocation="Embedded.xsd"/>
<xs:element name="ParentType">
<xs:complexType>
<xs:sequence>
<xs:element ref="emb:embedded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Embedded.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:emb="urn:Embedded"
targetNamespace="urn:Embedded"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="embedded" type="emb:EmbeddedType"/>
<xs:complexType name="EmbeddedType">
<xs:sequence>
<xs:element name="numeric" type="xs:int"/>
<xs:element name="embedded" type="emb:EmbeddedType"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
XML
<?xml version="1.0" encoding="utf-8"?>
<ParentType xmlns="urn:Parent"
xmlns:emb="urn:Embedded"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:Parent Parent.xsd">
<emb:embedded>
<emb:numeric>12</emb:numeric>
<emb:embedded>
<emb:numeric>5</emb:numeric>
</emb:embedded>
</emb:embedded>
</ParentType>
我有一个跨两个 .XSD 文件定义的复杂类型。
Parent.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:Parent"
xmlns:emb="urn:Embedded" targetNamespace="urn:Parent" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:import namespace="urn:Embedded" schemaLocation="Embedded.xsd"/>
<xs:element name="ParentType">
<xs:complexType>
<xs:sequence>
<xs:element name="embedded" type="emb:EmbeddedType"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Embedded.xsd:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="urn:Embedded" targetNamespace="urn:Embedded"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:complexType name="EmbeddedType">
<xs:sequence>
<xs:element name="numeric" type="xs:int"></xs:element>
<xs:element name="embedded" type="EmbeddedType"
minOccurs="0" maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
我的 XML 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<ParentType xmlns="urn:Parent" xmlns:emb="urn:Embedded">
<embedded>
<emb:numeric>12</emb:numeric>
<emb:embedded>
<emb:numeric>5</emb:numeric>
</emb:embedded>
</embedded>
</ParentType>
有没有办法改变事物,使两个 embedded
元素具有相同的前缀?由于外部原因,我不能只将 EmbeddedType
复制到 Parent.xsd 中。此外,不加选择地消除所有使用 emb
前缀的解决方案将不起作用。
如果您希望第一个 embedded
与第二个 embedded
一起位于 urn:Embedded
命名空间中,您可以通过 xs:element/@ref
:[=17 将其移动到那里=]
Parent.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:Parent"
xmlns:emb="urn:Embedded"
targetNamespace="urn:Parent"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:import namespace="urn:Embedded" schemaLocation="Embedded.xsd"/>
<xs:element name="ParentType">
<xs:complexType>
<xs:sequence>
<xs:element ref="emb:embedded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Embedded.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:emb="urn:Embedded"
targetNamespace="urn:Embedded"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="embedded" type="emb:EmbeddedType"/>
<xs:complexType name="EmbeddedType">
<xs:sequence>
<xs:element name="numeric" type="xs:int"/>
<xs:element name="embedded" type="emb:EmbeddedType"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
XML
<?xml version="1.0" encoding="utf-8"?>
<ParentType xmlns="urn:Parent"
xmlns:emb="urn:Embedded"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:Parent Parent.xsd">
<emb:embedded>
<emb:numeric>12</emb:numeric>
<emb:embedded>
<emb:numeric>5</emb:numeric>
</emb:embedded>
</emb:embedded>
</ParentType>