Mvp.Xml Library: XInclude.NET module
The XInclude.NET module provides an implementation of the
W3C XML Inclusions (XInclude)1.0 Recommendation and
XPointer Framework Recommendation written in C# for the .NET platform. XInclude.NET supports
XPointer element() Scheme,
XPointer xmlns() Scheme,
XPointer xpath1() Scheme and
XPointer xpointer() Scheme (XPath subset only). XInclude.NET currently supports streamable subset of the XInclude, implemented as fast, non-caching, forward-only XIncludingReader class found in the Mvp.Xml.XInclude namespace.
For more info about XInclude, XPointer and XInclude.NET see
References.
XInclude.NET has been thoroughly tested against
XML Inclusions (XInclude) Version 1.0, W3C Conformance Test Suite (2004-11-03) under Microsoft .NET 1.0, 1.1 and 2.0 on Windows 2000, Windows XP and Windows Server 2003.
1. Usage
XIncludingReader class, found in the Mvp.Xml.XInclude namespace, is the key class. It's customized XmlReader, which implements streamable subset of the XInclude and XPointer in a fast, non-caching, forward-only fashion. It can be set on top of another XmlReader - such design allows to perform XML Inclusions in many different scenarios, e.g. before or after validation, before or after transforming XML etc. Here are some common usage scenarios:
XML inclusion during XML reading:
XmlReader reader = new XIncludingReader("source.xml");
while (reader.Read())
...
XML inclusion while building XmlDocument:
XmlReader reader = new XIncludingReader(XmlReader.Create("source.xml"));
XmlDocument doc = new XmlDocument();
doc.Load(reader);
...
XML inclusion before an XSL Transformation:
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("stylesheet.xsl");
XmlReader reader = new XIncludingReader("source.xml");
XPathDocument xdoc = new XPathDocument(reader);
xslt.Transform(xdoc, null, new StreamWriter("result.xml"));
...
Find more examples and test cases in the "test" directory of the sources.
Note: When using XInclude with XSLT, take a look at the
nxslt.exe - feature-rich .NET XSLT command line utility, which supports XInclude via the XInclude.NET module.
2. Custom XmlResolver
XIncludingReader supports custom URI resolving. This way one can include XML documents from a variety of sources, such as RDBMS or even generated on-the-fly. Find a sample of inlcuding XML data from SQL Server on
Oleg Tkachenko's blog.
Just set your XmlResolver object to the XmlResolver property of the XIncludingReader. XIncludingReader will call ResolveUri() and GetEntity() methods of your resolver when fetching a resource by URI referenced in "href" attribute of an xi:include element. A custom XmlResolver must return either System.IO.Stream or System.IO.TextReader or System.Xml.XmlReader from the GetEntity() method.
3. XPointer
XInclude.NET uses
XPointer.NET module, which implements
XPointer Framework,
XPointer element() Scheme,
XPointer xmlns() Scheme,
XPointer xpath1() Scheme and
XPointer xpointer() Scheme (XPath subset only).
XPointerReader class, found in the Mvp.Xml.XPointer namespace represents XPointer-aware XmlReader and can be used as such outside of XInclude context too.
Find more info at the
XPointer.NET module homepage.
4. Conformance
- XIncludingReader class implements the streamable subset of the XInclude - intra-document references are not supported. That means XIncludingReader treats an absence of an "href" attribute on an xi:include element as an XInclude resource error.
- Syntactically incorrect URI references are treated as an XInclude resource error.
- Only XPath subset of the xpointer() XPointer Scheme is supported.
5. Support
You can get support using the following options:
6. References
- "Combining XML Documents with XInclude" by Oleg Tkachenko, MSDN
- W3C XML Inclusions (XInclude)1.0
- XPointer Framework Recommendation
- XPointer element() Scheme
- XPointer xmlns() Scheme
- XPointer xpointer() Scheme
- nxslt.exe - XInclude enabled command line XSLT utility.
- eXml - XInclude enabled XML ASP.NET Web Server control.