13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Console.WriteL<strong>in</strong>e("Exception: {0}",e.GetType().ToStr<strong>in</strong>g());}}}return;The heart of the application is found <strong>in</strong> the follow<strong>in</strong>g three l<strong>in</strong>es of rather selfexplanatorycode:XslTrans<strong>for</strong>m xslt = new XslTrans<strong>for</strong>m();xslt.Load(stylesheet);xslt.Trans<strong>for</strong>m(source, output);The style sheet can be loaded from a variety of sources, <strong>in</strong>clud<strong>in</strong>g XPath documents,<strong>XML</strong> readers, local disk files, and URLs. The Load method compiles the style sheet anduses the stored <strong>in</strong><strong>for</strong>mation to <strong>in</strong>itialize the XSLT processor. When Load returns, theprocessor is ready to per<strong>for</strong>m any requested trans<strong>for</strong>mation.The Trans<strong>for</strong>m method loads an <strong>XML</strong> document, runs the XSLT script, and writes theresults to the specified stream. Trans<strong>for</strong>m is particularly handy, because it saves youfrom explicitly load<strong>in</strong>g the source document and creat<strong>in</strong>g the output file. As we'll seemore <strong>in</strong> detail <strong>in</strong> the section "Per<strong>for</strong>m<strong>in</strong>g Trans<strong>for</strong>mations," on page 314, Trans<strong>for</strong>muses an <strong>in</strong>termediate XPath document to trans<strong>for</strong>m the <strong>XML</strong>.NoteSeveral other programm<strong>in</strong>g environments allow you to exercise totalcontrol over the XSLT process. In particular, <strong>in</strong> <strong>Microsoft</strong> W<strong>in</strong>32, thecomb<strong>in</strong>ed use of two dist<strong>in</strong>ct <strong>in</strong>stances of the <strong>Microsoft</strong>.<strong>XML</strong>DOMCOM object lets you programmatically per<strong>for</strong>m an XSLTtrans<strong>for</strong>mation. The follow<strong>in</strong>g JScript code illustrates how toproceed:// Collects arguments from the WSH command l<strong>in</strong>esource = WScript.Arguments(0);stylesheet = WScript.Arguments(1);output = WScript.Arguments(2);// Instantiates the <strong>XML</strong>DOM <strong>for</strong> the sourcexml = new ActiveXObject("<strong>Microsoft</strong>.<strong>XML</strong>DOM");xml.load(source);// Instantiates the <strong>XML</strong>DOM <strong>for</strong> the style sheetxsl = new ActiveXObject("<strong>Microsoft</strong>.<strong>XML</strong>DOM");xsl.load(stylesheet);// Creates the outputfso = newActiveXObject("Script<strong>in</strong>g.FileSystemObject");f = fso.CreateTextFile(output);f.Write(xml.trans<strong>for</strong>mNode(xsl.documentElement));f.Close();251

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!