From: Stefan Behnel on 15 May 2010 17:25 Adam Tauno Williams, 15.05.2010 23:04: > On Sat, 2010-05-15 at 22:58 +0200, Stefan Behnel wrote: >> Adam Tauno Williams, 15.05.2010 22:40: >>> On Sat, 2010-05-15 at 22:29 +0200, Stefan Behnel wrote: >>>> Adam Tauno Williams, 15.05.2010 20:37: >>>>> Say I have an XML document that begins with: >>>>> <?xml version="1.0" encoding="utf-8"?> >>>>> <dsml:dsml xmlns:dsml="http://www.dsml.org/DSML"> >>>>> How can one access the namespaces define in this node? I've done a fair >>>>> amount of XML in Python, but haven't been able to uncover the call to >>>>> enumerate the namespaces. >>>>> Primarily I am using etree from lxml. >>>> What do you need the namespaces for? >>> One needs to know the defined namespace in order to perform xpath >>> operations. >> Well, yes, but unless you already know the namespace (URI), you can't know >> what the tag you find signifies in the first place. >> Unless, obviously, you are confusing namespaces with namespace prefixes. >> But you don't need to know the prefixes for XPath. >> Does this help? >> http://codespeak.net/lxml/xpathxslt.html#namespaces-and-prefixes > > I know that. I'm getting an XML document and an xpath and need to > execute it. But i have to tell xpath via namespaces= the prefixes& > namespaces; so I need to get that data out of the document. Ah, you didn't provide that information in your initial post. So you control neither the document nor the XPath expression, right? Can't you get the namespace-prefix mapping from your user? After all, he/she is the only one who knows the meaning of the XPath expression. I'd just reject any expression with an undefined prefix. BTW, I'm still not sure I understand your problem. Could you provide some more details? Stefan
From: Martin v. Loewis on 15 May 2010 17:37 > BTW, I'm still not sure I understand your problem. Could you provide > some more details? > Wouldn't it be easier if you told the OP how to access the prefix mappings in lxml etree, or, if this was actually not possible, admitted that it is actually not possible? FWIW, in the DOM, you look at all attributes of an element node, and search for those whose namespace is "http://www.w3.org/2000/xmlns/" In SAX, you watch the startPrefixMapping events. Regards, Martin
From: Adam Tauno Williams on 15 May 2010 17:43 On Sat, 2010-05-15 at 23:25 +0200, Stefan Behnel wrote: > Ah, you didn't provide that information in your initial post. So you > control neither the document nor the XPath expression, right? Correct. > Can't you get the namespace-prefix mapping from your user? Nope. Or they are not going to be expecting to have to [since this works just fine in Java, which for this feature we are pretty much porting from]. > After all, he/she is the only one who knows the meaning of the XPath expression. ??? The namespaces are embedded in the document. Personally I find it odd I have to tell xpath about the namespace of the document it is a $*&@(*& method of. > I'd just reject any expression with an undefined prefix. Which I'm okay with; whether the prefix is undefined can be determined from the document! > BTW, I'm still not sure I understand your problem. Could you provide some > more details? This is an action in a workflow action (business process modeling). It has an input message of an XML document and a parameter of an xpath; it invokes a subordinate action [think: foreach] for each node resulting from the expression. -- Adam Tauno Williams <awilliam(a)whitemice.org> LPIC-1, Novell CLA <http://www.whitemiceconsulting.com> OpenGroupware, Cyrus IMAPd, Postfix, OpenLDAP, Samba
From: Martin v. Loewis on 15 May 2010 20:37 > ??? The namespaces are embedded in the document. Personally I find it > odd I have to tell xpath about the namespace of the document it is a > $*&@(*& method of. How so? Why do you say it's a "method", and why do you say "of"? Usually, xpath expressions are *not* part of the document they operate on, but part of the code that performs the operation. Consequentially, the namespace prefixes in the xpath expression do *not* occur in the document (other than by chance), but are defined by whoever writes the xpath expression. That is typically somebody different from the one writing the document - if you would always write them together, you wouldn't need xpath in the first place, but could produce the selection result right away. Regards. Martin
From: Adam Tauno Williams on 16 May 2010 00:00 On Sat, 2010-05-15 at 23:37 +0200, Martin v. Loewis wrote: > > BTW, I'm still not sure I understand your problem. Could you provide > > some more details? > Wouldn't it be easier if you told the OP how to access the prefix :) > mappings in lxml etree, or, if this was actually not possible, admitted > that it is actually not possible? I suspect that it is not; but that seems rather surprising. > FWIW, in the DOM, you look at all attributes of an element node, and > search for those whose namespace is "http://www.w3.org/2000/xmlns/" > In SAX, you watch the startPrefixMapping events. Given that XML documents can be very large I'd rather avoid a parsing of the document [beyond what lxml/etree] has already done] just to retrieve the namespaces and their prefixes.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Pyinstaller on WINE - cannot install pywin32 Next: Access to comp.lang.python |