Xml Namespace breaking my xpath!

Xml Namespace breaking my xpath!

Asked on January 9, 2019 in XML.
Add Comment


  • 7 Answer(s)

    Xml Namespace breaking xpath:

    /List/Fields/Field
    

         When removing the xmlns from the XML, the xPath will works as good. When it is there in my xPath will finds nothing.

    If we cannot able to register a namespace binding and cannot use assuming the registered prefix is “x”,

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

         Also there is another way,

    /*[name()='List']/*[name()='Fields']/*[name()='Field']
    
    Answered on January 9, 2019.
    Add Comment

         The elements in the list had been termed with a default namespace and this is adopted by all elements that are inside.

    User want to avoid the element namespace like below,

    /

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

    but it terms that the xpath will get any other element with List – Fields – Field

    Do a namespace check as well as a local-name check like,

    /*[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/']
    

         Else user can register the namespace with the library and then especially determine the prefix for that namespace and then add it to xpath expression, the method that which dependent on the library we are using.

    Answered on January 9, 2019.
    Add Comment

         The user need to register that namespace uri with their xpath library. Depending on the library, they can be able to use the ‘default’ prefix or they might need to give it a named prefix and use that in their xpath queries.

    Simple instance, in php using DOMXPath could do like this below,

    $xpath = new DOMXPath($document);
    $xpath->registerNamespace('x', 'http://schemas.microsoft.com/sharepoint/soap/');
    $xpath->query('/x:List/x:Fields/x:Field');
    
    Answered on January 9, 2019.
    Add Comment
    <List xmlns="http://schemas.microsoft.com/sharepoint/soap/">
     <Fields>
       <Field>
       </Field>
     </Fields>
    </List>
    Answered on February 25, 2019.
    Add Comment
    $xpath = new DOMXPath($document);
    $xpath->registerNamespace('x', 'http://schemas.microsoft.com/sharepoint/soap/');
    $xpath->query('/x:List/x:Fields/x:Field');
    Answered on February 25, 2019.
    Add Comment
    // 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 
    Answered on February 25, 2019.
    Add Comment
    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> – Anomie
    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.