xpath / php - 使用php选择包含特定属性的XML元素

时间:2016-06-23 19:30:56

标签: php xml attributes

我试图从具有相同属性名称的节点读取数据。我想将带有tag="650"的行读成3个独立的变量。 即:每次我在目录节点中都需要阅读给定的主题。

<report>
<catalog>
    <flexibleKey>123</flexibleKey>
    <numberOfTitleHolds>0</numberOfTitleHolds>
    <totalHolds>0</totalHolds>
    <numberOfCallNumbers>1</numberOfCallNumbers>
    <bibliographicLevel>FULL</bibliographicLevel>
    <catalogFormat>MARC</catalogFormat>
    <createdBy>ADMIN</createdBy>
    <dateCreated>2002-11-20</dateCreated>
    <dateCataloged>2003-02-05</dateCataloged>
    <modifiedBy>ADMIN</modifiedBy>
    <dateModified>2014-08-15</dateModified>
    <marc>
        <marcEntry tag="506" label="Access restriction" ind="  ">Classroom and In Library Use</marcEntry>
        <marcEntry tag="245" label="Title" ind="  ">title</marcEntry>
        <marcEntry tag="500" label="General Note" ind="  ">ATEC</marcEntry>
        <marcEntry tag="520" label="Abstract" ind="  ">info</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject 1</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject 2</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject 3</marcEntry>
    </marc>
</catalog>
<catalog>
    <flexibleKey>456</flexibleKey>
    <numberOfTitleHolds>0</numberOfTitleHolds>
    <totalHolds>0</totalHolds>
    <numberOfCallNumbers>1</numberOfCallNumbers>
    <bibliographicLevel>FULL</bibliographicLevel>
    <catalogFormat>MARC</catalogFormat>
    <createdBy>ADMIN</createdBy>
    <dateCreated>2002-11-20</dateCreated>
    <dateCataloged>2003-02-05</dateCataloged>
    <modifiedBy>ADMIN</modifiedBy>
    <dateModified>2014-08-15</dateModified>
    <marc>
        <marcEntry tag="506" label="Access restriction" ind="  ">Classroom and In Library Use</marcEntry>
        <marcEntry tag="245" label="Title" ind="  ">title</marcEntry>
        <marcEntry tag="500" label="General Note" ind="  ">ATEC</marcEntry>
        <marcEntry tag="520" label="Abstract" ind="  ">info</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject A</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject B</marcEntry>
    </marc>
</catalog>
</report>

我目前的代码低于......

$z = new XMLReader;
$z->open('my.xml');

while ($z->read() && $z->name !== 'catalog');

while ($z->name === 'catalog') {
   $node = simplexml_import_dom($doc->importNode($z->expand(), true));  
   echo $node->flexibleKey;
   echo $node->dateCreated;

   foreach ($node->marc->marcEntry as $tag) {
      // now I get lost :(
   }
}

感谢您提供的任何帮助:)

3 个答案:

答案 0 :(得分:0)

您可以使用SimpleXMLElement类来表示XML文档中的元素。您也可以使用xpath()方法选择自定义属性。

$xml = new SimpleXMLElement("xmlFile.xml");
$result = $xml->xpath("//*[@tag='650']");

foreach($result as $element)
{
    echo $element[0];
}

在上面的代码xpath("//*[@tag='650']")中,选择包含tag='650'属性的每个元素。

您可以在demo

中测试上述代码

答案 1 :(得分:0)

DOMDocumentDOMXPath救援:

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

$xpath = new DOMXPath($doc);
$results = $xpath->query("/catalog/marc/marcEntry[@tag = '650']");

foreach ($results as $result) {
    echo $result->nodeValue;    
}
?>

答案 2 :(得分:0)

这就是诀窍......

$z = new XMLReader;
$z->open('my.xml');

while ($z->read() && $z->name !== 'catalog');

    while ($z->name === 'catalog') {
        $node = simplexml_import_dom($doc->importNode($z->expand(), true));  
        echo $node->flexibleKey."<br />";
        echo $node->dateCreated."<br />";

        $result = $node->marc->xpath('marcEntry[@tag="650"]');
        foreach ($result as $subjecttag) {
            echo $subjecttag[0]."<br />";
        }
    }
}