XIncludeTransformer
The XIncludeTransformer provides a means of including other XML fragments into
the XML file being processed. It follows the XInclude specification. It is possible to include XML or text in several ways:
- Include an entire XML file,
- Include part of an entire XML file, or
- Include straight text.
The XIncludeTransformer component is declared and used in the sitemap as:
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
<map:components>
<map:transformers default="xslt">
<map:transformer name="xinclude" src="resource://lib/transforming/XIncludeTransformer"/>
...
</map:transformers>
</map:components>
<map:pipelines>
<map:pipeline>
<map:match pattern="**.html">
<map:generate src="context://content/index.xml" label="index-xml"/>
<map:transform type="xinclude"/>
...
</map:match>
</map:pipeline>
Simple Example
Within the source XML include statements are entered simply as this folowing example shows. The example
consists of a source file:
<?xml version="1.0" encoding="UTF-8"?>
<page:page xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:t="http://www.hsfr.org.uk/Schema/Text"
xmlns:page="http://www.hsfr.org.uk/Schema/Page">
<page:meta>
...
</page:meta>
<page:content>
<t:heading level="1">XInclude Test</t:heading>
<xi:include href="part-1.xml">
<xi:fallback>
<t:p>Error.</t:p>
</xi:fallback>
</xi:include>
<xi:include href="part-1.xml#element(/1/2/1)" parse="xml">
<xi:fallback>
<t:p>Error.</t:p>
</xi:fallback>
</xi:include>
<xi:include
href="part-2.xml#xmlns(page=http://www.hsfr.org.uk/Schema/Page)xpointer(//page:content/*)"
parse="xml">
<xi:fallback>
<t:p>Error.</t:p>
</xi:fallback>
</xi:include>
</page:content>
</page:page>
and three included files:
part-1.xml
<?xml version="1.0" encoding="UTF-8"?>
<page:page xmlns:t="http://www.hsfr.org.uk/Schema/Text" xmlns:page="http://www.hsfr.org.uk/Schema/Page">
<page:meta>
...
</page:meta>
<page:content>
<t:p>First paragraph</t:p>
</page:content>
</page:page>
part-2.xml
<?xml version="1.0" encoding="UTF-8"?>
<page:page xmlns:t="http://www.hsfr.org.uk/Schema/Text" xmlns:page="http://www.hsfr.org.uk/Schema/Page">
<page:meta>
...
</page:meta>
<page:content>
<t:p>Second paragraph</t:p>
</page:content>
</page:page>
part-3.xml
<?xml version="1.0" encoding="UTF-8"?>
<page:page xmlns:t="http://www.hsfr.org.uk/Schema/Text" xmlns:page="http://www.hsfr.org.uk/Schema/Page">
<page:meta>
...
</page:meta>
<page:content>
<t:p>Third paragraph</t:p>
</page:content>
</page:page>
After the transformer has run the document will be:
<page:page
xmlns:page="http://www.hsfr.org.uk/Schema/Page"
xmlns:t="http://www.hsfr.org.uk/Schema/Text">
<page:meta>
...
</page:meta>
<page:content>
<t:heading level="1">XInclude Test</t:heading>
<page:page>
<page:meta>
...
</page:meta>
<page:content>
<t:p>First paragraph</t:p>
</page:content>
</page:page>
<page:content>
<t:p>Second paragraph</t:p>
</page:content>
<t:p>Second paragraph</t:p>
</page:content>
</page:page>
Copyright 2006 – 2023 Hugh Field-Richards. All Rights Reserved.