6

Is it a bug or per design that xmlns attribute is not ignored?

(cake version 0.33.0)


With an Xml like so (a too simplified nuspec file):

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <!-- Continuously updated elements -->
        <version>3.0.0</version>
    </metadata>
</package>

I do a naÏve call var x = XmlPeek( "my.nuspec", "/package/metadata/version/text()" );
ad get the result x==null.

So I specify the namespace like so:

var settings = new XmlPeekSettings{
    Namespaces = new Dictionary<string, string> {{ 
        "ps", "http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" 
    }}
};
var x = XmlPeek( "my.nuspec", "/ps:package/ps:metadata/ps:version/text()", settings);

and get the result x==3.0.0 I anticipated.

LosManos
  • 6,117
  • 5
  • 44
  • 81

1 Answers1

4

It is not a bug.

To ignore the namespace you can use namespace agnostic xpath such as local-name():

var x = XmlPeek( "my.nuspec", "/*[local-name() = 'package']/*[local-name() = 'metadata']/*[local-name() = 'version']/text()");

or if you have only one version node:

var x = XmlPeek( "my.nuspec", "//*[local-name()='version']/text()");

but be careful with documents with large numbers of elements - this can become very slow.

Roman Marusyk
  • 19,402
  • 24
  • 55
  • 90