Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

And now a small lesson on XML namespaces

no, seriously

In namespace-aware XML documents, an element name is a qualified name (qname), composed by a prefix and a local name, separated by a colon (:). The prefix is bound to a URI via a namespace declaration. Example:

<first> <ns:second> <my:third xmlns:my="someURI"> <fourth xmlns="otherURI"> <fifth/> </fourth> </my:third> </ns:second> </first>

Let's read that. The element named first has a local name of first, and belongs to no namespaces. The element named ns:second is wrong, since namespace-aware parsers require the prefix to be declared, and ns is not. my:third belongs to the someURI namespace, which is locally bound to the my prefix. fourth belongs to the (locally default) namespace otherURI, as does fifth.

Hope this is clear enough...

Your problem

XPath has some problems with namespaces, namely that an XPath expression is interpreted in the context element (which in the case of your program is the invocant of findnodes). So the prefixes are resolved using the namespace declarations visible from that node. This forces you to know the prefixes used in the document, instead of the URIs, which creates the problems I said earlier (prefixes are not unique, URIs are).

Anyway, your problem is much easier: $tree is a XML::LibXML::Document, which has no knwoledge of namespaces, since they are declared (at the earliest) on the document element. This is why the second form works. BTW, in the previous examples (disregarding the ns:second element), if you did:

$docElem->findnodes('//my:third');
It wouldn't work, since the my prefix is not defined on the document element...

-- 
        dakkar - Mobilis in mobile

In reply to Re: XML::LibXML and XML Namespaces (processing OpenOffice documents) by dakkar
in thread XML::LibXML and XML Namespaces (processing OpenOffice documents) by tomhukins

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found