XSLTO
One technique we (MSDN2) are considering using for mapping our Xml messages to
our middle tier objects uses XPath statements in C#. The XPath statements are
used to map sections of Xml to functions.
This is technique was prototyped by Tim Ewald during our last development cycle.
General Overview
This project is a prototype implementation of a technique for mapping xml
sections to methods using xpath statements.
Input
The prototype expects the sample input file XMLFile1.xml. The file contains 3
elements:
<person>
<name>Tim</name>
<age>34</age>
</person>
Output
The prototype maps each element to a method and the element values to a method.
Each method outputs the name or value to the console. The console output is:
person
name
Tim
age
34
Implementation Specifics
-
Introduced an attribute MatchAttribute for tying xpath statements to methods.
-
Introduced the Transform class. It is not intended to be used directly. It is
meant to do the basic mechanics of matching xpath to methods, invoking methods
and doing the general walking of the document.
-
Transform has one public method, Execute, which starts walking the document.
-
The ApplyTemplates(string selection) is a recursive method that does the
majority of work. In general it does the following:
-
Create a list of xpath, method pairs.
-
Begin walking the input xml file, using an XPathNavigator.
-
The XPathNodeIterator selects the nodes based on the xpath statement.
-
For each selected node look for a match in the list of xpath,method pairs. If
one is found, pull out the parameters and invoke the method. The prototype
requires each method has one parameter of type XPathNavigator.
-
To keep current position on that XPathNavigator a stack is used.
-
Introduced the MyTransform class. This class derives from Transform; it has
methods mapped to specific expected input elements. For the sample input
(XMLFile1.xml), it has 3 methods (Person, Name and Age). These map to the
corresponding elements in the sample input file, outputting the name of the
element to the console. It has one additional method which is mapped to the
value of an element -- “text()”. This method outputs the value to the console.
Contact
This sample was provide by Kimberly Wolk and
ported to VS03 by Steve Dunn.
Copyright
COPYRIGHT © 2005 MICROSOFT, CORP. THIS CODE AND INFORMATION IS PROVIDED "AS
IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.