Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

No bonus points for you :-)

by space_monk (Chaplain)
on Mar 22, 2013 at 09:28 UTC ( [id://1024898]=note: print w/replies, xml ) Need Help??


in reply to Re: LibXML, XPath and Namespaces
in thread LibXML, XPath and Namespaces

What I really wanted to achieve was for the system to assume the default namespace was 'gt' so I didn't have to include it in the prefix in all XPath expressions.

It's fine when it's just one level deep e.g. EnvelopeVersion, but when you want to pick up a number of nodes 3 or 4 levels deep and keep having to repeat that 'gt:' at every level its a PITA.

I did mod your reply up for the effort though :-)

A Monk aims to give answers to those who have none, and to learn from those who know more.

Replies are listed 'Best First'.
Re: No bonus points for you :-)
by tobyink (Canon) on Mar 22, 2013 at 16:46 UTC

    "What I really wanted to achieve was for the system to assume the default namespace was 'gt' so I didn't have to include it in the prefix in all XPath expressions."

    Well, that would break XPath spec compliance. As per the XPath spec, node names with no colon always reference nodes with no namespace at all.

    Otherwise, if you could somehow set "gt" to be the default namespace for XPaths, you wouldn't be able to distinguish between the following two attributes:

    <gt:foo gt:bar="1" bar="2" />

    "It's fine when it's just one level deep e.g. EnvelopeVersion, but when you want to pick up a number of nodes 3 or 4 levels deep and keep having to repeat that 'gt:' at every level its a PITA."

    I enjoy golf as much as the next man, but is three characters per name really so bad? (You could always bind the namespace to just "g" so it was two characters.) I saved you having to construct XML::LibXML::XPathContext objects, didn't I??

    If your XPaths are fairly simple, you could take a look at XML::LibXML::QuerySelector which allows you to select nodes using CSS selectors. I wrote it for use with (X)HTML, but I don't see any reason it shouldn't roughly work with arbitrary XML.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Everything you said is true, but the addition of this adds obscurity to the XPath selectors. Namespace confusion doesn't really exist as the entire document is written in the specified namespace.

      I suppose another dirty way is to strip the xmlns attribute from the document before parsing it... :-)

      Anyway my basic question has been answered in the negative, so I'm going to simply have to live with the "clean" way of doing it....

      A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: No bonus points for you :-)
by ikegami (Patriarch) on Mar 24, 2013 at 07:12 UTC

    That wouldn't be a valid XPath. XPath "foo" matches child elements named "foo" in the null namespace. There's no way to specify a default namespace for nodetests in an XPath.

    Furthermore, gt is a prefix, not a namespace. http://www.govtalk.gov.uk/CM/envelope is the namespace in this case. gt is completely arbitrary, meaningless.

      Yes, I'm aware the prefix is meaningless. Word it how you like, I wanted some way of making a null/default prefix map to my namespace. I know that XML has its flaws and this seems to be one of them.

      A Monk aims to give answers to those who have none, and to learn from those who know more.
        It's a limitation of XPaths, not XML. It's entirely possible to define the default namespace in XML.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1024898]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-19 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found