I l @ ve RuBoard |
By now you should be convinced of the benefits of using XSLT stylesheets. The next step is to apply them in your J# applications. To do this, you have to answer certain questions:
These three questions match up with the three documents involved in an XSLT-based transformation (source XML, XSLT stylesheet, output XML). In this section, we'll examine how the .NET Framework answers these questions. In one example, you'll also see how to apply transformations to data held in ADO.NET DataSet objects. As test data for this section, we'll use the sample CakeCatalog.xml file and the CatalogTransform.xsl stylesheet shown earlier. Simple Transformations Using XslTransformThe XslTransform class lies at the heart of the .NET Framework support for the transformation of XML. Using XslTransform is fairly straightforward ”you simply provide it with the XML documents required and direct its output appropriately. To perform any transformation, the first thing you do is instantiate an XslTransform . You can then prime it with the particular transformation you want to apply to your source document using the Load method: XslTransformtransformer=newXslTransform(); transformer.Load("CatalogTransform.xsl"); You can now use the Transform method of the XslTransform instance to apply this particular transformation to any XML document to which you have access: transformer.Transform(sourceFile,targetFile); The sample J# application Transformer.jsl in the Transformer sample project shows how you can apply this simplest form of transformation. The program takes the names of three files ”one containing the source XML document, one containing the XSLT stylesheet, and another indicating the name of the target file in which to place the resulting XML document. Transformer.jslpackageTransformer; importSystem.*; importSystem.Xml.*; importSystem.Xml.Xsl.*; publicclassTransformer { publicstaticvoidmain(String[]args) { if(args.length!=3) { Console.WriteLine ("Usage:Transformersource.xmltarget.xmltransform.xsl"); Environment.Exit(1); } simpleTransform(args[0],args[1],args[2]); } privatestaticvoidsimpleTransform(Stringsource, Stringtarget,Stringstylesheet) { XslTransformtransformer=newXslTransform(); transformer.Load(stylesheet); transformer.Transform(source,target); } } As you can see, a very small amount of code is required to perform simple transformations. You can try out the Transformer sample application yourself by passing the CakeCatalog.xml and CakeTransform.xsl files seen previously as the source XML and XSLT files, respectively. The sample project provides these files as inputs and indicates that the output should be stored in the file TxCatalog.xml. Transformation Sources and TargetsYou can pass an XSLT stylesheet to the Load method in various forms:
Access to the source and target XML document are both defined as parameters to the Transform method. The source XML document can be
The target document can be specified as
There are also two forms of the Transform method that return an XmlReader that wraps up the resulting document. These are ideal when the resulting document must be processed in memory. Note Not all combinations of source and target are supported. See the .NET Framework documentation for the different forms of the Transform method. The following example shows how to use an XmlDocument as the input for a transformation. This approach works because the XmlDocument class implements IXPathNavigable : XslTransformtransformer=newXslTransform(); transformer.Load(stylesheet); XmlDocumentsourceDocument=newXmlDocument(); sourceDocument.Load(source); transformer.Transform(sourceDocument,null,Console.get_Out()); You can find the full code for a DOM-based transformation in the DomTransformer.jsl sample file. An XSLT stylesheet can include an xsl:output element that defines the preferred output format. The method attribute of this element can be set to xml , html , or text to specify the overall style of the output. The xsl:output element also has other attributes that determine the indentation of the output, the encoding, the document type to use, and so on. If you were to include the following line in the CatalogTransform.xsl file <xsl:outputmethod="text" /> the resulting output would simply be the text content of the document ”it would have no tags or XML declaration. Note that this element has no effect on the transformation result when the output is done through an XmlReader or XmlWriter . Transforming a DataSetAs you saw in the last chapter, it is possible to associate an XML document with an ADO.NET DataSet by using an XmlDataDocument . Because the XmlDataDocument class implements the IXPathNavigable interface, it can be used as a parameter to the Transform method. This means that data can be retrieved from a relational database into an ADO.NET DataSet and that data can then be transformed using an XSLT stylesheet using just a few lines of code: DataSetds=<datasource>//Obtaindatafromdatasource XmlDataDocumentdataDoc=newXmlDataDocument(ds); XslTransformtransformer=newXslTransform(); transformer.Load(stylesheet); //Maketheoutputprettythistime... XmlTextWriterwriter=newXmlTextWriter(Console.get_Out()); writer.set_Formatting(System.Xml.Formatting.Indented); transformer.Transform(dataDoc,null,writer); This code can be found in the sample file DataSetTransformer.jsl. Notice that it uses an XmlTextWriter to wrap standard output to set the formatting style to Indented . This adds line breaks and indentation, which makes the XML more human-readable . |
I l @ ve RuBoard |