Using XML Inclusions (XInclude) 1.0

This guide explains how to use XML Inclusions (XInclude) with XmlPrime.

This topic contains the following sections.

Performing XML Inclusion processing

XML Inclusion processing is performed using one of the ProcessProcessProcess methods on the static class XIncludeXIncludeXInclude.

In this example we will be using the following documents, assumed to be in files input.xml and pnael.xml respectively.

 
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>XInclude Example</title>
  </head>
  <body>
    <h1>XInclude Example</h1>
    <p>This is a simple example of XML Inclusion (XInclude).</p>

    <include xmlns="http://www.w3.org/2001/XInclude"
             href="panel.xml" />
      
  </body>
</html>
        
 
 
<div xmlns="http://www.w3.org/1999/xhtml">
  <h2>Included content</h2>
  <p>This content was included from file <em>panel.xml</em>.</p>
</div>
      
 

First, we create a new XmlNameTableXmlNameTableXmlNameTable instance. This will be used to share information such as element and attribute names between the XML documents being processed. Then we create an XmlResolverXmlResolverXmlResolver. This will be used to resolve included resources. Next we create an XmlReaderSettingsXmlReaderSettingsXmlReaderSettings instance and set its NameTableNameTableNameTable and XmlResolverXmlResolverXmlResolver property to our newly created name table and URL resolver respectively.

 
        
var nameTable = new NameTable();
var xmlResolver = new XmlPreloadedResolver(new XmlUrlResolver());
var xmlReaderSettings = new XmlReaderSettings
				                {
				                  NameTable = nameTable,
				                  XmlResolver = xmlResolver,
				                };

      
 

We create a DocumentSetDocumentSetDocumentSet. which ensures that multiple requests for the same document return the same response.

 
        
var documentSet = new DocumentSet(nameTable, xmlReaderSettings);

      
 

We now create an XIncludeSettingsXIncludeSettingsXIncludeSettings object, which describes all the settings used for XML inclusion. In particular, we specify the document set to use during XML Inclusion processing, and specify that Base URI Fixup and Language Fixup should be performed. These settings govern the introduction of xml:base and xml:lang attributes respectively.

 
          
var includeSettings = new XIncludeSettings
    {
      FixupXmlBase = true,
      FixupXmlLang = true,
      DocumentSet = documentSet
    };

        
 

We will serialize the results of performing the inclusion to the output file. We use an XdmWriterSettingsXdmWriterSettingsXdmWriterSettings object which describes the serialization parameters used to write the results. In particular we will set the character reference style to hex, and will specify indentation.

 
var serializationSettings = new XdmWriterSettings
    {
      CharacterReferenceStyle = CharacterReferenceStyle.Hexadecimal,
      Indent = true
    };

      
 

Finally, we invoke the Process (string, string, XdmWriterSettings, XIncludeSettings)Process (String, String, XdmWriterSettings, XIncludeSettings)Process (String^, String^, XdmWriterSettings^, XIncludeSettings^) method to serialize the result of performing XML Inclusion processing on input.xml to the output.xml.

 
XInclude.Process("input.xml", "output.xml", serializationSettings, includeSettings);