Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cake PeekXml does not "ignore" namespace

Tags:

cakebuild

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.

like image 609
LosManos Avatar asked Jul 11 '19 21:07

LosManos


1 Answers

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.

like image 95
Roman Marusyk Avatar answered Sep 19 '22 17:09

Roman Marusyk