This is my searching code
Code: Select all
echo $this->xmlFindData($this->xml, 'dctitle');
echo $this->xmlFindData($this->xml, "dclanguage");
/*
* Find the specified data.
* $xml is the xml document
* $tag is the tag with the data being searched for
* returns the result as a string or an array of strings if there is more then one match.
*/
function xmlFindData($input, $tag){
//echo $tag."-";
$item = $input->xpath('//'.$tag);
if(count($item) == 1)
return $item[0];
else if(count($item) > 1)
return $item;
else
return "";
}When I use this XML file with my function it produces: "White Fang", "en" for the output
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<package unique-identifier="UID">
<metadata>
<dc-metadata xmlns:dc="http://purl.org/dc/elements/1.0/" xmlns:oebpackage="http://openebook.org/namespaces/oeb-package/1.0/">
<dcidentifier id="UID">ED9B58F8-73E8-49CE-90BC-708BD763EA9A</dcidentifier>
<dctitle>White Fang</dctitle>
<dccreator>Jack London</dccreator>
<dclanguage>en</dclanguage>
</dc-metadata>
<x-metadata>
<meta name="x-SBP-category" content="General Interest"/>
<meta name="x-ShortDescription" content="The story of an animal that is part wolf and part dog."/>
<meta name="x-LongDescription" content="This is the story of an animal that is part wolf and part dog, viciously bad-tempered from mistreatment but eventually rescued from a cruel owner. The wild animal is finally tamed by a combination of patience and kindness. His new owner, Weedon Scott, is vindicated in his faith in the dog when White Fang defends Scott's father from attack."/>
<meta name="x-Keywords" content="fiction, fantasy, wolf, dog"/>
</x-metadata>
</metadata>
</package>
This XML file with my function produces no output.
Code: Select all
<?xml version="1.0"?>
<!DOCTYPE package PUBLIC "+//ISBN 0-9673008-1-9//DTD OEB 1.2 Package//EN" "http://openebook.org/dtds/oeb-1.2/oebpkg12.dtd">
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="bookid" version="2.0">
<metadata>
<dc-metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dctitle>Twenty Thousand Leagues Under the Seas</dctitle>
<dccreator>Jules Verne</dccreator>
<dclanguage xsi:type="dcterms:RFC3066">en-GB</dclanguage>
<dcrights>Public Domain</dcrights>
<dcpublisher>Gutenburg Foundation</dcpublisher>
<dcidentifier id="bookid">urn:uuid:jedisaber.com072720071405</dcidentifier>
</dc-metadata>
</metadata>
</package>