
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
868
|
Chapter 15: XML
See Also
See Recipe 15.4 and the “XmlDocument.Validate” topic in the MSDN documenta-
tion.
15.12 Extending XSLT Transformations
Problem
You want to perform operations that are outside the scope of XSLT to include data
in the transformed result.
Solution
Add an extension object to the transformation that can perform the operations nec-
essary based on the node it is passed. This can be accomplished by using the
XsltArgumentList.AddExtensionObject method. This object you’ve created (XslExtensionObject)
can then be accessed in the XSLT and a method called on it to return the data you
want included in the final transformed result:
string xmlFile = @"..\..\publications.xml";
string xslt = @"..\..\publications.xsl";
//Create the XslTransform and load the stylesheet.
// This is not XslCompiledTransform because it gives a different empty node.
//Create the XslCompiledTransform and load the stylesheet.
XslCompiledTransform transform = new XslCompiledTransform( );
transform.Load(xslt);
// Load the XML.
XPathDocument xPathDoc = new XPathDocument(xmlFile);
// Make up the args for the stylesheet with the extension object.
XsltArgumentList xslArg = new XsltArgumentList( );
// Create our custom extension object.
XSLExtensionObject xslExt = new XSLExtensionObject( );
xslArg.AddExtensionObject("urn:xslext", ...