Xpath并根据祖先的元素值有条件地选择后代 [英] Xpath and conditionally selecting descendants based on element value of ancestors

查看:90
本文介绍了Xpath并根据祖先的元素值有条件地选择后代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助我解决这个问题.

我对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>d.smith@yahoo.com</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.

有人可以帮我吗?任何建议将不胜感激.

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

BTW像这样使用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>d.smith@yahoo.com</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);

I am hoping someone can help me with this.

I am fairly new to XML parsing so XML parsing and I have only dealt with fairly simple parsing so far.

I am currently stuck with the following problem...

I have XML with this structure: -

    <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>d.smith@yahoo.com</details>
                </given_information>
            </information>
        </member>
    </members> 

I am using simplexml & xpath like this: -

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

The problem I have is that I dont know how to get a variable from the element based on the element. So I need to be able to create the variables $mobile_number, $home_number and $email_address accurately based on the information in the ancestor element of .

Can anyone help me with this? Any advice would be greatly appreciated.

I hope I have explained the problem well enough and my terminology is correct but please forgive me if it is not as I am still a newbie at this.

BTW is using simplexml/xpath like this the best way to do it? I have heard xmlDOM is better but I cant seem to find any resources related to that which would help me with the problem above.

Thanks in advance!

解决方案

Try this for a start:

<?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>d.smith@yahoo.com</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);

这篇关于Xpath并根据祖先的元素值有条件地选择后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆