XSD 命名空间 "ns2"问题

标签 xsd

我在 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>

请求.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>

通用.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 类型与 common.xsd 中定义的 EmailList 类型相同schema,唯一的方法就是使用命名空间。

因此,您的期望可以总结为:

“我应该能够自由地将两个不同模式定义中定义的类型混合在一起而不区分它们,解析器应该理解这一点。”

因此,您现在应该能够看出您的期望如何不符合逻辑。

如果你不想要“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>

关于XSD 命名空间 "ns2"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31131868/

相关文章:

c# - .Net - 在没有 WSDL 的情况下使用 Web 服务

python - 当 wsdl 不包含时如何设置 suds 架构位置

visual-studio-2010 - 使用 Visual Studio 2010 浏览 xsd 文件时 "Unauthorized zone"是什么意思?

java - WADL/生成的 XSD 的问题,使用 Jersey 和契约优先的方法

java - intelliJ IDEA 中的自定义 spring 命名空间处理程序

xml - XSD if-else 条件

c# - 确定类如何使用反序列化 XML 的最有效方法

c# - 针对 xsd 和 CRLF 字符的 Xml 验证

c# - 当我的所有 .XSD 都存储为资源时,如何解析 .XSD 的 schemaLocation 属性?

sql - 使用命名空间修改 xml