Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Xwhat ? Xpath !

This blog is a great way to organize my thoughts and has some great sparring partners, special thanks to Joseph Cooney. It all started with an involuntary collision with xslt, after which I tried to get a better grab on xsl(t). After rereading my books on the subject (Dino Esposito's applied XML programming (MS-Press), and Dan Wahlin's XML for ASP.NET developers (Sams)) I'll try to summarize what I have learned.

Problem : Create an (XML) document out of an other XML-document.

Approach 1 : Use a XSL style-sheet.
An XSL stylesheet describes the layout of the output document. The stylesheet is composed of templates. When processing the input document the values of the document's nodes determine which template of the style sheet is used to transform the node. Selecting nodes is done in XPATH expressions, like MyDataSet/NortwindEmployees/Employee. The stylesheet and the the input document are processed by the XSLTransformer. To process multiple nodes XSLT has instructions like for-each, for conditional processing XSLT has instructions like if test and when. An XSLTransformer can be activated by IE which leads to a bang when it tries to transform an aspx page, which started my ramblings. According to Frans Bouma that is mainly due to the older XML engine of IE. The transformer can also be started from a .net application. The XML component uses it or you can program it using the System.Xml.Xsl.XslTransform class.

Approach 2 : Use a XMLdocument object
If you want to bypass XSLT you can approach in and output documents using the DOM (Document Object Model). The framework has the System.Xml.XmlDocument and the System.Xml.XmlDatadocument class. Objects of these classes can load an XML document, or another datasource, into memory. They provide a very rich programming interface to manipulate a document. A very interesting method is CreateNavigator(), the result of this method gives you the opportunity to use XPATH again to navigate through the (input) document.

Approach 3 : Use XQuery
This is a whole new ballgame. Xquery relates to XML as SQL relates to databases. Xquery has all the possibilities of XPATH and of XSLT, is predicted to swallow them, but is not available as an usable implementation yet.

In the first place I am an OOP programmer. In the early days of .net, coming from Delphi,  I had been playing with the Delphi (t)XMLdocument wrapper class to catch XML datasets coming in over a web service. Which worked pretty well. Living in the .net world I am confronted with a lot more creatures of the xml family. Some, like xslt, scare me as they are somewhat difficult to catch. People rave about xsl(t), when specifying their delights they mainly mention the XPATH part of it. But XPATH is not limited to XSLT, in the .net framework it is also available when you're doing DOM-programing. And that's something I do understand, I'll use the second approach.

Blog on,

Peter


Posted 11-19-2003 1:39 PM by pvanooijen

[Advertisement]

Comments

JosephCooney wrote re: Xwhat ? Xpath !
on 11-19-2003 4:08 PM
Re: XPath - I also find the SelectNodes and SelectSingleNode methods (which are members of XmlNode which pretty much everything in the Xml namespace is derived from) to be very useful. You pass it an XPath expression and get back a list of nodes (for SelectNodes) or a single node (for SelectSingleNode). I believe that creating an XPathNavigator is more performant, and using the more light-weight XPathDocument (rather than XmlDocument or XmlDataDocument) is also supposed to help performance. I'm fairly sure Dino's book covers this. Having said all that I've never noticed any perf. problems with just using SelectNodes and SelectSingleNode.
Peter van Ooijen wrote re: Xwhat ? Xpath !
on 11-20-2003 2:17 PM
Dino is pretty short on the XpathDocument class. It's supposed to be very performant. In Whidbey Xpathdocument is far more prominent.
Derek Williams wrote re: Xwhat ? Xpath !
on 04-26-2004 12:15 PM
I've been reading Effective XML - Good book, you may want to pick it up.
Peter van Ooijen wrote re: Xwhat ? Xpath !
on 04-26-2004 3:54 PM
That's Don Box's book, isn't it ?
It's on my wish list. He's indeed a hell of an author.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?