php - Xpath 和根据祖先的元素值有条件地选择后代

标签 php parsing xpath conditional-statements siblings

我希望有人能帮助我解决这个问题。

我对 XML 解析相当陌生,因此 XML 解析到目前为止我只处理过相当简单的解析。

我目前遇到以下问题...

我有具有以下结构的 XML:-

    <members>
        <member>
            <name>David Smith</name>
            <information>
                <description>home telephone</description>
                <given_information>
                    <details>0123465987</details>
                </given_information>
            </information>
            <information>
                <description>mobile telephone</description>
                <given_information>
                    <details>056142856987</details>
                </given_information>
            </information>
            <information>
                <description>email address</description>
                <given_information>
                    <details><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c78326f717568745c657d747373327f7371" rel="noreferrer noopener nofollow">[email protected]</a></details>
                </given_information>
            </information>
        </member>
    </members> 

我正在使用 simplexml 和 xpath,如下所示:-

    $XML_load =  simplexml_load_file('members.xml');
    foreach ($XML_load->members->xpath('//member') as $member)
    {
    $member_name = $member->name;
    } 

我遇到的问题是我不知道如何根据元素从元素中获取变量。所以我需要能够根据 的祖先元素中的信息准确地创建变量 $mobile_number、$home_number 和 $email_address 。

有人可以帮我解决这个问题吗?任何建议将不胜感激。

我希望我已经很好地解释了这个问题,并且我的术语是正确的,但如果不是,请原谅我,因为我仍然是新手。

顺便说一句,像这样使用 simplexml/xpath 是最好的方法吗?我听说 xmlDOM 更好,但我似乎找不到任何与之相关的资源来帮助我解决上述问题。

提前致谢!

最佳答案

首先尝试一下:

<?php
$dom = new DOMDocument();
$dom->load('yourXmlFile.xml');

/*$dom->loadXml('<members>
    <member>
        <name>David Smith</name>
        <information>
            <description>home telephone</description>
            <given_information>
                <details>0123465987</details>
            </given_information>
        </information>

        <information>
            <description>mobile telephone</description>
            <given_information>
                <details>056142856987</details>
            </given_information>
        </information>

        <information>
            <description>email address</description>
            <given_information>
                <details><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9cd87dac4c0ddc1e9d0c8c1c6c687cac6c4" rel="noreferrer noopener nofollow">[email protected]</a></details>
            </given_information>
        </information>
    </member>
</members>');*/

$xpath = new DOMXPath($dom);

$members = array();
foreach ($xpath->query('//member') as $memberNode) {
    $nameNode = $xpath->query('name', $memberNode);
    if ($nameNode->length > 0) {
        $name = $nameNode->item(0)->nodeValue;
        $members[$name] = array();

        foreach ($xpath->query('information', $memberNode) as $informationNode) {
            $descriptionNode = $xpath->query('description', $informationNode);
            $givenInformationDetailsNode = $xpath->query('given_information/details', $informationNode);

            if ($descriptionNode->length > 0 && $givenInformationDetailsNode->length > 0) {
                $members[$name][$descriptionNode->item(0)->nodeValue] = $givenInformationDetailsNode->item(0)->nodeValue;
            }
        }
    }
} 

var_dump($members);

关于php - Xpath 和根据祖先的元素值有条件地选择后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5866864/

相关文章:

php - 如何 $_GET [' ' ] 从 mod_modulenamet/mpl/default.php 中的 $document->addStylesheet() 到 joomla 模块的 css.php 文件的值

php - 在 Facebook 应用程序中获取好友位置

Python从字符串中解析json

parsing - 如何用多个文件编译yacc/Bison ?

parsing - 使用 ocamlyacc 减少/减少冲突

PHP:用下划线替换对象变量中的破折号

php - 如何根据值获取列名?不可能的?

java - XPath 在java中选择文档中具有指定名称的所有节点

html - div之后的文本的XPath?

excel vba - Selenium 查找元素