没有命名空间的XML子节点

时间:2015-02-02 10:04:26

标签: xml xsd

我有以下xml和子节点没有前缀。

<ns1:SearchResponse xmlns:ns1="alpwsdl.masterPricer" 
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> 
  <SearchReturn xsi:type="ns1:searchReply"> 
    <searchReference xsi:type="xsd:int">1234</searchReference> 
  </SearchReturn> 
</ns1:SearchResponse>

在我的xml架构中,前缀用于子节点和父节点。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="alpwsdl.masterPricer" 
            targetNamespace="alpwsdl.masterPricer"
            elementFormDefault="qualified">
  <xsd:element name="SearchResponse">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="SearchReturn">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="searchReference">
                <xsd:complexType>
                  <xsd:simpleContent>

我该如何解决?

1 个答案:

答案 0 :(得分:0)

在您的特定情况下,可以通过删除elementFormDefault属性来解决问题,但在一般情况下,当您想在同一文档中使用多个名称空间时,可以将定义拆分为多个XSD文件:

schema.xsd:

<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:ns1="alpwsdl.masterPricer">
    <xsd:import schemaLocation="schema1.xsd" namespace="alpwsdl.masterPricer"/>
    <xsd:element name="SearchReturn">
        <xsd:complexType>
            <xsd:all>
                <xsd:element ref="searchReference" minOccurs="0"/>
            </xsd:all>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="searchReference" type="xsd:string"/>
</xsd:schema>

schema1.xsd:

<xsd:schema targetNamespace="alpwsdl.masterPricer" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:ns1="alpwsdl.masterPricer">
    <xsd:import schemaLocation="schema.xsd"/>
    <xsd:element name="SearchResponse">
        <xsd:complexType>
            <xsd:all>
                <xsd:element ref="SearchReturn" minOccurs="0"/>
            </xsd:all>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>