public sealed class XslTransform
|
Additional arguments can also be added to the stylesheet using the XsltArgumentList class. This class contains input parameters for the stylesheet and extension objects which can be called from the stylesheet.
To transform XML data:
Scripting Support:
This class supports embedded scripting using the msxsl:script element. When the stylesheet is loaded, all defined functions are compiled and wrapped in a class definition. The msxsl:script element requires that:
The msxsl:script element belongs to the namespace urn:schemas-microsoft-com:xslt. The stylesheet must include the namespace declaration xmlns:msxsl=urn:schemas-microsoft-com:xslt.
The msxsl:script element can include a language attribute that specifies the scripting language to use. The value of the language attribute must be one of the following: C#, CSharp, VB, VisualBasic, JScript, or JavaScript. Because the language name is not case-sensitive, JavaScript and javascript are both valid. If a language attribute is not specified, it defaults to JScript.
The msxsl:script element must include an implements-prefix attribute that contains the prefix representing the namespace associated with the script block. This namespace must be defined within the stylesheet. A stylesheet can include multiple script blocks which are grouped by namespace. You cannot have script blocks with multiple languages within the same namespace. Script blocks can call a function defined in another script block, provided the script blocks reside within the same namespace. The contents of a script block are parsed according to the rules and syntax of the scripting language (supplied by the language attribute). For example, if you had a C# script block comments would be prefixed by the
//
characters. The comments must be valid XML content.
Note It is recommended that you wrap script blocks in a CDATA section.
<msxsl:script implements-prefix='xy' language='C#'> <![CDATA[ // Add code here. ]]> </msxsl:script>
Functions can be declared within the msxsl:script element. The following table shows the namespaces that are supported by default. You can use classes outside the listed namespaces, however, these classes must be fully qualified.
Supported Namespaces | Description |
---|---|
System | System classes. |
System.Collection | Collection classes. |
System.Text | Text handling classes. |
System.Xml | Core XML classes. |
System.Xml.Xsl | XSLT classes. |
System.Xml.XPath | XPath classes. |
The supplied arguments and return values defined by the script functions must be one of the W3C XPath types. The following table details the mapping between the W3C XPath types and the .NET XPath classes.
W3C XPath Type | Equivalent .NET XPath Class |
---|---|
String | System.String |
Boolean | System.Boolean |
Number | System.Double |
Node fragment | System.Xml.XPath.XPathNavigator |
Node set | System.Xml.XPath.XPathNodeIterator |
If the script function utilizes one of the following numeric types: Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, or Decimal, these types are coerced to Double, which maps to the W3C XPath type number.
An exception is thrown when a function is called that cannot convert the argument result to one of the required types.
For more information see the conceptual topic at MSDN: xslttransformationswithxsltransform
//Create a new XslTransform object. XslTransform xslt = new XslTransform(); //Load the stylesheet. xslt.Load("http://server/favorite.xsl"); //Create a new XPathDocument and load the XML data to be transformed. XPathDocument mydata = new XPathDocument("inputdata.xml"); //Create an XmlTextWriter which outputs to the console. XmlWriter writer = new XmlTextWriter(Console.Out); //Transform the data and send the output to the console. xslt.Transform(mydata,null,writer);
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. Initializes a new instance of the XslTransform class. |
XmlResolver | Write-only Sets the XmlResolver used to resolve external resources when the XslTransform.Transform method is called. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Load | Overloaded:Load(IXPathNavigable stylesheet) Loads the XSLT stylesheet contained in the IXPathNavigable. |
Load | Overloaded:Load(string url) Loads the XSLT stylesheet specified by a URL. |
Load | Overloaded:Load(XmlReader stylesheet) Loads the XSLT stylesheet contained in the XmlReader. |
Load | Overloaded:Load(XPathNavigator stylesheet) Loads the XSLT stylesheet contained in the XPathNavigator. |
Load | Overloaded:Load(IXPathNavigable stylesheet, XmlResolver resolver) Loads the XSLT stylesheet contained in the IXPathNavigable. |
Load | Overloaded:Load(string url, XmlResolver resolver) Loads the XSLT stylesheet specified by a URL. |
Load | Overloaded:Load(XmlReader stylesheet, XmlResolver resolver) Loads the XSLT stylesheet contained in the XmlReader. |
Load | Overloaded:Load(XPathNavigator stylesheet, XmlResolver resolver) Loads the XSLT stylesheet contained in the XPathNavigator. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Transform | Overloaded:Transform(IXPathNavigable input, XsltArgumentList args) Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlReader. |
Transform | Overloaded:Transform(string inputfile, string outputfile) Transforms the XML data in the input file and outputs the result to an output file. |
Transform | Overloaded:Transform(XPathNavigator input, XsltArgumentList args) Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlReader. |
Transform | Overloaded:Transform(IXPathNavigable input, XsltArgumentList args, Stream output) Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to a Stream. |
Transform | Overloaded:Transform(IXPathNavigable input, XsltArgumentList args, TextWriter output) Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to a TextWriter. |
Transform | Overloaded:Transform(IXPathNavigable input, XsltArgumentList args, XmlWriter output) Transforms the XML data in the IXPathNavigable using the specified args and outputs the result to an XmlWriter. |
Transform | Overloaded:Transform(XPathNavigator input, XsltArgumentList args, Stream output) Transforms the XML data in the XPathNavigator using the specified args and outputs the result to a Stream. |
Transform | Overloaded:Transform(XPathNavigator input, XsltArgumentList args, TextWriter output) Transforms the XML data in the XPathNavigator using the specified args and outputs the result to a TextWriter. |
Transform | Overloaded:Transform(XPathNavigator input, XsltArgumentList args, XmlWriter output) Transforms the XML data in the XPathNavigator using the specified args and outputs the result to an XmlWriter. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public XslTransform(); |
XmlResolver XmlResolver {set;}
|
By default, XslTransform uses the XmlUrlResolver to resolve resources using the file:// and http:// protocols. However, if you have implemented a custom XmlResolver, you can specify your custom XmlResolver instead.
~XslTransform(); |
public virtual int GetHashCode(); |
public Type GetType(); |
public void Load( |
stylesheet
Exception Type | Condition |
---|---|
XsltCompileException | The loaded resource is not a valid stylesheet. |
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.
public void Load( |
url
Exception Type | Condition |
---|---|
XsltCompileException | The loaded resource is not a valid stylesheet. |
XslTransform supports the XSLT 1.0 syntax. The XSLT stylesheet must include the namespace declaration xmlns:xsl=http://www.w3.org/1999/XSL/Transform.
using System; using System.IO; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; public class Sample { private const String filename = "books.xml"; private const String stylesheet = "output.xsl"; public static void Main() { //Load the stylesheet. XslTransform xslt = new XslTransform(); xslt.Load(stylesheet); //Load the file to transform. XPathDocument doc = new XPathDocument(filename); //Create an XmlTextWriter which outputs to the console. XmlTextWriter writer = new XmlTextWriter(Console.Out); //Transform the file and send the output to the console. xslt.Transform(doc, null, writer); writer.Close(); } }
The sample uses the following two input files.
books.xml
<?xml version='1.0'?> <!-- This file represents a fragment of a book store inventory database --> <bookstore> <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore>
output.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="bookstore"> <HTML> <BODY> <TABLE BORDER="2"> <TR> <TD>ISBN</TD> <TD>Title</TD> <TD>Price</TD> </TR> <xsl:apply-templates select="book"/> </TABLE> </BODY> </HTML> </xsl:template> <xsl:template match="book"> <TR> <TD><xsl:value-of select="@ISBN"/></TD> <TD><xsl:value-of select="title"/></TD> <TD><xsl:value-of select="price"/></TD> </TR> </xsl:template> </xsl:stylesheet>
public void Load( |
stylesheet
Exception Type | Condition |
---|---|
XsltCompileException | The current node does not conform to a valid stylesheet. |
This method loads the XSLT stylesheet including any stylesheets referenced in xsl:include and xsl:import elements. The stylesheet loads from the current node of the XmlReader through all its children. This enables you to use a portion of a document as the stylesheet. By default XslTransform uses an XmlUrlResolver with default credentials (that is CredentialCache.DefaultCredentials). To specify a custom resolver, or a resolver with a different credential set, use the overload that takes an XmlResolver as one of its arguments.
After the Load method returns, the XmlReader is positioned on the next node after the end of the stylesheet. If the end of the document is reached, the XmlReader is positioned at the end of file (EOF).
If the stylesheet contains entities, you should specify an XmlReader that can resolve entities ( XmlReader.CanResolveEntity returns true). In this case, an XmlValidatingReader can be used.
using System; using System.IO; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; public class Sample { private const String filename = "books.xml"; private const String stylesheet = "titles.xsl"; public static void Main() { //Create the reader to load the stylesheet. //Move the reader to the xsl:stylesheet node. XmlTextReader reader = new XmlTextReader(stylesheet); reader.Read(); reader.Read(); //Create the XslTransform object and load the stylesheet. XslTransform xslt = new XslTransform(); xslt.Load(reader); //Load the file to transform. XPathDocument doc = new XPathDocument(filename); //Create an XmlTextWriter which outputs to the console. XmlTextWriter writer = new XmlTextWriter(Console.Out); //Transform the file and send the output to the console. xslt.Transform(doc, null, writer); writer.Close(); } }The example uses the following data files as input.books.xml
<bookstore> <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore>titles.xsl
<!--Stylesheet to sort all books by title--> <!--Created 2/13/2001--> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="bookstore"> <books> <xsl:apply-templates select="book"> <xsl:sort select="title"/> </xsl:apply-templates> </books> </xsl:template> <xsl:template match="book"> <book><xsl:copy-of select="node()"/></book> </xsl:template> </xsl:stylesheet>
public void Load( |
stylesheet
Exception Type | Condition |
---|---|
XsltCompileException | The current node does not conform to a valid stylesheet. |
This method loads the XSLT stylesheet including any stylesheets referenced in xsl:include and xsl:import elements. By default XslTransform uses an XmlUrlResolver with default credentials (that is CredentialCache.DefaultCredentials). To specify a custom resolver, or a resolver with a different credential set, use the overload which takes an XmlResolver as one of its arguments.
The stylesheet is loaded from the current position of the XPathNavigator. To use just a portion of the loaded document as the stylesheet, navigate to the node corresponding to the beginning of the stylesheet. After the Load method returns, the XPathNavigator is positioned at the beginning of the stylesheet (on the xsl:stylesheet node).
public void Load( |
stylesheet
resolver
Exception Type | Condition |
---|---|
XsltCompileException | The loaded resource is not a valid stylesheet. |
The resolver is not cached after the Load method completes.
public void Load( |
url
resolver
Exception Type | Condition |
---|---|
XsltCompileException | The loaded resource is not a valid stylesheet. |
The resolver is not cached after the Load method completes.
using System; using System.IO; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; using System.Net; public class Sample { private const String filename = "books.xml"; private const String stylesheet = "sort.xsl"; public static void Main() { //Create the XslTransform. XslTransform xslt = new XslTransform(); //Create a resolver and set the credentials to use. XmlUrlResolver resolver = new XmlUrlResolver(); NetworkCredential myCred = new NetworkCredential("username","password","domain"); resolver.Credentials = myCred; //Load the stylesheet. xslt.Load(stylesheet, resolver); //Load the XML data file. XPathDocument doc = new XPathDocument(filename); //Create the XmlTextWriter to output to the console. XmlTextWriter writer = new XmlTextWriter(Console.Out); //Transform the file. xslt.Transform(doc, null, writer); writer.Close(); } }
The example uses the following data files as input.
books.xml
<!-- This file represents a fragment of a book store inventory database --> <bookstore> <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore>
sort.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="bookstore"/> <xsl:include href="http://serverA/includefile.xsl"/> <xsl:template match="book"> <TR> <TD><xsl:value-of select="@ISBN"/></TD> <TD><xsl:value-of select="title"/></TD> <TD><xsl:value-of select="price"/></TD> </TR> </xsl:template> </xsl:stylesheet>
includefile.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="bookstore"> <HTML> <BODY> <TABLE BORDER="2"> <TR> <TD>ISBN</TD> <TD>Title</TD> <TD>Price</TD> </TR> <xsl:apply-templates select="book"> <xsl:sort select="@ISBN"/> </xsl:apply-templates> </TABLE> </BODY> </HTML> </xsl:template> </xsl:stylesheet>
public void Load( |
stylesheet
resolver
Exception Type | Condition |
---|---|
XsltCompileException | The current node does not conform to a valid stylesheet. |
This method loads the XSLT stylesheet including any stylesheets referenced in xsl:include and xsl:import elements. The stylesheet loads from the current node of the XmlReader through all its children. This enables you to use a portion of a document as the stylesheet.
After the Load method returns, the XmlReader is positioned on the next node after the end of the stylesheet. If the end of the document is reached, the XmlReader is positioned at the end of file (EOF).
If the stylesheet contains entities, you should specify an XmlReader that can resolve entities ( XmlReader.CanResolveEntity returns true). In this case, an XmlValidatingReader can be used.
The resolver is not cached after the Load method completes.
public void Load( |
stylesheet
resolver
Exception Type | Condition |
---|---|
XsltCompileException | The current node does not conform to a valid stylesheet. |
The stylesheet is loaded from the current position of the XPathNavigator. To use a portion of the loaded document as the stylesheet, navigate to the node corresponding to the beginning of the stylesheet. After the Load method returns, the XPathNavigator is positioned at the beginning of the stylesheet (on the xsl:stylesheet node).
The resolver is not cached after the Load method completes.
protected object MemberwiseClone(); |
public virtual string ToString(); |
public XmlReader Transform( |
input
args
The args are matched with the xsl:param elements defined in the stylesheet.
This method enables you to do an asynchronous transformation of the source document.
Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an XmlDocument containing just the node fragment, and pass that XmlDocument to the Transform method.
The following example performs a transformation on a node fragment.
XslTransform xslt = new XslTransform(); xslt.Load("print_root.xsl"); XmlDocument doc = new XmlDocument(); doc.Load("library.xml"); // Create a new document containing just the node fragment. XmlNode testNode = doc.DocumentElement.FirstChild; XmlDocument tmpDoc = new XmlDocument(); tmpDoc.LoadXml(testNode.OuterXml); // Pass the document containing the node fragment // to the Transform method. Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl"); xslt.Transform(tmpDoc, null, Console.Out);
The example uses the library.xml and print_root.xsl files as input and outputs the following to the console.
Passing <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> to print_root.xsl Root node is book.
library.xml
<library> <book genre='novel' ISBN='1-861001-57-5'> <title>Pride And Prejudice</title> </book> <book genre='novel' ISBN='1-81920-21-2'> <title>Hook</title> </book> </library>
print_root.xsl
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" > <output method="text" /> <template match="/"> Root node is <value-of select="local-name(//*[position() = 1])" /> </template> </stylesheet>
//Create the XslTransform object. XslTransform xslt = new XslTransform(); //Load the stylesheet. xslt.Load("titles.xsl"); //Transform the file. XPathDocument doc = new XPathDocument("books.xml"); XmlReader reader = xslt.Transform(doc, null);
inputfile
outputfile
//Create the XslTransform object. XslTransform xslt = new XslTransform(); //Load the stylesheet. xslt.Load("output.xsl"); //Transform the file. xslt.Transform("books.xml", "books.html");
public XmlReader Transform( |
input
args
Exception Type | Condition |
---|---|
XsltException | There was an error processing the XSLT transform. |
The args are matched with the xsl:param elements defined in the stylesheet. The transformation selections apply to the document as a whole. In other words, if the current node is set on a node tree other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. After the transformation has been performed, the XPathNavigator remains in its original state. This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.
This method enables you to do an asynchronous transformation of the source document.
public void Transform( |
input
args
output
Exception Type | Condition |
---|---|
XsltException | There was an error processing the XSLT transform. |
The args are matched with the xsl:param elements defined in the stylesheet.
Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an XmlDocument containing just the node fragment, and pass that XmlDocument to the Transform method.
The following example performs a transformation on a node fragment.
XslTransform xslt = new XslTransform(); xslt.Load("print_root.xsl"); XmlDocument doc = new XmlDocument(); doc.Load("library.xml"); // Create a new document containing just the node fragment. XmlNode testNode = doc.DocumentElement.FirstChild; XmlDocument tmpDoc = new XmlDocument(); tmpDoc.LoadXml(testNode.OuterXml); // Pass the document containing the node fragment // to the Transform method. Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl"); xslt.Transform(tmpDoc, null, Console.Out);
The example uses the library.xml and print_root.xsl files as input and outputs the following to the console.
Passing <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> to print_root.xsl Root node is book.
library.xml
<library> <book genre='novel' ISBN='1-861001-57-5'> <title>Pride And Prejudice</title> </book> <book genre='novel' ISBN='1-81920-21-2'> <title>Hook</title> </book> </library>
print_root.xsl
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" > <output method="text" /> <template match="/"> Root node is <value-of select="local-name(//*[position() = 1])" /> </template> </stylesheet>
public void Transform( |
input
args
output
Exception Type | Condition |
---|---|
XsltException | There was an error processing the XSLT transform. |
The args are matched with the xsl:param elements defined in the stylesheet.
Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an XmlDocument containing just the node fragment, and pass that XmlDocument to the Transform method.
The following example performs a transformation on a node fragment.
XslTransform xslt = new XslTransform(); xslt.Load("print_root.xsl"); XmlDocument doc = new XmlDocument(); doc.Load("library.xml"); // Create a new document containing just the node fragment. XmlNode testNode = doc.DocumentElement.FirstChild; XmlDocument tmpDoc = new XmlDocument(); tmpDoc.LoadXml(testNode.OuterXml); // Pass the document containing the node fragment // to the Transform method. Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl"); xslt.Transform(tmpDoc, null, Console.Out);
The example uses the library.xml and print_root.xsl files as input and outputs the following to the console.
Passing <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> to print_root.xsl Root node is book.
library.xml
<library> <book genre='novel' ISBN='1-861001-57-5'> <title>Pride And Prejudice</title> </book> <book genre='novel' ISBN='1-81920-21-2'> <title>Hook</title> </book> </library>
print_root.xsl
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" > <output method="text" /> <template match="/"> Root node is <value-of select="local-name(//*[position() = 1])" /> </template> </stylesheet>
public void Transform( |
input
args
output
Exception Type | Condition |
---|---|
XsltException | There was an error processing the XSLT transform. |
The args are matched with the xsl:param elements defined in the stylesheet.
Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an XmlDocument containing just the node fragment, and pass that XmlDocument to the Transform method.
The following example performs a transformation on a node fragment.
XslTransform xslt = new XslTransform(); xslt.Load("print_root.xsl"); XmlDocument doc = new XmlDocument(); doc.Load("library.xml"); // Create a new document containing just the node fragment. XmlNode testNode = doc.DocumentElement.FirstChild; XmlDocument tmpDoc = new XmlDocument(); tmpDoc.LoadXml(testNode.OuterXml); // Pass the document containing the node fragment // to the Transform method. Console.WriteLine("Passing " + tmpDoc.OuterXml + " to print_root.xsl"); xslt.Transform(tmpDoc, null, Console.Out);
The example uses the library.xml and print_root.xsl files as input and outputs the following to the console.
Passing <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> to print_root.xsl Root node is book.
library.xml
<library> <book genre='novel' ISBN='1-861001-57-5'> <title>Pride And Prejudice</title> </book> <book genre='novel' ISBN='1-81920-21-2'> <title>Hook</title> </book> </library>
print_root.xsl
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" > <output method="text" /> <template match="/"> Root node is <value-of select="local-name(//*[position() = 1])" /> </template> </stylesheet>
using System; using System.IO; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; public class Sample { private const String filename = "books.xml"; private const String stylesheet = "output.xsl"; public static void Main() { //Load the stylesheet. XslTransform xslt = new XslTransform(); xslt.Load(stylesheet); //Load the file to transform. XPathDocument doc = new XPathDocument(filename); //Create an XmlTextWriter which outputs to the console. XmlTextWriter writer = new XmlTextWriter(Console.Out); //Transform the file and send the output to the console. xslt.Transform(doc, null, writer); writer.Close(); } }
The example uses the following files as input.
books.xml
<?xml version='1.0'?> <!-- This file represents a fragment of a book store inventory database --> <bookstore> <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore>
output.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="bookstore"> <HTML> <BODY> <TABLE BORDER="2"> <TR> <TD>ISBN</TD> <TD>Title</TD> <TD>Price</TD> </TR> <xsl:apply-templates select="book"/> </TABLE> </BODY> </HTML> </xsl:template> <xsl:template match="book"> <TR> <TD><xsl:value-of select="@ISBN"/></TD> <TD><xsl:value-of select="title"/></TD> <TD><xsl:value-of select="price"/></TD> </TR> </xsl:template> </xsl:stylesheet>
public void Transform( |
input
args
output
Exception Type | Condition |
---|---|
XsltException | There was an error processing the XSLT transform. |
The args are matched with the xsl:param elements defined in the stylesheet. The transformation selections apply to the document as a whole. In other words, if the current node is set on a node tree other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. After the transformation has been performed, the XPathNavigator remains in its original state. This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.
using System; using System.IO; using System.Data; using System.Data.SqlClient; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; public class Sample { public static void Main() { // Create a DataSet and load it with customer data. DataSet dsNorthwind = new DataSet(); String sConnect; sConnect="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind"; SqlConnection nwconnect = new SqlConnection(sConnect); String sCommand = "Select * from Customers where Region='WA'"; SqlDataAdapter myDataAdapter = new SqlDataAdapter(sCommand, nwconnect); myDataAdapter.Fill(dsNorthwind,"Customers"); // Load the DataSet into an XmlDataDocument. XmlDataDocument doc = new XmlDataDocument(dsNorthwind); // Create the XslTransform object and load the stylesheet. XslTransform xsl = new XslTransform(); xsl.Load("customers.xsl"); // Create an XPathNavigator to use in the transform. XPathNavigator nav = doc.CreateNavigator(); // Create a FileStream object. FileStream fs = new FileStream("cust.html", FileMode.Create); // Transform the data. xsl.Transform(nav, null, fs); } }The example uses the file, customers.xsl, as input.
<!-- Stylesheet to sort customers by city--> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="NewDataSet"> <HTML> <BODY> <TABLE BORDER="2"> <TR> <TD>Company Name</TD> <TD>Contact Name</TD> <TD>City</TD> </TR> <xsl:apply-templates select="Customers"> <xsl:sort select="City"/> </xsl:apply-templates> </TABLE> </BODY> </HTML> </xsl:template> <xsl:template match="Customers"> <TR> <TD><xsl:value-of select="CompanyName"/></TD> <TD><xsl:value-of select="ContactName"/></TD> <TD><xsl:value-of select="City"/></TD> </TR> </xsl:template> </xsl:stylesheet>
public void Transform( |
input
args
output
Exception Type | Condition |
---|---|
XsltException | There was an error processing the XSLT transform. |
The args are matched with the xsl:param elements defined in the stylesheet. The transformation selections apply to the document as a whole. In other words, if the current node is set on a node tree other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. After the transformation is performed, the XPathNavigator remains in its original state. This means that the node, which is current before the transformation process, remains the current node after the Transform method has been called.
public void Transform( |
input
args
output
Exception Type | Condition |
---|---|
XsltException | There was an error processing the XSLT transform. |
The args are matched with the xsl:param elements defined in the stylesheet. The transformation selections apply to the document as a whole. In other words, if the current node is set on a node tree other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. After the transformation is performed, the XPathNavigator remains in its original state. This means that the node, that is current before the transformation process, remains the current node after the Transform method has been called.
XmlDocument doc = new XmlDocument(); doc.Load("books.xml"); // Modify the XML file. XmlElement root = doc.DocumentElement; root.FirstChild.LastChild.InnerText = "12.95"; // Create an XPathNavigator to use for the transform. XPathNavigator nav = root.CreateNavigator(); // Transform the file. XslTransform xslt = new XslTransform(); xslt.Load("output.xsl"); XmlTextWriter writer = new XmlTextWriter("books.html", null); xslt.Transform(nav, null, writer);