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.
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.
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
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?
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...
来源:https://stackoverflow.com/questions/1098116/xinclude-schema-namespace-validation