DOM XPath查询帮助 [英] DOM XPath Query Help

查看:90
本文介绍了DOM XPath查询帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这个xpath的查询使我疯狂。我想要做的是搜索一个特定课程类型的高尔夫课程的xml文件(在这种情况下是用于google maps api的kml文件),并抓取每个匹配的< Placemark> 元素,以便我可以使用结果创建一个新的xml对象,以便我可以进一步过滤一个新的查询。问题是我似乎无法弄清楚如何获得每个匹配的< Placemark> 元素

so this xpath query is driving me crazy. What I'm trying to do is search an xml file (in this case a kml file for use with the google maps api) of golf courses for a specific course type and grab each matching <Placemark> element so that i can create a new xml object with the results so that I can further filter with a new query. Problem is I can't seem to figure out how to get each matching <Placemark> element

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
    <Document>
      <Placemark id="placemark3">
          <name>Test Country Club</name>
          <description>
              <![CDATA[
                 <div class="contact">Post Office Box 329 <a href="#" target="_blank">website</a></div>
              ]]>
          </description>
          <alpha>a</alpha>
          <position>2</position>
          <type>Public/Daily Fee</type>
          <styleUrl>#nineholeStyle</styleUrl>
          <Point>
              <coordinates>-79.285576,37.111809</coordinates>
          </Point>
      </Placemark>
  </Document>
</kml>';

$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);

//trying to get any Placemark element where type child matches a specific query
//in this case we want to search for Public/Daily Fee and return the placemark and everything inside it.
$query = $xpath->query("//Placemark[type='Public/Daily Fee']/ancestor::Placemark");;

  foreach ($query as $result) {
    //eventually want to merge each result into new xml object to be further filtered
  }

任何帮助将不胜感激。谢谢

Any help would be greatly appreciated. Thanks

推荐答案

你没有在查询之前注册XML命名空间

// register it
$xpath->registerNamespace('kml', "http://earth.google.com/kml/2.1");

// and use it, too !
$query = $xpath->query("//kml:Placemark[kml:type='Public/Daily Fee']");

ancestor :: kml:Placemark 在你的表达中没有任何意义,我把它留下了。)

(the ancestor::kml:Placemark in your expression made no sense, I left it out.)

必须使用XPath查询中的命名空间前缀,即使命名空间问题是XML文档的默认命名空间(如您的情况)。

The namespace prefix in the XPath query must be used even if the namespace in question is the default namespace of the XML document (like in your case).

元素< kml xmlns =http://earth.google.com/kml/2.1> 相当于< kml:kml xmlns:kml =http:// earth。 google.com/kml/2.1\" >。

但是, / kml 的XPath查询不等同于 / kml:kml 。后者将匹配两个元素(当事先注册 kml 命名空间)时,前者将不会匹配,无论你做什么。

However, an XPath query of /kml is not equivalent to /kml:kml. The latter would match both elements (when the kml namespace is registered beforehand), the former would match none of them, no matter what you do.

另请注意,您不需要使用与XML文档中使用的相同的命名空间前缀。

Also note that you do not need to use the same namespace prefix that is used in the XML document.

假设XML文档是这样的:

Assume the XML document is this:

<kml:kml xmlns:kml="http://earth.google.com/kml/2.1">
  <kml:Document />
</kml:kml>

然后你仍然可以这样做:

Then you could still do this:

$xpath->registerNamespace('foo', "http://earth.google.com/kml/2.1");
$query = $xpath->query("/foo:kml/foo:Document");

并获得正确的结果。命名空间前缀只是一个简写。

and get back a correct result. Namespace prefixes are just a shorthand.

这篇关于DOM XPath查询帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆