ChatGPT解决这个技术问题 Extra ChatGPT

Xml Namespace breaking my xpath!

I have the following XML:

<List xmlns="http://schemas.microsoft.com/sharepoint/soap/">
 <Fields>
   <Field>
   </Field>
 </Fields>
</List>

This is a slimmed down version of XML being returned from a SharePoint web service. I also have the following xPath:

/List/Fields/Field

When I remove the xmlns from my XML the xPath works fine. When it's in there my xPath finds nothing. Is there something I should be doing differently with my xPath? Modifying the XML is not an option.

No that is not a duplicate. That question specifices C# while this is strictly xpath/xml. Also, his namespace isn't blank, while mine is.
You need some level of abstraction... You are asking how to select elements under the default namespace with an XPath expression. This is the most FAQ.

P
Patrick from NDepend team

I also have the following xPath: /List/Fields/Field When I remove the xmlns from my XML the xPath works fine. When it's in there my xPath finds nothing

If you cannot register a namespace binding and cannot use (assuming the registered prefix is "x"):

/x:List/x:Fields/x:Field

then there is another way:

/*[name()='List']/*[name()='Fields']/*[name()='Field']

and how can the attributes be accessed then?
@KaiJauk, unprefixed attributes are always in "no namespace" regardless of their parent-element being or not being in the scope of a default namespace. So, no problem. Of course, one could also use @*[name() = 'myName'] or @*[name() = 'myPrefix:myName']
@36305 ah okay, this means I can use it like /*[local-name()="mynode" and @myAttr="value"]?
If the namespace you're referring to has prefix (xmlns:x), name() will include the prefix (x:List). local_name() gives you the unprefixed element name (List).
@andrew.rockwell Yes
r
rogermushroom

The List element has been defined with a default namespace and this is adopted by all elements inside.

You therefore need to ignore the element namespace like so:

/*[local-name()='List']/*[local-name()='Fields]/*[local-name()='Field]

but this means that the xpath will pick up any other element with List - Fields - Field

You can do a namespace check as well as a local-name check like so:

/*[local-name()='List' and namespace-uri()='http://schemas.microsoft.com/sharepoint/soap/']/*[local-name()='Fields' and namespace-uri()='http://schemas.microsoft.com/sharepoint/soap/']/*[local-name()='Field' and namespace-uri()='http://schemas.microsoft.com/sharepoint/soap/']

Or you can register the namespace with the library and then explicitly specify the prefix for that namespace and add it to xpath expression, the method of which is dependent on the library you are using.


This answer is working whereas accepted answer is not working....Thanks @zode64 for this help. I was straggling to get my xpath expression get working for the name space included xml. Now with this I need not to worry anymore about the name space. Thanks a ton..
A
Anomie

You most likely have to register that namespace uri with your xpath library. Depending on the library, you may be able to use the 'default' prefix or you may need to give it a named prefix and use that in your xpath queries.

For example, in php (since you didn't specify a language) using DOMXPath you could do something like this:

$xpath = new DOMXPath($document);
$xpath->registerNamespace('x', 'http://schemas.microsoft.com/sharepoint/soap/');
$xpath->query('/x:List/x:Fields/x:Field');

Yeah, I'm a .NET guy and normally my solution would be the .NET equivalent of that. Unfortunately we are using a third party "rapid" development environment that won't give us the ability to do this...
Also, why do they even include a namespace if there is no prefix? (the x: portion of your xPath)
There is a namespace. It's just using the 'default' prefix in this particular file. The file would be semantically identical were it <foo:List xmlns:foo="http://schemas.microsoft.com/sharepoint/soap/"><foo:Fields><foo:Field></foo:Field></foo:Fields></foo:List>
@Abe Miessler The namespace you've mentioned in your xml is the default namespace for the root element and its child. In your xpath you have to use the same uri namespace, but not the same prefix, which is only a label. If you don't register your namespace, your xpath refers to element with empty uri. You can also register your namespace with empty prefix, so your xpath doesn't need 'x:' prefix.
The ability to bind namespaces is so fundamental to an XPath library that if your library doesn't provide this capability, you should throw it out.
G
Greg Domjan

I've just been having this issue while using Xalan-c

The bit I didn't quite get initially is that the XPath or XSLT namespace aliases/prefixes can be different to that of the document - depending on your namespace resolver.

It appears that if there is a namespace on the doc, then it fails to match a path element unless a namespace is used. (standard but not always followed?)

The XalanDocumentPrefixResolver will map XPath or XSLT namespaces to URI and try and give them id by getting the prefix - where there is no prefix it used the name which turned into xmlns

/xmlns:List/xmlns:Fields/xmlns:Field

Alternatively you could create your own resolver, but it still requires a minimal namespace used in the xpath :(

Here is one I hacked together while testing, no guarantee of memory

// don't care what prefix given, there can only be the one
struct NoPrefixResolver : public xalanc::PrefixResolver {

    NoPrefixResolver(const xalanc::XalanDOMString&   theURI) : m_uri(theURI){}

    virtual const xalanc::XalanDOMString*
        getNamespaceForPrefix(const xalanc::XalanDOMString&     prefix) const {
        return &m_uri;
    }

    virtual const xalanc::XalanDOMString&   getURI() const {
        return m_uri;
    }

    const xalanc::XalanDOMString    m_uri;
};

/x:List/x:Fields/x:Field 
/a:List/b:Fields/c:Field 

D
David Gausmann

If you can skip the document element, the following XPath can also help:

//Fields/Field

This works as long as you don't have 'Fields' below any other node and as long the sub nodes have no namespace.