0

如何修改此 XSD 架构,以便验证该 XML 中没有命名空间声明的 XML 文件?

当候选 XML 包含相同命名空间的声明时,架构会验证,但如果我不必先编辑候选文档,显然工作流程会更容易。

我敢肯定我在这里遗漏了一些简单的东西,但是一个小时的猜测并没有改变主意。

架构——data.xsd

<schema
  xmlns=xsd:"http://www.w3.org/2001/XMLSchema"
  xmlns:target="http://myurl.com/response"
  targetNamespace="http://myurl.com/response"
  elementFormDefault="qualified">
  <xsd:element name="response">
    <xsd:complexType mixed="true" name="response">
      <xsd:all>
        <xsd:element name="item">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:any namespace="##any"
                   processContents="lax"
                   minOccurs="0"
                   maxOccurs="unbounded" /xsd:>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="activities" >
          <xsd:complexType>
            <xsd:sequence>
              <xsd:any namespace="##any"
                   processContents="lax"
                   minOccurs="0"
                   maxOccurs="unbounded" /xsd:>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <!-- et cetera -->
      </xsd:all>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

本文档验证 -- data.namespaced.xml

<?xml version="1.0" encoding="utf-8"?>
<response
  xmlns="http://myurl.com/response"
  >
  <item>
    <! -- valid stuff -->
  </item>
  <activities>
    <! -- valid stuff -->
  </activities>
</response>

该文档没有 -- data.xml

<?xml version="1.0" encoding="utf-8"?>
<response>
  <item>
    <! -- valid stuff -->
  </item>
  <activities>
    <! -- valid stuff -->
  </activities>
</response>

我为此使用 xmlllint:

xmllint --noout --schema data.xsd data.[namespaced.].xml

提前致谢。

4

1 回答 1

1

Remove the targetNamespace attribute from the xsd:schema. You could then also remove the elementFormDefault as it makes no difference when there's no target namespace.

In your original schema with the targetNamespace a declaration like

<xsd:element name="response">

declares an element whose local name is response and whose namespace URI is http://myurl.com/response. The namespace URI is an integral part of the element's fully qualified name, so the schema will not validate an element with the same local name but a different (or empty) namespace.

Conversely, if you remove the target namespace then the schema will validate non-namespaced documents but will not validate namespaced ones.

于 2013-10-24T15:49:43.843 回答