1

在给定的 XML 结构上

<films>
  <title>Jaws</title>
  <year>1975</year>
  <title>Spaceballs</title>
  <year>1987</year>
</films>

如何输出与 xpath 连接的每个节点的node nametext?喜欢

title: Jaws
year: 1975
title: Spaceballs
year: 1987
4

2 回答 2

1

使用纯 XPath,您需要 2.0 版://*[not(*)]/concat(local-name(), ': ', .). 如果您只有 XPath 1.0,那么您需要选择已经显示的元素,然后输出名称和内容。

于 2013-10-26T12:04:32.360 回答
0

那这个呢:

$xml = <<<EOF
<films>
  <title>Jaws</title>
  <year>1975</year>
  <title>Spaceballs</title>
  <year>1987</year>
</films>
EOF;

$doc = new DOMDocument();
$doc->loadXML($xml);
$selector = new DOMXpath($doc);

foreach($selector->query('/films/*') as $child) {
    echo $child->nodeName . ': ' . $child->nodeValue . PHP_EOL;
}

? 输出:

title: Jaws
year: 1975
title: Spaceballs
year: 1987
于 2013-10-26T11:22:24.523 回答