在XSD中忽略目标命名空间

时间:2014-07-17 10:19:41

标签: xml xsd xml-namespaces xmllint

我有一些供应商提供的XML模式。他们在这里:

OCISchemaBASE.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="C" targetNamespace="C">

  <xs:element name="BroadsoftDocument" type="OCIMessage"/>

  <xs:complexType name="OCIMessage">
    <xs:sequence>
      <xs:choice>
        <xs:element name="sessionId" type="xs:normalizedString"/>
        <xs:element name="userId" type="xs:token"/>
        <xs:element name="phoneNumber" type="xs:token"/>
      </xs:choice>
      <xs:element name="command" type="OCICommand" minOccurs="1" maxOccurs="15"/>
    </xs:sequence>
    <xs:attribute name="protocol" use="required">
      <xs:simpleType>
        <xs:restriction base="xs:NMTOKEN">
          <xs:enumeration value="OCI"/>
          <xs:enumeration value="NSOCI"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
  </xs:complexType>

  <xs:complexType name="OCICommand" abstract="true">
    <xs:sequence>
    </xs:sequence>
    <xs:attribute name="echo" type="xs:string" use="optional"/>
  </xs:complexType>

  <xs:complexType name="OCIRequest" abstract="true">
    <xs:complexContent>
      <xs:extension base="OCICommand">
        <xs:sequence>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

</xs:schema>

OCISchemaLogin.xsd

<xs:schema xmlns:xs             = "http://www.w3.org/2001/XMLSchema"
           xmlns:core           = "C"
           xmlns                = ""
           attributeFormDefault = "qualified"
           elementFormDefault   = "qualified">

  <xs:import namespace      = "C"
             schemaLocation = "OCISchemaBASE.xsd"/>

  <xs:complexType name="AuthenticationRequest">
    <xs:complexContent>
      <xs:extension base="core:OCIRequest">
        <xs:sequence>
          <xs:element name="userId" type="UserId"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>    

</xs:schema>

OCISchemaDataTypes.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:core="C" attributeFormDefault="qualified" elementFormDefault="qualified">

  <xs:simpleType name="UserId">
    <xs:restriction base="xs:token">
      <xs:minLength value="1"/>
      <xs:maxLength value="161"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

这是我根据这些模式创建的XML文档:

<?xml version="1.0" encoding="ISO-8859-1"?>
<BroadsoftDocument
    protocol="OCI" xmlns="C"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
    <sessionId xmlns="">1405499871130</sessionId>
    <command xsi:type="AuthenticationRequest" xmlns="">
        <userId>user@xdp.broadsoft.com</userId>
    </command>
</BroadsoftDocument>

当我尝试使用xmllint针对模式验证此文档时,会发生以下验证失败:

$ xmllint --noout --schema OCISchemaLogin.xsd --schema OCISchemaBASE.xsd --schema OCISchemaDataTypes.xsd ./AuthenticationRequest.xml
./AuthenticationRequest.xml:3: namespace warning : xmlns: URI C is not absolute
    protocol="OCI" xmlns="C"
                            ^
Element '{C}BroadsoftDocument': No matching global declaration available for the validation root.
./AuthenticationRequest.xml fails to validate

为什么BroadsoftDocument元素不在C命名空间中,尽管OCISchemaBASE.xsd中有目标名称空间声明?

同样的问题是关于OCIMessage类型中的sessionId元素。

1 个答案:

答案 0 :(得分:0)

您必须让XML处理器知道在哪里可以找到XML的XSD。

这样做的一种方法是将schemaLocation添加到根BroadsoftDocument元素:

<BroadsoftDocument
    xsi:schemaLocation="C OCISchemaBASE.xsd"
    [...]

另请注意您的警告信息

./AuthenticationRequest.xml:3: namespace warning : xmlns: URI C is not absolute
    protocol="OCI" xmlns="C"

是因为过度简化的命名空间C被解释为相对URI,其为deprecated

The use of relative URI references, including same-document references,
in namespace declarations is deprecated.