【问题标题】:Find a specific element and its value with XPath in XML PHP在 XML PHP 中使用 XPath 查找特定元素及其值
【发布时间】: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 上的其他问题中找到我想要的内容(有很多)....如果你找到了,请让我参考那个问题。

【问题讨论】:

    标签: php xml xpath


    【解决方案1】:

    wp:anchor 位于不同的命名空间中(来自w:document)。查找 xmlns:wp 属性。这是wp 前缀的命名空间定义。

    您还必须为该命名空间注册一个别名/前缀。

    $xpath->registerNamespace(
       'word', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
    );    
    $xpath->registerNamespace(
       'wp', 'urn:???'
    );
    

    您的代码为命名空间 URI http://schemas.openxmlformats.org/wordprocessingml/2006/main 注册前缀 word

    这允许 Xpath 处理器解析 Xpath 表达式中的前缀。您可以将其解读为:

    //word:drawing -> //{http://schemas.openxmlformats.org/wordprocessingml/2006/main}drawing

    XML 解析器对节点名称执行相同的操作。

    &lt;w:drawing/&gt; -> &lt;{http://schemas.openxmlformats.org/wordprocessingml/2006/main}drawing/&gt;

    这就是它的匹配方式。但是因为这样的东西真的很难阅读(对于人类来说)并导致使用大型 XML 文件别名/前缀。您可以在 Xpath 表达式中使用与文档中相同的前缀 (w, wp, ...),但您必须将它们注册到相同的命名空间 URI。将前缀视为变量名,保持它们的可读性,以便您以后可以理解您的代码。

    【讨论】:

    • 非常感谢!这真的让我朝着正确的方向前进!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2018-08-04
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多