System.Xml


System.Xml is the XML infrastructure within Microsoft's .NET framework. It provides support for XML 1.0, XML Namespaces 1.0, XSLT 1.0, XPath 1.0, DOM level 1 and level 2, and XML Schema. Although this package is sometimes referred to as MSXML.NET, Microsoft does not use this terminology. The API is completely different from the older MSXML3/4 products described in the earlier part of this appendix.

Within this framework, the .NET namespace System.Xml.Xsl provides the API for XSLT processing, while System.Xml.Xpath provides the XPath API.

XPathDocument

This class represents a document optimized for XPath and XSLT processing. An instance of this class can be created by directly loading XML from a file:

  XPathDocument doc = new XPathDocument("source.xml");  

Other constructors are available, allowing the document to be constructed from a Stream, a TextReader, or an XMLReader. Some of the constructors have a second parameter allowing you to specify whether whitespace text nodes should be stripped or preserved.

An XPathDocument implements the interface IXPathNavigable, described below.

XMLNode

This class represents a node in the .NET implementation of the DOM. Because it supports the DOM data model as well as the XPath data model, it is likely to be less efficient for XPath and XSLT processing than the XPathDocument object.

There are subclasses of XMLNode for the different node kinds, for example XMLElement, XMLAttribute and so on.

This class, like XPathDocument, implements the IXPathNavigable interface. This means that any software written to use the IXPathNavigable interface can use either an XPathDocument or an XMLNode document as the data source.

One of the subclasses of XMLNode is System.Xml.XmlDataDocument, which supports an XML view of data in a relational database. This allows the XSLT processor to run directly against relational data.

IXPathNavigable

This is a very small interface, with a single method, CreateNavigator. This method returns an XPathNavigator object that can be used to process the underlying data, treating it as an implementation of the XPath data model.

For example:

  XPathDocument doc = new XPathDocument("source.xml");   XPathNavigator nav = doc.CreateNavigator();  

XPathNavigator

The XPathNavigator object holds a current position within a tree, and provides methods to extract properties of the node at that position, and to move the current position to another related node. If the data source is implemented as an XPathDocument, there is actually no object representing the node itself, which makes the model very efficient because small objects have a high overhead.

Because an XPathNavigator is capable of returning all the information in the XPath data model, it acts as an abstraction of a source document, and any object that implements the XPathNavigator interface can be used as a source document for an XSLT transformation.

XSLTransform

The class System.Xml.Xsl.XslTransform is used to perform an XSLT transformation. The basic sequence of operations is:

  • Create an XslTransform object.

  • Use the Load method to load (and compile) the stylesheet.

  • Use the Transform method to perform the transformation.

There are different variants of the Load method that allow the stylesheet to be loaded by supplying a URL, by nominating an XMLReader to read the stylesheet and perform XML parsing, or by supplying an XPathNavigator or IXPathNavigable that locates the stylesheet within an existing document in memory.

If you want to go through all the stages of loading a stylesheet, you can write:

  XPathDocument ss = new XPathDocument("stylesheet.xsl");   XPathNavigator nav = doc.createNavigator();   XslTransform trans = new XslTransform();   trans.Load(nav);  

The options to supply a URL or an XMLReader as input to the Load method can be seen as shortcuts to this process.

The Transform method has a very large number of different overloaded variants. Essentially it takes four arguments: the source document to be transformed, the values supplied for stylesheet parameters, the destination for the result document, and an XmlResolver that is used to resolve URI references supplied to the document() function. The number of variations of this method is due to the fact that both the source and the destination can be supplied in a number of different ways, and these are supported in all combinations.

  • The source document can be supplied by giving a URL, or in the form of an IXPathNavigable or XPathNavigator object.

  • Stylesheet parameters are supplied in an XsltArgumentList object. This allows parameter values to be added using an AddParam() method. Parameters of type boolean, number, and string can be supplied using the Boolean, Double, and String classes; a parameter of type node-set can be supplied in the form of an XPathNavigator.

  • The output destination can be given as an XmlWriter, a TextWriter, or a Stream. Alternatively, instead of supplying the output destination as an argument, the Transform() method can return an XMLReader, which means that the calling application can use the transformation results in the same way as it would use the output of an XML parser.

  • An XMLResolver can be supplied. Like the URIResolver in the JAXP interface, this object is used to fetch a document when supplied with a URI. This allows you to implement your own URI schemes or to use your own catalogs to find a local copy of required documents such as stylesheet modules, schemas, or DTDs.

For example:

  // Load a stylesheet   XslTransform trans = new XslTransform();   trans.Load("stylesheet.xsl");   // Load a source document   XPathDocument source = new XPathDocument("source.xml");   // Set the current date and time as a stylesheet parameter   XsltArgumentList args = new XsltArgumentList();   DateTime now = DateTime.Now;   args.AddParam("date", "", d.ToString());   // Create an XmlTextWriter for the output XmlTextWriter out = new XmlTextWriter(Console.Out);   // Perform the transformation (no XmlResolver is supplied) xslt.Transform(source, args, writer, null);   writer.Close();  



XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net