简体   繁体   English

xpath获取所有元素

[英]xpath to get all elements

I am using simplexml to parse xml and I can get single node but not all nodes i want 我正在使用simplexml解析xml,但我可以得到单个节点,但不是我想要的所有节点

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>

You can try with the following XPath expression which ignores the namespace: 您可以尝试使用以下XPath表达式来忽略名称空间:

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

This expression selects all nodes with name 'COL' no matter in what namespace the node is in. 无论节点位于哪个命名空间中,此表达式都会选择名称为“ COL”的所有节点。

The right xpath for this would be 正确的xpath将是

//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 我认为您的问题可能是XML名称空间,当我通过使用Google找到的该工具运行XML时,这对我来说是个问题: http : //www.xmlme.com/XpathTool.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM