xPath只能找到* [英] xPath finds nothing but *
问题描述
这开始让我很生气.我有以下XML代码:
This is starting to piss me off real bad. I have this XML code:
已使用正确的命名空间更新
<?xml version="1.0" encoding="utf-8"?>
<Infringement xsi:schemaLocation="http://www.movielabs.com/ACNS http://www.movielabs.com/ACNS/ACNS2v1.xsd" xmlns="http://www.movielabs.com/ACNS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Case>
<ID>...</ID>
<Status>Open</Status>
</Case>
<Complainant>
<Entity>...</Entity>
<Contact>...</Contact>
<Address>...</Address>
<Phone>...</Phone>
<Email>...</Email>
</Complainant>
<Service_Provider>
<Entity>...</Entity>
<Address></Address>
<Email>...</Email>
</Service_Provider>
<Source>
<TimeStamp>...</TimeStamp>
<IP_Address>...</IP_Address>
<Port>...</Port>
<DNS_Name></DNS_Name>
<Type>...</Type>
<UserName></UserName>
<Number_Files>1</Number_Files>
<Deja_Vu>No</Deja_Vu>
</Source>
<Content>
<Item>
<TimeStamp>...</TimeStamp>
<Title>...</Title>
<FileName>...</FileName>
<FileSize>...</FileSize>
<URL></URL>
</Item>
</Content>
</Infringement>
此PHP代码:
<?php
$data = urldecode($_POST["xml"]);
$newXML = simplexml_load_string($data);
var_dump($newXML->xpath("//ID"));
?>
我只转储了$ newXML并获得了大量数据,但是我运行过的唯一返回空数组的xPath是"*"
I've dumped only $newXML and gotten tons of data, but the only xPath I've run that returned anything but an empty array was "*"
"//ID"不是应该在文档中找到所有ID节点吗?为什么不起作用?
Isn't "//ID" supposed to find all ID nodes in the document? Why isn't it working?
谢谢
推荐答案
我只转储了$ newXML并得到了 大量的数据,但是我仅有的xPath 运行返回任何东西,但 空数组为"*"
I've dumped only $newXML and gotten tons of data, but the only xPath I've run that returned anything but an empty array was "*"
那么var_dump($newXML->xpath("*"));
返回了什么?
<Infringement>
?
如果问题是名称空间,请尝试以下操作:
var_dump($newXML->xpath("//*[local-name() = 'ID']"));
这将匹配文档中名称为'ID'的任何元素,而与命名空间无关.
This will match any element in the document whose name is 'ID', regardless of namespace.
如果我全部替换掉,我的东西就会起作用 "xmlns"和"ns"
My stuff works if i replace all "xmlns" with "ns"
等等,什么?您确定您向我们展示了文档中所有与xmlns相关的属性吗?
Wait, what? Are you sure you showed us all the xmlns-related attributes in the document?
更新: 对问题进行了编辑,以显示XML确实具有默认的名称空间声明.这就解释了最初的问题:由于默认的命名空间声明,您的XPath表达式选择了没有命名空间的ID元素,但是文档中的元素却位于movielabs ACNS命名空间中.
Update: The question was edited to show that the XML really does have a default namespace declaration. That explains the original problem: your XPath expression selects ID elements that are in no namespace, but the elements in your document are in the movielabs ACNS namespace, thanks to the default namespace declaration.
元素上的声明xmlns="http://www.movielabs.com/ACNS"
表示此元素和所有没有名称空间前缀(例如ID)的子代均位于名称空间URI'
The declaration xmlns="http://www.movielabs.com/ACNS"
on an element means "this element and all descendants that don't have a namespace prefix (like ID) are in the namespace represented by the namespace URI 'http://www.movielabs.com/ACNS'." (Unless an intervening descendant has a different default namespace declaration, which would shadow this one.)
因此,请使用我上面的local-name()
答案忽略名称空间,或使用jasso的技术来指定movielabs ACNS并按预期使用它.
So use my local-name()
answer above to ignore namespaces, or use jasso's technique to specify the movielabs ACNS and use it as intended.
这篇关于xPath只能找到*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!