需要显示父ID的子数据

时间:2011-07-15 08:40:51

标签: php xpath

我正在努力使用Xpath,我有一个xml列表,我需要根据父ID获取子数据...

我的xml文件:

<projecten>
<project id="1">
    <titel>Shop 1</titel>
    <siteurl>http://test.be</siteurl>
    <screenshot>test.jpg</screenshot>
    <omschrijving>comment 1</omschrijving>
</project>
<project id="2">
    <titel>Shop 2</titel>
    <siteurl>http://test2.be</siteurl>
    <screenshot>test2.jpg</screenshot>
    <omschrijving>comment</omschrijving>
</project>
</projecten>

我用来获取项目1数据的代码(不起作用):

$xmlDoc = new DOMDocument();
$xmlDoc->load(data.xml);
$xpath = new DOMXPath($xmlDoc);

$projectId = '1';   
$query = '//projecten/project[@id='.$projectId.']';

$details = $xpath->query($query);

foreach( $details as $detail )
{
    echo $detail->titel;
    echo $detail->siteurl;
    echo $detail->screenshot;
    echo $detail->omschrijving;     
}

但这并没有显示任何内容,如果有人可以指出我...谢谢

2 个答案:

答案 0 :(得分:1)

除了已经给出的解决方案,您还可以使用:

foreach ($xpath->query(sprintf('/projecten/project[@id="%d"]', $id)) as $projectNode) {
    echo
        $projectNode->getElementsByTagName('titel')->item(0)->nodeValue,
        $projectNode->getElementsByTagName('siteurl')->item(0)->nodeValue,
        $projectNode->getElementsByTagName('screenshot')->item(0)->nodeValue,
        $projectNode->getElementsByTagName('omschrijving')->item(0)->nodeValue;
}

或直接使用Xpath

获取DOMText节点值
foreach ($xpath->query(sprintf('/projecten/project[@id="%d"]', $id)) as $projectNode) {
    echo
        $xpath->evaluate('string(titel)', $projectNode),
        $xpath->evaluate('string(siteurl)', $projectNode),
        $xpath->evaluate('string(screenshot)', $projectNode),
        $xpath->evaluate('string(omschrijving)', $projectNode);
}

或将节点导入SimpleXml

foreach ($xpath->query(sprintf('/projecten/project[@id="%d"]', $id)) as $projectNode) {
    $detail = simplexml_import_dom($projectNode);
    echo
        $detail->titel,
        $detail->siteurl,
        $detail->screenshot,
        $detail->omschrijving;
}

甚至直接在XPath中连接所有值:

$xpath = new DOMXPath($dom);
echo $xpath->evaluate(
    sprintf(
        'concat(
            /projecten/project[@id = %1$d]/titel,
            /projecten/project[@id = %1$d]/siteurl,
            /projecten/project[@id = %1$d]/screenshot,
            /projecten/project[@id = %1$d]/omschrijving
         ', $id
    )
);

答案 1 :(得分:0)

像以下一样访问子节点:

echo $detail->title;

如果您使用DOM*功能,则无效。如果您使用SimpleXML,这可能会有用。

对于DOM *试试这个:

$dom = new DOMDocument;
$dom->loadXml('<projecten>
<project id="1">
    <titel>Shop 1</titel>
    <siteurl>http://test.be</siteurl>
    <screenshot>test.jpg</screenshot>
    <omschrijving>comment 1</omschrijving>
</project>
<project id="2">
    <titel>Shop 2</titel>
    <siteurl>http://test2.be</siteurl>
    <screenshot>test2.jpg</screenshot>
    <omschrijving>comment</omschrijving>
</project>
</projecten>
');

$id = 2;
$xpath = new DOMXPath($dom);
foreach ($xpath->query(sprintf('/projecten/project[@id="%s"]', $id)) as $projectNode) {
    // repeat this for every needed node
    $titleNode = $xpath->query('titel', $projectNode)->item(0);
    if ($titleNode instanceof DOMElement) {
        echo $titleNode->nodeValue;
    }

    // or us a loop for all child nodes
    foreach ($projectNode->childNodes as $childNode) {
        echo $childNode->nodeValue;
    }
}