Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

The behaviour you saw is absolutely correct and not a bug at all. To quote the author of libxml2 from a message aptly titled Re: [xml] XPath and default namespaces (bet you're sick of this by now :) ):

You cannot define a default namespace for XPath, period, don't try you can't, the XPath spec does not allow it. This can't work and trying to add it to libxml2 would simply make it non conformant to the spec.

In a nutshell forget about using default namespace within XPath expressions, this will *never* work, you *can't* !

Google [daniel veillard default namespace xpath] if you want more.

As he says, XPath has no notion of a default namespace. //lastName in an XPath expression always matches that element in the null namespace, not the default namespace. According to the spec:

A QName in the node test is expanded into an expanded-name using the namespace declarations from the expression context. This is the same way expansion is done for element type names in start and end-tags except that the default namespace declared with xmlns is not used: if the QName does not have a prefix, then the namespace URI is null (this is the same way attribute names are expanded).

In //sdnList:lastName, sdnList is not a namespace. Only URIs can be namespaces. The stuff in front of the colon is the prefix, and is merely a stand-in for the URI. <sdnList xmlns="http://tempuri.org/sdnList.xsd"> puts the sdnList element (and all its prefix-less descendants) in the http://tempuri.org/sdnList.xsd namespace. You have to associate this URI with a prefix, then use the prefix in your expression. This is exactly the approach lestrrat posted:

my $xc = XML::LibXML::XPathContext->new( $doc->documentElement() ); $xc->registerNs( foobar => 'http://tempuri.org/sdnList.xsd' ); my $result = $xc->findvalue( '//foobar:lastName' );

I wrote about this a while ago.

Note that the prefix is arbitrary and has nothing to do with what appears in your document. This is as it should be, because the following document means exactly the same as the one you have:

<camel:sdnList xmlns:camel="http://tempuri.org/sdnList.xsd"> <camel:sdnEntry> <camel:lastName>Hello world!</camel:lastName> </camel:sdnEntry> </camel:sdnList>

For that matter, even this means the same:

<camel:sdnList xmlns:camel="http://tempuri.org/sdnList.xsd"> <penguin:sdnEntry xmlns:penguin="http://tempuri.org/sdnList.xsd"> <camel:lastName>Hello world!</camel:lastName> </penguin:sdnEntry> </camel:sdnList>

Or this:

<sdnList xmlns="http://tempuri.org/sdnList.xsd"> <penguin:sdnEntry xmlns:penguin="http://tempuri.org/sdnList.xsd"> <lastName>Hello world!</lastName> </penguin:sdnEntry> </sdnList>

You get the idea.

Makeshifts last the longest.


In reply to Re^2: Namespaced XML::LibXML XPath query (not a bug) by Aristotle
in thread Namespaced XML::LibXML XPath query by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (5)
    As of 2014-09-21 06:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (166 votes), past polls