XPath query returning 'false' in SimpleXML
I have an xml fragment as such:
<meta_tree type="root">
<meta_data>
<meta_cat>Content Provider</meta_cat>
<data>Mammoth</data>
</meta_data>
<meta_data>
<meta_cat>Genre</meta_cat>
<data>Games</data>
</meta_data>
<meta_data>
<meta_cat>Channel Name</meta_cat>
<data>Games Trailers</data>
</meta_data>
<meta_data>
<meta_cat>Collection</meta_cat>
<data>Strategy</data>
</meta_data>
<meta_data>
<meta_cat>Custom 1</meta_cat>
<data>PC</data>
</meta_data>
<meta_data>
<meta_cat>DRM Protected</meta_cat>
<data>N</data>
</meta_data>
<meta_data>
<meta_cat>Aspect Ratio</meta_cat>
<data>16:9</data>
</meta_data>
<meta_data>
<meta_cat>Streaming Type</meta_cat>
<data>VOD</data>
</meta_data>
</meta_tree>
which I garnered from the snippet of $meta_tree->asXML()
.
So given that, I need to have an xpath query for each element, so I'm using:
$meta_tree->xpath("/meta_data[meta_cat='Content Provider']");
but this returns false.
I have tried:
/meta_tree/meta_data[meta_cat='Content Provider']
//meta_data[meta_cat='Content Provider']
meta_data[meta_cat='Content Provider']
(this returns an array, but the array is empty)
I've been using AquaPath, which validates my query, so I'm not sure what I'm doing wrong.
Anyone got any ideas?
DJS.
EDIT: As per Tolomak's suggestion, here's some more info.
The element is a fragment of a larger doc, as follows (names have been replaced with *** to protect the innocent):
<contents total_items="1" xmlns="http://rxml.***.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://rxml.***.com/xsd/*Ingestion.xsd ">
<content item_id="451195">
...
<meta_tree type="root">
... see snippet above ...
</meta_tree>
</content>
</contents>
I retrieve the fragment above by:
$dom = new SimpleXMLElement(... xml_string ... );
foreach($dom->content as $content)
{
$contentMetadata = $this->getMetadata($content->meta_tree)
}
public function getMetadata($meta_tree)
{
echo $meta_tree->asXML();
}
ANSWER: problem was the default namespace. I have chosen to strip them out rather than deal wi开发者_如何学编程th the namespace, unless anyone knows how to register the xmlns default namespace in SimpleXMLElement.
try
$meta_tree->xpath("meta_data[meta_cat='Content Provider']")
e.g.
<?php
$meta_tree = new SimpleXMLElement(getData());
$foo = $meta_tree->xpath("meta_data[meta_cat='Content Provider']");
var_dump($foo);
function getData() {
return '<meta_tree type="root">
<meta_data>
<meta_cat>Content Provider</meta_cat>
<data>Mammoth</data>
</meta_data>
<meta_data>
<meta_cat>Genre</meta_cat>
<data>Games</data>
</meta_data>
<meta_data>
<meta_cat>Channel Name</meta_cat>
<data>Games Trailers</data>
</meta_data>
<meta_data>
<meta_cat>Collection</meta_cat>
<data>Strategy</data>
</meta_data>
<meta_data>
<meta_cat>Custom 1</meta_cat>
<data>PC</data>
</meta_data>
<meta_data>
<meta_cat>DRM Protected</meta_cat>
<data>N</data>
</meta_data>
<meta_data>
<meta_cat>Aspect Ratio</meta_cat>
<data>16:9</data>
</meta_data>
<meta_data>
<meta_cat>Streaming Type</meta_cat>
<data>VOD</data>
</meta_data>
</meta_tree>';
}
prints
array(1) {
[0]=>
object(SimpleXMLElement)#2 (2) {
["meta_cat"]=>
string(16) "Content Provider"
["data"]=>
string(7) "Mammoth"
}
}
Problem was the default namespace. I have chosen to strip them out rather than deal with the namespace, unless anyone knows how to register the xmlns default namespace in SimpleXMLElement.
精彩评论