Simplexml XPath的陌生人 [英] Simplexml XPath strangeness
本文介绍了Simplexml XPath的陌生人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?xml version =1.0encoding =UTF- 8\" >?;
<! - 由功能操纵引擎2009(Build 5658)生成 - >
< kml xmlns =http://earth.google.com/kml/2.2xmlns:atom =http://www.w3.org/2005/Atom>
<文件>
< name>南澳大利亚< / name>
< visibility> 1< / visibility>
< description><![CDATA [Statistical Local Area 2008]]>< / description>
< Folder id =kml_ft_SA_SLA08>
< name> SA_SLA08< / name>
< Placemark id =kml_1>
< name> Mitcham(C) - West< / name>
< Style>
<! - style info blah blah - >
< / Style>
<多边形>
<! - blah blah - >
< / Polygon>
< / Placemark>
<! - 删除更多地标 - >
< / Folder>
< / Document>
< / kml>
我遇到的问题是使用XPath从中选择任何东西!
$ doc = new DOMDocument();
$ doc-> load('myfile.xml'); //返回true
$ xp = new DOMXPath($ doc);
$ places = $ xp-> query(// Placemark);
echo $ places-> length; // - > 0 ?? !! ??
$ everything = $ xp-> query(// *); //(所以我知道XPath没有完全被borked)
echo $ everything-> length; // - > 2085
这里发生了什么?
解决方案
<?php
$ doc = new DOMDocument()
$ doc-> load('file.xml'); //返回true
$ xp = new DOMXPath($ doc);
$ xp-> registerNamespace('ge','http://earth.google.com/kml/2.2');
$ places = $ xp-> query(// ge:Placemark);
echo $ places-> length; // - > 0 ?? !! ??
$ everything = $ xp-> query(// *); //(所以我知道XPath没有完全被borked)
// echo $ doc-> saveXML();
显然,您必须注册'ge'命名空间并将其查询,至少这是什么几分钟后,我想起了。我想有时我们忘记我们在处理命名空间:p
I'm writing a script which reads and manipulates a KML (xml) document. Below is a snippet of the document I'm reading:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by Feature Manipulation Engine 2009 (Build 5658) -->
<kml xmlns="http://earth.google.com/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>South Australia</name>
<visibility>1</visibility>
<description><![CDATA[Statistical Local Area 2008]]></description>
<Folder id="kml_ft_SA_SLA08">
<name>SA_SLA08</name>
<Placemark id="kml_1">
<name>Mitcham (C) - West</name>
<Style>
<!-- style info blah blah -->
</Style>
<Polygon>
<!-- blah blah -->
</Polygon>
</Placemark>
<!-- snip lots more Placemarks -->
</Folder>
</Document>
</kml>
The problem I'm having is with using XPath to select anything from it!
$doc = new DOMDocument();
$doc->load('myfile.xml'); // returns true
$xp = new DOMXPath($doc);
$places = $xp->query("//Placemark");
echo $places->length; // --> 0 ??!!??
$everything = $xp->query("//*"); // (so I know that the XPath isn't fully borked)
echo $everything->length; // --> 2085
What's going on here?
解决方案
<?php
$doc = new DOMDocument();
$doc->load('file.xml'); // returns true
$xp = new DOMXPath($doc);
$xp->registerNamespace('ge', 'http://earth.google.com/kml/2.2');
$places = $xp->query("//ge:Placemark");
echo $places->length; // --> 0 ??!!??
$everything = $xp->query("//*"); // (so I know that the XPath isn't fully borked)
//echo $doc->saveXML();
Apparently you have to register the 'ge' namespace and query it as such, at least this is what I came up with after a few minutes googling. I guess sometimes we forget we're dealing with namespaces :p
这篇关于Simplexml XPath的陌生人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文