This is starting to piss me off real bad. I have this XML code:
Updated with correct namespaces
<?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>
And this PHP code:
<?php
$data = urldecode($_POST["xml"]);
$newXML = simplexml_load_string($data);
var_dump($newXML->xpath("//ID"));
?>
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 "*"
Isn't "//ID" supposed to find all ID nodes in the document? Why isn't it working?
Thanks
So what was returned from
var_dump($newXML->xpath("*"));
?<Infringement>
?If the problem is namespaces, try this:
This will match any element in the document whose name is 'ID', regardless of namespace.
Wait, what? Are you sure you showed us all the xmlns-related attributes in the document?
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.
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.)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.