您好我有一个xml格式的api响应,其中包含以下一系列项:
<item>
<title>blah balh</title>
<pubDate>Tue, 20 Oct 2009 </pubDate>
<media:file date="today" data="example text string"/>
</item>
Run Code Online (Sandbox Code Playgroud)
我想使用DOMDocument从标签"media:file"获取属性"data".我在下面的尝试不起作用:
$xmldoc = new DOMDocument();
$xmldoc->load('api response address');
foreach ($xmldoc->getElementsByTagName('item') as $feeditem) {
$nodes = $feeditem->getElementsByTagName('media:file');
$linkthumb = $nodes->item(0)->getAttribute('data');
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?请帮忙.
编辑:我不能因某些原因留下评论马克.我收到了错误
Call to a member function getAttribute() on a non-object
Run Code Online (Sandbox Code Playgroud)
当我运行我的代码.我也试过了
$nodes = $feeditem->getElementsByTagNameNS('uri','file');
$linkthumb = $nodes->item(0)->getAttribute('data');
Run Code Online (Sandbox Code Playgroud)
其中uri是与媒体名称空间(NS)相关的uri,但同样是同样的问题.
请注意,媒体元素的形式并不是我认为这是问题的一部分,因为我通常没有解析问题的问题.
Python elementTree似乎无法使用命名空间.我有什么选择?BeautifulSoup也很容易使用名称空间.我不想把它们剥掉.
特定python库如何获取命名空间元素及其集合的示例均为+1.
编辑:您能否使用您选择的库提供代码来处理这个真实世界的用例?
你将如何获得字符串'Line Break','2.6'和列表['PYTHON','XML','XML-NAMESPACES']
<?xml version="1.0" encoding="UTF-8"?>
<zs:searchRetrieveResponse
xmlns="http://unilexicon.com/vocabularies/"
xmlns:zs="http://www.loc.gov/zing/srw/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:lom="http://ltsc.ieee.org/xsd/LOM">
<zs:records>
<zs:record>
<zs:recordData>
<srw_dc:dc xmlns:srw_dc="info:srw/schema/1/dc-schema">
<name>Line Break</name>
<dc:title>Processing XML namespaces using Python</dc:title>
<dc:description>How to get contents string from an element,
how to get a collection in a list...</dc:description>
<lom:metaMetadata>
<lom:identifier>
<lom:catalog>Python</lom:catalog>
<lom:entry>2.6</lom:entry>
</lom:identifier>
</lom:metaMetadata>
<lom:classification>
<lom:taxonPath>
<lom:taxon>
<lom:id>PYTHON</lom:id>
</lom:taxon>
</lom:taxonPath>
</lom:classification>
<lom:classification>
<lom:taxonPath>
<lom:taxon>
<lom:id>XML</lom:id>
</lom:taxon>
</lom:taxonPath>
</lom:classification>
<lom:classification>
<lom:taxonPath>
<lom:taxon>
<lom:id>XML-NAMESPACES</lom:id>
</lom:taxon>
</lom:taxonPath>
</lom:classification>
</srw_dc:dc>
</zs:recordData>
</zs:record>
<!-- ... more records ... -->
</zs:records> …Run Code Online (Sandbox Code Playgroud) 对于"hello world"类型的xml文档:
<?xml version="1.0" encoding="UTF-8"?>
<!-- see http://www.w3.org/TR/REC-xml-names/ -->
<bk:book xmlns:bk="urn:loc.gov:books" xmlns:isbn="urn:ISBN:0-395-36341-6">
<bk:title>Cheaper by the Dozen</bk:title>
<isbn:number>1568491379</isbn:number>
</bk:book>
Run Code Online (Sandbox Code Playgroud)
你可以随意定义urn用于自定义目的吗?
jaxb如何确定编组对象的命名空间前缀声明列表?我使用xjc为ebics(ebics schema)编译java类.当我为ebicsRequest创建一个实例时,它看起来像这样:
<?xml version="1.0" encoding="UTF-16"?>
<ns2:ebicsRequest xmlns:ns2="http://www.ebics.org/H003" Revision="1" Version="H003" xmlns="http://www.ebics.org/H003" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:ns4="http://www.ebics.org/S001" xmlns:ns5="http://www.ebics.org/H000">
<ns2:header authenticate="true">
<ns2:static>
<ns2:HostID>SIZBN001</ns2:HostID>
<ns2:Nonce>A5488F43223063171CA0FA59ADC635F0</ns2:Nonce>
<ns2:Timestamp>2009-08-04T08:41:56.967Z</ns2:Timestamp>
<ns2:PartnerID>EBICS</ns2:PartnerID>
<ns2:UserID>EBIX</ns2:UserID>
<ns2:Product Language="de">EBICS-Kernel V2.0.4, SIZ/PPI</ns2:Product>
<ns2:OrderDetails>
<ns2:OrderType>FTB</ns2:OrderType>
<ns2:OrderID>A037</ns2:OrderID>
<ns2:OrderAttribute>OZHNN</ns2:OrderAttribute>
<ns2:StandardOrderParams/>
</ns2:OrderDetails>
<ns2:BankPubKeyDigests>
<ns2:Authentication Algorithm="RSA" Version="X002">...</ns2:Authentication>
<ns2:Encryption Algorithm="RSA" Version="E002">...</ns2:Encryption>
</ns2:BankPubKeyDigests>
<ns2:SecurityMedium>0000</ns2:SecurityMedium>
<ns2:NumSegments>1</ns2:NumSegments>
</ns2:static>
<ns2:mutable>
<ns2:TransactionPhase>Initialisation</ns2:TransactionPhase>
</ns2:mutable>
</ns2:header>
<ns2:AuthSignature>
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<ds:Reference URI="#xpointer(//*[@authenticate='true'])">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue>CSbjPbiNcFqSl6lCI1weK5x1nMeCH5bTQq5pedq5uI0=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>...</ds:SignatureValue>
</ns2:AuthSignature>
<ns2:body>
<ns2:DataTransfer>
<ns2:DataEncryptionInfo authenticate="true">
<ns2:EncryptionPubKeyDigest Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" Version="E002">dFAYe281vj9NB7w+VoWIdfHnjY9hNbZLbHsDOu76QAE=</ns2:EncryptionPubKeyDigest>
<ns2:TransactionKey>...</ns2:TransactionKey>
</ns2:DataEncryptionInfo>
<ns2:SignatureData authenticate="true">...</ns2:SignatureData>
</ns2:DataTransfer>
</ns2:body> …Run Code Online (Sandbox Code Playgroud) 我试图使用Lxml来解析.docx文档的内容.我知道lxml将命名空间前缀替换为实际的命名空间,但是这使得检查我正在使用的元素标记类型真的很痛苦.我希望能够做类似的事情
if (someElement.tag == "w:p"):
Run Code Online (Sandbox Code Playgroud)
但由于lxml坚持预先设置te ful命名空间,我要么必须做类似的事情
if (someElemenet.tag == "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}p'):
Run Code Online (Sandbox Code Playgroud)
或者像这样从元素的nsmap属性中查找完整的命名空间名称
targetTag = "{%s}p" % someElement.nsmap['w']
if (someElement.tag == targetTag):
Run Code Online (Sandbox Code Playgroud)
如果有一种更容易的方法来说服lxml
在编写此解析器时,这将节省大量的击键次数.这可能吗?我在文档中遗漏了什么吗?
我一直在使用XML格式,它混合了不同的现有格式和一些自定义元素和属性,我想我应该为这些自定义位编写一个架构.
我做的一件事是在现有格式的元素上使用自定义属性,如下所示:
<ns1:something attA="b" attB="a" ns2:extraAtt="c"/>
Run Code Online (Sandbox Code Playgroud)
我知道这样做是允许的,但我想不出如何在XML Schema中声明我的"extraAtt",或者更糟糕的是,在DTD中.
我正在尝试在 python 中使用 xml.etree 创建 shibboleth 配置文件,但我在输出完成的文档时遇到了省略命名空间分配的问题。我很确定这是使用 ElementTree 输出“未使用的”XML 命名空间中描述的问题
我宣布他们...
namespaces = {
'resolver': 'urn:mace:shibboleth:2.0:resolver',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'pc': 'urn:mace:shibboleth:2.0:resolver:pc',
'ad': 'urn:mace:shibboleth:2.0:resolver:ad',
'dc': 'urn:mace:shibboleth:2.0:resolver:dc',
'enc': 'urn:mace:shibboleth:2.0:attribute:encoder',
'sec': 'urn:mace:shibboleth:2.0:security',
}
for prefix, uri in namespaces.iteritems():
ET.register_namespace(prefix, uri)
Run Code Online (Sandbox Code Playgroud)
如果我打印出为文档定义的命名空间...
print json.dumps( ET._namespace_map, indent=True )
Run Code Online (Sandbox Code Playgroud)
我在那里看到我的命名空间..
{
"urn:mace:shibboleth:2.0:resolver:pc": "pc",
"http://schemas.xmlsoap.org/wsdl/": "wsdl",
"urn:mace:shibboleth:2.0:resolver:ad": "ad",
"urn:mace:shibboleth:2.0:resolver": "resolver",
"http://www.w3.org/1999/xhtml": "html",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
"urn:mace:shibboleth:2.0:security": "sec",
"urn:mace:shibboleth:2.0:attribute:encoder": "enc",
"http://www.w3.org/2001/XMLSchema": "xs",
"http://www.w3.org/2001/XMLSchema-instance": "xsi",
"http://www.w3.org/XML/1998/namespace": "xml",
"urn:mace:shibboleth:2.0:resolver:dc": "dc"
}
Run Code Online (Sandbox Code Playgroud)
当我将文档写入文件或将其转储到标准输出时,它会忽略“enc”和“pc”命名空间。与另一个问题一样,“enc”和“pc”不用于限定元素,而是用于限定属性值。我可以通过手动将它们添加到文档来强制它们显示...
root.set("xmlns:enc", "urn:mace:shibboleth:2.0:attribute:encoder")
root.set("xmlns:pc", "urn:mace:shibboleth:2.0:resolver:pc")
Run Code Online (Sandbox Code Playgroud)
这感觉就像我不应该有这样做。有没有办法在输出文档时强制 xml.etree …
我已将我的应用程序从JSF 1.2迁移到2.2.
它在java.sun.com域 上使用XML命名空间xmlns:f="http://java.sun.com/jsf/core".但是,Oracle的Java EE 7教程正在使用xmlns.jcp.org域名
上的XML命名空间xmlns:f="http://xmlns.jcp.org/jsf/core".
推荐哪一个,为什么会改变?
在开始之前:我知道子节点从父节点继承命名空间,这就是我的问题发生的原因.不幸的是,我发送XML的Web服务不接受没有命名空间的子节点,并且因为它是一个政府实体,所以它们的部分变化是不太可能的.
话虽这么说,我使用Spring-WS在我的应用程序和Web服务之间进行通信,因此框架使用变换器以一种方式使用转换器将我的有效负载源解析为框架的有效负载结果:
transformer.transform(Source, Result);
Run Code Online (Sandbox Code Playgroud)
在进行转换之前,我的XML有这两个节点,如下所示:
<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="3.10">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
Run Code Online (Sandbox Code Playgroud)
转换后,第二个命名空间被删除(正如我之前所说,我知道原因):
<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="3.10">
<NFe>
Run Code Online (Sandbox Code Playgroud)
我也知道我可以使用marshallers来实现相同的结果并自己编写解析代码.使用这种方法也是可以接受的,但我不知道使用除上面列出的方法之外的其他方法来实现相同的事情(转换javax.xml.transform.Source为javax.xml.transform.Result).
那我有两个问题:
1 - 我可以避免使用默认方法(不使用marshallers)的行为吗?
2 - 是否还有其他工具可以进行相同的转换?
xml-namespaces ×10
xml ×6
python ×3
java ×2
dom ×1
domdocument ×1
elementtree ×1
facelets ×1
java-ee-7 ×1
jaxb ×1
jsf ×1
jsf-2.2 ×1
lxml ×1
namespaces ×1
parsing ×1
php ×1
shibboleth ×1
spring-ws ×1
xml-parsing ×1
xsd ×1