Subject:Xpath to return xmlns Author:Neal Walters Date:09 Jan 2007 11:00 AM
Is there an xpath to return the xmlns without having to define any namespace?
When I use Stylus to generate xpath I get:
/*[local-name()="MyRoot" and namespace-uri()="http://MyRoot"]/@xmlns:ns0
Can I make this more generic to return whatever the value of xmlns is, regardless of whether it is ns0 or x1? And can I do it without having to define the ns0?
I'd like something like //[prefix=xmlns[1] - in other words give me the value of the first attribute that is found that has a prefix of xmlns.
Is it possible? I'm trying to do this in an MS/SQL2005 xquery.
Subject:Xpath to return xmlns Author:(Deleted User) Date:09 Jan 2007 11:32 AM
Hi Neil,
what you want to do is not entirely doable in XPath, because the attributes starting with "xmlns" are not part of the 'attribute::' axis, but belong to the 'namespace::' axis.
So the query you are looking for is /*/namespace::*[1]
But with this caveats:
1) the axis contains all the xmlns:xxx attributes defined on the element AND on all the parent elements, plus the predefined 'xml' namespace
2) the order is implementation defined, so you need to test where SQL2005 places the 'xml' namespace
3) not all XPath 1.0 processors implement this axis, and the ones who did had so many headaches that XPath 2.0 made it deprecated and optional (and XQuery removed it altogether)