A View Inside My Head

Jason's Random Thoughts of Interest

Gravatar

Steve (WWW) wrote at 08/14/06 10:45 am:    

Jason, I've been unable to get your solution working. I'm new to .Net, but I'm guessing from the last line in your post "if you could pass in a XmlNamespaceManager object." means that your work-around will not simply work within the XMLDataSource control itself, correct? I cannot just replace my XPath string with your longer version. I'm guessing it means I'll need to add code (in the page load method?) that initializes a XmlNamespaceManager object and some other bits of magic.


Gravatar

Jason F. (WWW) wrote at 08/14/06 11:01 am:    

Steve: My point was that XmlDataSource does not support XmlNamespaceManager, like object in the System.Xml namespace do. Therefore, the only way to use a XML document that contains namespaces with the XmlDataSource is to either transform it first to remove the namespaces, or to use the long/somewhat complicated XPath notation that I demonstrated.


Gravatar

Steve (WWW) wrote at 08/14/06 6:28 pm:    

I'll have to give it another attempt tonight, but I was unable to get your solution working in my situation. I believe (I don't have the code in front of me, that the XPath line I tried was:

/*[name()='gpx' and namespace-uri()='www.gpxsomething.com/v/1/1']/trk/ trkseg/trkpt

and

/*[name()='gpx' and namespace-uri()='www.gpxsomething.com/v/1/1']/ *[name()='trk']/*[name()='trkseg']/*[name()=' trkpt']

And neither of them worked. The original XPath I was trying to use was =

/gpx/trk/trkseg/trkpt


Gravatar

Jason (WWW) wrote at 08/14/06 6:44 pm:    

Oh, that's right... You're the reason I started this whole post!

For those following along at home, Steve's post is at:

http://forums.asp.net/thread/136...ad/ 1366619.aspx

His XML starts off with:

<gpx
version="1.1"
creator="SportTracks"
xmlns:st="urn:uuid:D0EB2ED5-49B6-44e3-B13C- CF15BE7DD7DD"
xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance"
xmlns="http://www.topografix.com/GPX/1/1"
schemaLocation="http://www.topografix.com/......." >


Notice the default namespace (xmlns="http://www.topografix.com/GPX/1/1")

This is a URI, not a URL. What's the difference? A URL is a web address. A URI is just some sort of identifier that you will continue to reuse in order to identify a resource. It could be 'Blah', or 'JasonRocks', or what most people do is make it a URL that they own, so that nobody else can (or would want to) claim it.

So, in this case, even the "http://" part is significant because "http://www..." is a different string than "www...".

Your namespace-qualified XPath would resemble:

/*[namespace-uri()='http://www.topografix.com/GPX/ 1/1' and name()='gpx']/*[name()='trk']/*[name()='trkseg']/ *[name()='trkpt']

BTW: The namespace-uri() part would actually be optional in your case, because everything is under the same namespace, so you could get away with just querying for local names /*[name()='foobar'], etc.

Also, Haloscan (the commenting engine for this blog) is inserting spaces into the provided XPath--ignore those.


Gravatar

Scott Hanselman (WWW) wrote at 08/14/06 8:35 pm:    

What are the performance ramifications?


Gravatar

Jason F. (WWW) wrote at 08/14/06 10:42 pm:    

I did a simple test comparing the "short" XPath syntax with a NamespaceManager to the "long" syntax using namespace-uri() and/or name():

I performed 10,000 iterations of XmlDocument.SelectNodes() and used the System.Diagnostics.StopWatch for timing (calculating the average afterwards).

In the end, the execution time of the long syntax was about 1.54 times longer than that of the short syntax. This seemed to be consistent for both shorter XML documents and longer ones as well (like the site feed for computerzen.com).

1.54 times seems like a lot, but this turned out to be around 65 ticks for evaluating "/rss/channel/item" versus 100 ticks for evaluating "/*[name()='rss'/*[name()='channel']/*[name()=' item']"


Gravatar

Cormac O'Brien wrote at 08/17/06 11:27 am:    

I ran into the same problem. I can't believe that XmlDataSource doesn't handle namespaces. But it does support stylesheets, so you can use a simple stylesheet to strip out all the namespaces. Basically create a copy of your document with all the elements and attributes moved to the namespace "". This would cause problems if you had a docoument with nodes of the same name in different namespaces, but in general is quite a simple solution. Here's a link to a stylesheet that worked for me:

http://www.tei-c.org.uk/wiki/ind...- Namespaces.xsl


Gravatar

Jason F. (WWW) wrote at 08/17/06 11:48 am:    

That's the same solution that Bill Evjen came up with.

One consideration, though, is that if you ever want to use the XmlDataSource to edit data (i.e., be able to modify and save it), then you cannot transform it (per the documentation, transformation is a condition that makes the datasource readonly).


Gravatar

J. Eiten wrote at 09/05/08 12:09 am:    

Thank you. Very clearly laid out. This was just the information I was looking for, especially the XPATH expression with name and namespace-uri qualifiers. Super! Thanks.


Gravatar

bucknersc wrote at 11/07/08 2:03 pm:    

thanks...this was a big help!


Gravatar

Jack Stow (WWW) wrote at 11/16/08 11:48 am:    

Many thanks for the post. After an afternoon of banging my head against the wall with XPath, I finally tracked it down to the namespace problem, used your method and it now all works!!


Gravatar

reale wrote at 05/28/09 6:07 am:    

Any sample code using XPathNavigator in C# ??

thanks.


Name:

Email (Accessible only to JasonF):

Your Homepage:

Comment:  ?

 

Commenting by HaloScan.com