I found a few Q's relating to xinclude, but none that specifically answered my very basic question about how to include external docs

Here are a couple of xml docs that I would like to ref each other:

<?xml version="1.0" encoding="UTF-8"?>
<t:person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://test test-schema.xsd"
    xmlns:t="http://test">

    <t:first-name>Wilma</t:first-name>
    <t:last-name>Flintstone</t:last-name>
    <t:spouse>
        <xi:include xmlns:xi="http://www.w3.org/TR/xinclude" href="fred.xml"/>
    </t:spouse>

</t:person>

<?xml version="1.0" encoding="UTF-8"?>
<t:person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://test test-schema.xsd"
 xmlns:t="http://test">
    <t:first-name>Fred</t:first-name>
    <t:last-name>Flintstone</t:last-name>
    <t:spouse>
        <xi:include xmlns:xi="http://www.w3.org/TR/xinclude" href="wilma.xml"/>
    </t:spouse>
</t:person>

and the schema:

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

<xs:element name="person" type="personType"/>

<xs:complexType name="personType">
    <xs:sequence>
        <xs:element name="first-name" type="xs:string"/>
        <xs:element name="last-name" type="xs:string"/>
        <xs:element name="spouse" type="personType"/>
    </xs:sequence>
</xs:complexType>

</xs:schema>

the xi:include element is coming up as invalid. I've been searching around, and can't find a simple example like this. The xi:include simply a stand-in for the element that is supposed to be there, correct?

thanks, bp

有帮助吗?

解决方案 2

Someone who wants to perform both validation and XInclude processing may wish to perform XInclude first, then validate, or to validate first, then perform XInclude, or validate first, then perform XInclude processing, then validate again. In the current state of mind-reading technology, software cannot tell which of these is desired without help from the human. You know what order you want things to happen in, but have you told your software? From your description, it sounds as if your processor defaults to validate-first, then do XInclude; if you want a non-default processing sequence, you'll have to tell your processor. How you do that is processor-dependent; read the documentation.

其他提示

You've got the wrong namespace for Xinclude.

Running xmllint --xinclude on either of your xml files produces no change because it isn't recognized as an xinclude statement.

Change the namespace to: xmlns:xi="http://www.w3.org/2001/XInclude"

And it will make some change in the output, but you'll also get an error on the mutual recursion:

$xmllint --xinclude wilma.xml 
fred.xml:8: element include: XInclude error : detected a recursion in wilma.xml
<?xml version="1.0" encoding="UTF-8"?>
<t:person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://test" xsi:schemaLocation="http://test test-schema.xsd">
    <t:first-name>Wilma</t:first-name>
    <t:last-name>Flintstone</t:last-name>
    <t:spouse>
        <t:person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://test" xsi:schemaLocation="http://test test-schema.xsd">
    <t:first-name>Fred</t:first-name>
    <t:last-name>Flintstone</t:last-name>
    <t:spouse>
        <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="wilma.xml"/>
    </t:spouse>
</t:person> 
    </t:spouse>
</t:person>

XInclude should recursively process xincludes, however according to http://www.w3.org/TR/xinclude/#loops:

When recursively processing an xi:include element, it is a fatal error to process another xi:include element with an include location and xpointer attribute value that have already been processed in the inclusion chain.

Also, when you do try to validate it against the schema, besides the recursion error, you get:

fred.xml:4: element person: Schemas validity error : Element '{http://test}person': This element is not expected. Expected is ( {http://test}first-name ).
wilma.xml fails to validate

I believe this is because your schema says that spouse IsA personType, but in your xml, spouse CONTAINS a personType element: person.

Plus what c-m-sperberg-mcqueen said above.

If you want it to validate before doing xinclude expansion, then you need to include the xi:include element and it's attributes in the schema.

If you want it to validate after doing xinclude expansion, the xinclude processor will ( unless you tell it not to in some manner ) usually add an xml:base attribute to the included element, so you will need to add xml:base as an allowed attribute in the schema. ( I had initially thought, that since the xml namespace is reserved, that xml: attributes didn't need to be included in the schema, but that is not the case. )

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top