Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

xml - XInclude Schema/Namespace Validation?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...