Transforming an XML Document

Example 7-5 shows one of the simplest possible XSLT-related programs in C#. Given a source filename, a stylesheet filename, and a destination filename, it transforms the source into the destination using the stylesheet. It will work with any XML source file and any XSLT stylesheet.

Example 7-5. One of the simplest possible XSLT programs
using System.Xml.Xsl;

public class Transform {
  public static void Main(string [ ] args) {
    string source = args[0];
    string stylesheet = args[1];
    string destination = args[2];
    XslTransform transform = new XslTransform( );
    transform.Load(stylesheet);
    transform.Transform(source, destination);
  }
}

I won’t explain in excruciating detail how this program works, but I will point out a few important facts about the XslTransform type. It only has one property, XmlResolver, and two methods, Load( ) and Transform( ), but it is still one of the most versatile pitchers in the .NET XML bullpen.

First, the Load( ) method has eight overloads. All of them load the specified XSLT stylesheet, but each of them takes the stylesheet in a different parameter type. The stylesheet may be specified as an IXPathNavigable (such as XmlDocument), URL, XmlReader, or XPathNavigator; together, these types cover every possible way to read an XML document. For each Load( ) method that takes one of these parameters, there is another one taking an XmlResolver as the second parameter. This XmlResolver is used to resolve any stylesheets referenced in xsl:import and xsl:include elements.

Tip

xsl:import and xsl:include perform similar, but not identical, functions, Both allow you to place common templates in a separate stylesheet for easy reuse. However, xsl:import can only appear at the beginning of a stylesheet, and any imported templates have a lower priority than the template in the importing stylesheet. In contrast, xsl:include can appear anywhere in a stylesheet, and any included templates have the same priority as those in the including stylesheet.

If an overload of Load( ) with no resolver parameter is used, the default XmlResolver, XmlUrlResolver, is used; if a null instance is passed in, external namespaces are not resolved. The XmlResolver passed in is used only for purposes of loading the specified stylesheet, and is not cached for use in processing the XSL transform.

In version 1.1 of the .NET Framework, an additional parameter of type System.Security.Policy.Evidence is added to several overloads of the Load( ) method. The Evidence type provides information used to authorize assembly access and code generation for any scripts included in the stylesheet. I’ll discuss scripting towards the end of this chapter.

Second, the Transform( ) method has a total of nine overloads. One takes two strings, an input URI and an output URI. The others take various combinations of IXPathNavigable or XPathNavigator as the first parameter, an XsltArgumentList as the second parameter, and nothing, a Stream, an XmlWriter, or a TextWriter as the third parameter. Two other overloads take only two parameters and return the result tree as an XmlReader, which can be navigated as you’ve already seen in Chapter 2.

Finally, in version 1.0 of the .NET Framework, the XmlResolver property contains the XmlResolver used when invoking the XSLT document( ) function. The document( ) function lets you process multiple source documents in a single stylesheet. This XmlResolver may be the same as the one passed in to the Load( ) method, but does not need to be. In version 1.1 of the .NET Framework, this XmlResolver instance is passed as a parameter to the Transform( ) method.

You may never need to do any more than this with XSLT but if you do, you should be aware that there is a lot more you can do with .NET and XSLT.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.30.19