【问题标题】:Xpath Regex in PHP not workingPHP中的Xpath正则表达式不起作用
【发布时间】:2014-02-04 00:11:10
【问题描述】:

这是我返回的 XML:

<?xml version="1.0" encoding="utf-8"?>
    <lists>
        <list>
            <id>6791</id>
            <title><![CDATA[List 1]]></title>
            <type>0</type>
            <priority>0</priority>
            <due><![CDATA[0000-00-00 00:00:00]]></due>
            <notes><![CDATA[]]></notes>
            <user_id>49211</user_id>
            <owner><![CDATA[]]></owner>
            <item1>
                <done>0</done>
                <title><![CDATA[Bamboo Montage-83 Knee High Studded Contrast Colored Zipper Riding Boot - Brown PU]]></title>
                <barcode>B00H2Y2UY6</barcode>
                <priority>2</priority>
                <item_id>57741</item_id>
            </item1>
            <item2>
                <done>0</done>
                <title><![CDATA[List 2]]></title>
                <barcode><![CDATA[]]></barcode>
                <priority>2</priority>
                <item_id>57751</item_id>
            </item2>
            <item3>
                <done>0</done>
                <title><![CDATA[List Item 1]]></title>
                <barcode><![CDATA[]]></barcode>
                <priority>2</priority>
                <item_id>57761</item_id>
            </item3>
        </list>

我想获得list-&gt;item[0-9] 节点,它附加了一个数字,我正在使用 PHP 的 SimpleXMLElementclass 来获得它们,但我无法使用正则表达式获得节点,这是我的代码:

    foreach ($list_xml->lists->xpath("/list/^item[0-9]$") as $listItem) {
             echo $listItem->title;
    }

注意 $list_xml 的转储是这样的:

SimpleXMLElement Object
(

[lists] => SimpleXMLElement Object
    (
        [list] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [id] => 6791
                        [title] => SimpleXMLElement Object
                            (
                            )

                        [type] => 0
                        [priority] => 0
                        [due] => SimpleXMLElement Object
                            (
                            )

                        [notes] => SimpleXMLElement Object
                            (
                            )

                        [user_id] => 49211
                        [owner] => SimpleXMLElement Object
                            (
                            )

                        [item1] => SimpleXMLElement Object
                            (
                                [done] => 0
                                [title] => SimpleXMLElement Object
                                    (
                                    )

                                [barcode] => B00H2Y2UY6
                                [priority] => 2
                                [item_id] => 57741
                            )

                        [item2] => SimpleXMLElement Object
                            (
                                [done] => 0
                                [title] => SimpleXMLElement Object
                                    (
                                    )

                                [barcode] => SimpleXMLElement Object
                                    (
                                    )

                                [priority] => 2
                                [item_id] => 57751
                            )

                        [item3] => SimpleXMLElement Object
                            (
                                [done] => 0
                                [title] => SimpleXMLElement Object
                                    (
                                    )

                                [barcode] => SimpleXMLElement Object
                                    (
                                    )

                                [priority] => 2
                                [item_id] => 57761
                            )

                    )
           )
    )

【问题讨论】:

    标签: php regex xpath simplexml


    【解决方案1】:

    SimpleXmlElement 由仅支持 XPath 1.0 的 libxml2 提供支持。 XPath 1.0 不支持字符串匹配正则表达式,XPath 2.0 支持。

    所以有两个选择:

    选项 1:继续使用 XPath 1.0

    在这种情况下,您可以尝试动态构建 xpath 查询以匹配所有 item* 节点:

    $items=array();
    for($i=0; $i<10; $i++) {
       $items[] = '/list/item' . $i;
    }
    $xpath=join($items, '|'); // $xpath == '/list/item1|/list/item2| ...'
    

    选项 2:移至 XPath 2.0

    没有计划将此支持添加到 PHP。阅读这个 SO 问题: (What is the best way to use XSLT 2.0 with PHP?) 寻找解决方法。在撰写本文时,这是关于该主题的最实际的内容之一。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多