XSD 命名空间 "ns2" 问题
XSD namespace "ns2" issue
我在 Request.xsd 中定义了架构,它将引用 common.xsd。
我期待输出应该如下所示
<Request xmlns="http://ws.myref.com/schemas/test"
xmlns="http://ps.myref.com/schemas/2008/Common">
<EmailList>
<Mail>test@gmail.com</Mmail>
</EmailList>
</Request>
但是我遇到了额外的命名空间 "ns2" 问题。谁能帮我解决这个问题
<ns2:Request xmlns:ns2="http://ps.myref.com/schemas/test"
xmlns="http://ps.myref.com/schemas/Common">
<ns2:EmailList>
<Mail>test@gmail.com</Mail>
</ns2:EmailList>
</ns2:Request>
Request.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" targetNamespace="http://ps.myref.com/schemas/schemas/test"
xmlns="http://ps.myref.com/schemas/schemas/test" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:com="http://ps.myref.com/schemas/Common">
<xsd:import namespace="http://ps.myref.com/schemas/Common" schemaLocation="../schemas/common/common.xsd"/>
<xsd:element name="Request">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EmailLists" type="com:EmailList" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Common.xsd
<?xml version="1.0"?>
<xsd:schema xmlns="http://ps.myref.com/schemas/2008/Common" elementFormDefault="unqualified"
targetNamespace="http://ps.myref.com/schemas/Common"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsd:complexType name="EmailList">
<xsd:sequence>
<xsd:element name="Mail" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
在这种情况下,您的期望是不合理的。
因为类型 "EmailList" 是在 common.xsd 文件中的命名空间 http://ps.myref.com/schemas/2008/Common
下定义的,所以当您在中使用 EmailList 类型时,您别无选择,只能以某种方式区分它另一个模式。如果您查看 request.xsd,您会发现这里确实发生了以下情况:
<xsd:element name="EmailLists" type="com:EmailList" />
本例中的 com:
是一个前缀,旨在表明该类型是在另一个架构中定义的,并且在与正在使用的架构不同的命名空间下定义。
以同样的方式,当xsd验证器使用request.xsd验证模式实例时,它必须确保您在实例中使用的EmailList类型是相同的EmailList类型这是在 common.xsd 模式中定义的,唯一的方法是使用命名空间。
因此您的期望可以总结为:
"I should be able to mix types defined in two different schema definitions freely together without differentiating them and the parser should understand that."
所以你现在应该能看出你的期望是多么不合逻辑。
如果您不想在其中添加 "ns2:",您唯一的选择就是这样做:
<Request xmlns"http://ps.myref.com/schemas/test">
<EmailList xmlns"http://ps.myref.com/schemas/test">
<Mail xmlns="http://ps.myref.com/schemas/Common">test@gmail.com</Mail>
</EmailList>
</Request>
我在 Request.xsd 中定义了架构,它将引用 common.xsd。 我期待输出应该如下所示
<Request xmlns="http://ws.myref.com/schemas/test"
xmlns="http://ps.myref.com/schemas/2008/Common">
<EmailList>
<Mail>test@gmail.com</Mmail>
</EmailList>
</Request>
但是我遇到了额外的命名空间 "ns2" 问题。谁能帮我解决这个问题
<ns2:Request xmlns:ns2="http://ps.myref.com/schemas/test"
xmlns="http://ps.myref.com/schemas/Common">
<ns2:EmailList>
<Mail>test@gmail.com</Mail>
</ns2:EmailList>
</ns2:Request>
Request.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" targetNamespace="http://ps.myref.com/schemas/schemas/test"
xmlns="http://ps.myref.com/schemas/schemas/test" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:com="http://ps.myref.com/schemas/Common">
<xsd:import namespace="http://ps.myref.com/schemas/Common" schemaLocation="../schemas/common/common.xsd"/>
<xsd:element name="Request">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EmailLists" type="com:EmailList" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Common.xsd
<?xml version="1.0"?>
<xsd:schema xmlns="http://ps.myref.com/schemas/2008/Common" elementFormDefault="unqualified"
targetNamespace="http://ps.myref.com/schemas/Common"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsd:complexType name="EmailList">
<xsd:sequence>
<xsd:element name="Mail" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
在这种情况下,您的期望是不合理的。
因为类型 "EmailList" 是在 common.xsd 文件中的命名空间 http://ps.myref.com/schemas/2008/Common
下定义的,所以当您在中使用 EmailList 类型时,您别无选择,只能以某种方式区分它另一个模式。如果您查看 request.xsd,您会发现这里确实发生了以下情况:
<xsd:element name="EmailLists" type="com:EmailList" />
本例中的 com:
是一个前缀,旨在表明该类型是在另一个架构中定义的,并且在与正在使用的架构不同的命名空间下定义。
以同样的方式,当xsd验证器使用request.xsd验证模式实例时,它必须确保您在实例中使用的EmailList类型是相同的EmailList类型这是在 common.xsd 模式中定义的,唯一的方法是使用命名空间。
因此您的期望可以总结为:
"I should be able to mix types defined in two different schema definitions freely together without differentiating them and the parser should understand that."
所以你现在应该能看出你的期望是多么不合逻辑。
如果您不想在其中添加 "ns2:",您唯一的选择就是这样做:
<Request xmlns"http://ps.myref.com/schemas/test">
<EmailList xmlns"http://ps.myref.com/schemas/test">
<Mail xmlns="http://ps.myref.com/schemas/Common">test@gmail.com</Mail>
</EmailList>
</Request>