问题描述:

All

I used the XPath lib in Python language. but I can't get some attribute with colon in my xml file.

The xml file content:

 xmlns:rtr="http://www.reuters.com/ns/2003/08/content"

<packageItem conformance="power" guid="2013-07-17 15:53:21" standard="NewsML-G2" standardversion="2.14" version="1" xml:lang="en">

<itemClass qcode="icls:composite" rtr:msgType="S"/>

These are two method I have written.

def getxmlVal(self, expr):

data = xpath.findvalue(expr, self.doc)

return data

def getValnamespaces(self, expr, namespace):

context = xpath.XPathContext(default_namespace=namespace)

return context.findvalue(expr, self.doc);

I also tried:

for the method getxmlVal

  1. expr=

    "//packageItem/itemMeta/itemClass/*[local-name()='rtr:msgType' and

    namespace-uri()='http://www.reuters.com/ns/2003/08/content']";

  2. expr= "//itemMeta/itemClass/*[name()='rtr:msgType']";

for the method getValnamespaces

expr= "//itemMeta/itemClass/*[name()='rtr:msgType']";

namespace = "http://www.reuters.com/ns/2003/08/content";

but I can get None

What's wrong with my method?

Thanks

网友答案:

You need to use @ to match an attribute, and you shouldn't specify the namespace while trying to match local-name():

expr= "//packageItem/itemMeta/itemClass/@*[local-name()='msgType' and namespace-uri()='http://www.reuters.com/ns/2003/08/content']";
相关阅读:
Top