【发布时间】:2019-07-04 08:27:00
【问题描述】:
我是 XPath 的新手,所以慢慢来 ;-)
我正在尝试从节点中获取内容
XML 结构看起来像(简化的 OOXML):
<w:p>
<w:r>
<w:drawing>
<wp:anchor wp14:editId="3BCCBF8F" wp14:anchorId="1109B0B5"
distR="114300" distL="114300" distB="0" distT="0"
allowOverlap="1" layoutInCell="1" locked="0" behindDoc="0"
relativeHeight="251663360" simplePos="0">
<a:graphic a="{url}">
<a:graphicData uri="{urli}">
<pic:pic xmlns:pic="{uri}">
<pic:blipFill>
<a:blip cstate="print" r:embed="rId13"/>
{all closing tag p, r, w etc}
<w:p>
<w:r>
<w:drawing>
<wp:anchor wp14:editId="3BCCBF8F" wp14:anchorId="1109B0B5"
distR="114300" distL="114300" distB="0" distT="0"
allowOverlap="1" layoutInCell="1" locked="0" behindDoc="0"
relativeHeight="251663360" simplePos="0">
<a:graphic a="{url}">
<a:graphicData uri="{urli}">
<pic:pic xmlns:pic="{uri}">
<pic:blipFill>
<a:blip cstate="print" r:embed="rId14"/>
{all closing tag p, r, w etc}
我的代码如下所示:
$result 下面只是一个带有 xml 的字符串
$document = new DOMDocument();
$document->loadXML($result);
$xpath = new DOMXpath($document);
$xpath->registerNamespace(
'word', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
);
foreach ($xpath->evaluate('//word:drawing//word:anchor') as $index => $node) {
var_dump($node);
}
我得到一个空节点。我显然做错了什么。我期待这个代码的锚节点。
我基本上可以循环抛出每个节点并找到每个节点的子项,但这似乎浪费了 XPath...
类似:
foreach ($xpath->evaluate('//word:drawing') as $index => $node) {
foreach($xpath->evaluate('*', $node) as $anchornode) {
var_dump($anchornode);
}
}
我真正想做的是在绘图元素(rId13 和 rId14)中获取 r:embed 值
我一直在尝试在 SO 上的其他问题中找到我想要的内容(有很多)....如果你找到了,请让我参考那个问题。
【问题讨论】: