xpath获取所有元素

发布于 2024-09-28 15:15:25 字数 1950 浏览 0 评论 0原文

我正在使用 simplexml 来解析 xml,我可以得到单个节点,但不是我想要的所有节点,

示例

    $xmlObject = simplexml_load_file($xml_file); // load the xml file
        $cols = $xmlObject->RESULTSET->ROW->COL;
    foreach ( $cols as $COL) {
        echo $COL->DATA;
    }

只给我第一行的第一个列,我尝试过

    $xmlObject = simplexml_load_file($xml_file); // load the xml file
    $cols = $xmlObject->xpath('*//COL');
    foreach ( $cols as $COL) {
        echo $COL->DATA;
    }

,但没有得到

任何结果,知道我可能做错了什么吗?

  <?xml version="1.0" encoding="UTF-8"?>
  <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
    <RESULTSET FOUND="445">
        <ROW MODID="45" RECORDID="1">
            <COL>
                <DATA>Integrating Ethanol</DATA>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA>train track and gas pump</DATA>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA>1.jpg</DATA>
            </COL>
            <COL>
                <DATA/>
            </COL>
        </ROW>
        <ROW MODID="29" RECORDID="3">
            <COL>
                <DATA>Keeping in Balance</DATA>
            </COL>
            <COL>
                <DATA>21</DATA>
            </COL>
            <COL>
                <DATA>book cover of Sweet Invisible Body</DATA>
            </COL>
            <COL>
                <DATA/>
            </COL>
        </ROW>
    </RESULTSET>
  </FMPXMLRESULT>

I am using simplexml to parse xml and I can get single node but not all nodes i want

example

    $xmlObject = simplexml_load_file($xml_file); // load the xml file
        $cols = $xmlObject->RESULTSET->ROW->COL;
    foreach ( $cols as $COL) {
        echo $COL->DATA;
    }

only gives me the first col of the first row, i tried

    $xmlObject = simplexml_load_file($xml_file); // load the xml file
    $cols = $xmlObject->xpath('*//COL');
    foreach ( $cols as $COL) {
        echo $COL->DATA;
    }

and got nothing back

any idea what I might be doing wrong ?

  <?xml version="1.0" encoding="UTF-8"?>
  <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
    <RESULTSET FOUND="445">
        <ROW MODID="45" RECORDID="1">
            <COL>
                <DATA>Integrating Ethanol</DATA>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA>train track and gas pump</DATA>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA>1.jpg</DATA>
            </COL>
            <COL>
                <DATA/>
            </COL>
        </ROW>
        <ROW MODID="29" RECORDID="3">
            <COL>
                <DATA>Keeping in Balance</DATA>
            </COL>
            <COL>
                <DATA>21</DATA>
            </COL>
            <COL>
                <DATA>book cover of Sweet Invisible Body</DATA>
            </COL>
            <COL>
                <DATA/>
            </COL>
        </ROW>
    </RESULTSET>
  </FMPXMLRESULT>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃→可爱长大的 2024-10-05 15:15:25

您可以尝试使用以下忽略命名空间的 XPath 表达式:

//*[local-name() = 'COL']

此表达式选择名为“COL”的所有节点,无论该节点位于哪个命名空间中。

You can try with the following XPath expression which ignores the namespace:

//*[local-name() = 'COL']

This expression selects all nodes with name 'COL' no matter in what namespace the node is in.

老旧海报 2024-10-05 15:15:25

正确的 xpath 是

//COL

没有

*

我认为你的问题可能是 XML 命名空间,当我通过使用 Google 找到的这个工具运行 XML 时,它给我带来了问题: http://www.xmlme.com/XpathTool.aspx

The right xpath for this would be

//COL

without the

*

I think your problem might be that XML namespace, which was causing the problem for me when I ran the XML through this tool that I found using Google: http://www.xmlme.com/XpathTool.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文