c.davies c.davies - 5 months ago 15
PHP Question

xpath/php - Select XML element that contain specific attributes using php

I'm trying to read data from nodes that have the same attribute names. I would like to read the lines with

tag="650"
into 3 separate variables. ie: each time I'm in a catalog node I need to read the given subjects.

<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>


My current code is below...

$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 :(
}
}


Thanks for any help you can offer :)

Answer

This did the trick...

$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 />";
        }
    }
}
Comments