I can't figure out whether XPath itself is to blame or whether it's the particular XPath implementations that make this so difficult. The SO question – How to change an an XML element in a namespace with MSDeploy Parameters.xml file? – was my inspiration.
What Doesn't Work
Here's the basic example that doesn't work.
XML:
<spring>
<objects xmlns="http://ift.tt/1ciLKrL">
<object id="CultureResolver" type="Spring.Globalization.Resolvers.SessionCultureResolver, Spring.Web">
<!--configure for server-->
<property name="DefaultCulture" value="en" />
</object>
</objects>
</spring>
XPath:
//spring/objects/object[@id='CultureResolver']/@type
What I Expect to Work
I would, perhaps naively, expect the following to work.
Modified XML:
<spring>
<spring:objects xmlns:spring="http://ift.tt/1ciLKrL">
<spring:object id="CultureResolver" type="Spring.Globalization.Resolvers.SessionCultureResolver, Spring.Web">
<!--configure for server-->
<spring:property name="DefaultCulture" value="en" />
</spring:object>
</spring:objects>
</spring>
Modified XPath query:
//spring/spring:objects/spring:object[@id='CultureResolver']/@type
What Does Work
Modified XML:
<spring xmlns="" xmlns:spring="http://ift.tt/1ciLKrL">
<spring:objects>
<spring:object id="CultureResolver" type="Spring.Globalization.Resolvers.SessionCultureResolver, Spring.Web">
<!--configure for server-->
<spring:property name="DefaultCulture" value="en" />
</spring:object>
</spring:objects>
</spring>
Modified XPath query (same as under What I Expect to Work):
//spring/spring:objects/spring:object[@id='CultureResolver']/@type
Why?
Is this confusing because of the interplay between namespaces and prefixes? It seems like declaring a namespace without a prefix not only includes the relevant element in that namespace but also includes all of its children too, hence describing it as a "default namespace" (like in this answer to a related question). Whereas declaring a namespace with a prefix doesn't even include the relevant element in that namespace!
Is there some reason why namespaces need to be included in the root element of the XML document, independent of particular XPath implementations?
No comments:
Post a Comment