要求是,我只希望 xsd 中定义的一些元素可以作为根元素访问。因此,为此,方法是通过声明复杂类型并引用它们来使所有其他元素成为本地元素。
但是,如果在另一个 xsd 定义中需要这些*这些不可访问或本地元素,换句话说*,它们可以是属于在不同 xsd 中定义的某个其他命名空间的元素的子元素。
如何实现这种模式定义?
例如在下面说我只希望A:root 和 custom:root是有效根而不是inner:A,所以我在根元素下声明了 innerA 并引用了类型,而 A:innerA 可以在元素下?
我无法在 B.xsd 中定义 innerA,bcz它将使 innerA 位于 B.xsd 的 targetnamespace 即 custom namespace中。
例如
a.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://www.abc.com/custom"
xmlns:A="http://www.abc.com/A"
xmlns:custom="http://www.abc.com/custom">
<xsd:import namespace="http://www.abc.com/custom"
schemaLocation="B.xsd"/>
<xsd:complexType name="innerType">
<xsd:sequence>
<xsd:element name="abc" type="xs:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="root">
<xsd:complexType mixed="true">
<xsd:choice>
<xsd:element ref="custom:root">
<xsd:element name="innerA" type="A:innerType"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
B.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://www.abc.com/custom"
xmlns:A="http://www.abc.com/A"
xmlns:custom="http://www.abc.com/custom">
<xsd:import namespace="http://www.abc.com/A"
schemaLocation="A.xsd"/>
<xsd:element name="root">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element ref="A:innerA"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
上面 B.xsd 中的最后第四行导致错误。如何引用 innerA 元素。有什么建议吗?
XML
<A:root xmlns:A="http://www.abc.com/a" xmlns:custom="http://www.abc.com/custom"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=""http://www.abc.com/a "http://www.abc.com/a/A.xsd">
<custom:root>
<A:innerA>
<A:abc>
</A:innerA>
</custom:root>
</A:hello>