【问题标题】:Validating XML against a Schema with Namespaces根据具有命名空间的架构验证 XML
【发布时间】:2014-06-26 13:23:26
【问题描述】:

我有几个 XML 文件,我必须针对我的程序中的架构进行验证。我将架构文件存储在本地,当我获得 XML 文件时,我知道要应用哪个架构。当我尝试针对架构验证我的 XML 文件之一并且此架构使用导入语句导入特定命名空间时,我遇到了一个奇怪的错误。这是我想应用于我的 XML 的示例 XSD:

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:test="http://www.test.com/test" xmlns:tns="http://www.test.com/test/folder/targetnamespace" targetNamespace="http://www.test.com/test/folder/targetnamespace" elementFormDefault="qualified">
    <import namespace="http://www.test.com/test" schemaLocation="test.xsd"/>
    <element name="tag1">
        <complexType>
            <sequence minOccurs="1" maxOccurs="1">
                <choice>
                    ...
                    ...
                </choice>
                ...
                ...
                <element name="..." type="..." minOccurs="1" maxOccurs="unbounded"/>
            </sequence>
        </complexType>
    </element>
    <complexType name="tag2">
        <sequence>
            <choice minOccurs="1" maxOccurs="1">
                ...
                ...
            </choice>
            <element ref="test:entries" minOccurs="1" maxOccurs="1"/>
        </sequence>
    </complexType>
</schema>

test.xsd 与此 xsd 文件位于同一位置。我试图针对此架构验证的 XML 如下:

<?xml version="1.0" encoding="utf-8"?>
<tag1 xmlns="http://www.test.com/test/folder/targetnamespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://www.test.com/test">
    <id>ABCDEF</id> 
    <profile>
        ...
        ...
        <test:entries>
          ...
          ...
        </test:entries>
    </profile>
</tag1>

关于哪里出了问题有什么想法吗?我得到的 XML 中是否缺少架构文件的位置?我希望根据我在验证例程中加载的架构文件来验证此 XML。有什么想法吗?

这是导入的 Schema:

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.test.com/test/folder/targetnamespace"
    elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://www.test.com/test">

    <complexType name="EntriesType">
        <sequence>
            ...
            ...
        </sequence>
    </complexType>

    <complexType name="...">
        ...
        ...
    </complexType>
    <element name="entries" type="tns:EntriesType"></element>
</schema>

【问题讨论】:

  • “我遇到了一个奇怪的错误”是一个可怕的问题描述,因为您没有告诉我们那个“奇怪的错误”是什么,而且我们无法从这里看到您的屏幕。您遇到的具体问题是什么?
  • 无法将名称“test:entries”解析为(n)“元素声明”组件。
  • 请包含您的其他架构(声明entries 元素的架构)
  • 按需添加。
  • 您的 test 架构中没有名为 entries 的元素(至少您在此处发布的内容),因此您收到的错误是绝对正确的。跨度>

标签: xml xsd-validation


【解决方案1】:

我设法通过在我的验证器例程中加载两个 XSD 文件来摆脱这种情况。即使其中一个 XSD 文件被导入到另一个文件中,即使在更正了对命名空间的所有引用之后,它也总是失败。所以我决定在我的验证器例程中加载这两个 XSD 文件,然后它就开始工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多