Cake PeekXml does not "ignore" namespace
Asked Answered
G

1

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.

Garrett answered 11/7, 2019 at 21:41 Comment(0)
C
5

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.

Centime answered 17/7, 2019 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.