X包括架构/命名空间验证?

发布于 2024-07-25 23:47:42 字数 2200 浏览 13 评论 0原文

我正在尝试使用 XML Includes 来帮助管理需要可供人类和机器使用的大型 XML 结构。

但是,在尝试构建可根据现有架构进行验证的 XML 文件时,我遇到了无数问题。 这是我正在尝试做的事情的一个简化示例。

我的“main.xml”文件未验证。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Main.xml - This fails to validate. -->
<ns1:main xsi:schemaLocation="http://www.example.com/main main.xsd"
          xmlns:ns1="http://www.example.com/main"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xi="http://www.w3.org/2001/XInclude">

    <name>String</name>
    <xi:include href="child.xml"/> <!-- What I'm trying to do. -->

</ns1:main>

“child.xml”文件作为独立文件可以很好地验证。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Child.xml - This validates fine, as a standalone file. -->
<ns1:child xsi:schemaLocation="http://www.example.com/main main.xsd"
           xmlns:ns1="http://www.example.com/main"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <name>String</name>
    <age>String</age>

 </ns1:child>

这是我的架构:

 <?xml version="1.0" encoding="UTF-8"?>
 <!-- Main.xsd - My Simplified Schema -->
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:ns1="http://www.example.com/main"
            targetNamespace="http://www.example.com/main">

    <!-- Main Element (References Child) -->
    <xs:element name="main">
         <xs:complexType>
             <xs:sequence>
                 <xs:element name="name" type="xs:string"/>
                 <xs:element ref="ns1:child"/>
             </xs:sequence>
         </xs:complexType>
    </xs:element>

    <!-- Child Element -->
    <xs:element name="child">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="age" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
     </xs:element>

</xs:schema>

我的问题几乎显然与名称空间相关,但我不知道如何解决我的问题。

I'm trying to use XML Includes to help manage a large XML structure that needs to be usable by both humans and machines.

But am experiencing a myriad of problems when trying to construct XML files that can be validated against an existing schema. Here's a simplified example of what I'm trying to do.

My "main.xml" file does not validate.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Main.xml - This fails to validate. -->
<ns1:main xsi:schemaLocation="http://www.example.com/main main.xsd"
          xmlns:ns1="http://www.example.com/main"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xi="http://www.w3.org/2001/XInclude">

    <name>String</name>
    <xi:include href="child.xml"/> <!-- What I'm trying to do. -->

</ns1:main>

The "child.xml" file validates fine as a standalone file.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Child.xml - This validates fine, as a standalone file. -->
<ns1:child xsi:schemaLocation="http://www.example.com/main main.xsd"
           xmlns:ns1="http://www.example.com/main"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <name>String</name>
    <age>String</age>

 </ns1:child>

Here's my schema:

 <?xml version="1.0" encoding="UTF-8"?>
 <!-- Main.xsd - My Simplified Schema -->
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:ns1="http://www.example.com/main"
            targetNamespace="http://www.example.com/main">

    <!-- Main Element (References Child) -->
    <xs:element name="main">
         <xs:complexType>
             <xs:sequence>
                 <xs:element name="name" type="xs:string"/>
                 <xs:element ref="ns1:child"/>
             </xs:sequence>
         </xs:complexType>
    </xs:element>

    <!-- Child Element -->
    <xs:element name="child">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="age" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
     </xs:element>

</xs:schema>

My issues are almost-obviously related to namespaces but I'm at a loss for how to fix my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疾风者 2024-08-01 23:47:42

正如 skaffman 已经指出的那样,XML Schema 和 XInclude 不兼容。

来自 xmllint 的验证错误消息清楚地说明了这一点:

main.xml:9: element include: Schemas validity error : Element  '{http://www.w3.org/2001/XInclude}include': This element is not expected. Expected is ( {http://www.example.com/main}child ).
main.xml fails to validate

引用 W3C 建议:“XInclude 定义与应用 XML 模式生成的增强信息集没有任何关系。可以提供这样的增强信息集。作为输入信息集,或者这种增强可能会应用于包含在内所产生的信息集。”

因此,您应该首先通过应用 XInclude 构建整个 XML 文件,然后验证该文件。

编辑:您可以使用 xmllint 和 --xinclude 来验证 main.xml。

As skaffman already pointed out, XML Schema and XInclude are not compatible.

The validation error message from xmllint states this clearly:

main.xml:9: element include: Schemas validity error : Element  '{http://www.w3.org/2001/XInclude}include': This element is not expected. Expected is ( {http://www.example.com/main}child ).
main.xml fails to validate

To quote the W3C Recommendation: "XInclude defines no relationship to the augmented infosets produced by applying an XML schema. Such an augmented infoset can be supplied as the input infoset, or such augmentation might be applied to the infoset resulting from the inclusion."

Thus you should first construct the whole XML file by applying the XIncludes, and validate this file afterwards.

Edit: You can use xmllint with --xinclude to validate main.xml.

挥剑断情 2024-08-01 23:47:42

我同意 grantwparks - XInclude 和 XML Schema 绝对可以一起使用。 这些规范有意相互独立。 显然,XInclude 的作者希望提供在验证之前或验证之后处理包含的自由。

这个velocityreviews 上的帖子讨论了对我有帮助的问题和答案要了解此事,请参阅 xml.com 上的这篇文章,引用其中:

关于 XInclude 最常见的问题之一是如何包含
与验证、XSL 转换和其他过程交互
可以应用于 XML 文档。 简短的回答是,它
没有。 XInclusion 不是任何其他 XML 过程的一部分。 它是一个
您可以执行也可以不执行的单独步骤
对你有用。

例如,考虑针对架构进行验证。 一个文档可以是
在纳入之前、纳入之后或两者都进行验证。 如果您验证
xi:include 元素之前的文档被替换,然后
模式必须像它一样声明 xi:include 元素
声明任何其他元素。 如果您在之后验证文档
xi:include 元素被替换,然后模式必须声明
替换元件。 包含和验证是分开的,
可以按任何顺序执行的正交过程
当地环境方便

所以它似乎可以归结为,让您的 XML 工具在验证之前处理 xi:include 元素(这就是 OP 示例所需要的)。 例如,在Eclipse的XML编辑器中,XML->XML下有一个设置“Process XML Inclusions”。 XML 文件 -> 验证(使用 RSA 8.5),需要打开它才能让编辑器在验证之前处理 xi:include。

安迪

I agree with grantwparks - XInclude and XML Schema absolutely can be used together. The specifications are intentionally independent from each other. Apparently the authors of XInclude wanted to provide the freedom to let the includes be processed before validation or after validation.

This thread on velocityreviews discusses the issue and the answer that helped me to understand the matter was this post on xml.com, quoting from it:

One of the most common questions about XInclude is how inclusion
interacts with validation, XSL transformation, and other processes
that may be applied to an XML document. The short answer is that it
doesn't. XInclusion is not part of any other XML process. It is a
separate step which you may or may not perform when and where it is
useful to you.

For example, consider validation against a schema. A document can be
validated before inclusion, after inclusion, or both. If you validate
the document before the xi:include elements are replaced, then the
schema has to declare the xi:include elements just like it would
declare any other element. If you validate the document after the
xi:include elements are replaced, then the schema has to declare the
replacement elements. Inclusion and validation are separate,
orthogonal processes that can be performed in any order which is
convenient in the local environment

So what it seems to boil down to, is to get your XML tools to process the xi:include elements before validation (that is what the example of the OP needs). For example, in the XML Editor of Eclipse, there is a setting "Process XML Inclusions" under XML -> XML Files -> Validation (using RSA 8.5), which needs to be turned on to get the editor to process xi:include before validation.

Andy

笔芯 2024-08-01 23:47:42

我不认为这是由 XInclude 和 schema 之间不兼容引起的; 在我看来,包含在验证之前没有被处理。 因此,该架构不允许 main 中存在“include”元素,而仅允许“child”元素。 如果您可以强制 XML 处理器在验证之前处理包含内容...

I don't think this is caused by an incompatibility between XInclude and schema; it looks to me like the include is not being processed before validation. Thus the schema does not allow for an "include" element in main, only a "child" element. If you could force your XML processor to process includes before validation...

阳光下的泡沫是彩色的 2024-08-01 23:47:42

我不认为 XML Schema 和 XInclude 规范保证彼此兼容。 个别 XML 处理器可能允许这样做,但其他处理器则不允许。

作为一般规则,我认为两者不应该一起使用。

PS 我不确定你为什么认为这是一个命名空间问题。 是什么给你这样的印象?

I don't think the XML Schema and XInclude specifications are guaranteed to be compatible with each other. Individual XML processors might allow it, but others will not.

As a general rule, I'd say that the two should not be used together.

P.S. I'm not sure why you think this is a namespace problem. What gives you that impression?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文