SimpleXML Xpath() inconsistent search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abnoba12
Forum Newbie
Posts: 1
Joined: Tue Jun 24, 2008 5:49 pm

SimpleXML Xpath() inconsistent search

Post by abnoba12 »

I'm working some software to process open eBooks. Inside of them they hold their metadata in XML format. The tags are standard trough all versions, but the structure isn't. I've decided to just search for the tag and return the results using simpleXML's xpath() function, but it doesn't work consistently.

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 "";
    }
This is one XML file that I have tried my code with and it works.
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 is the other XML file and this one doesn't work .
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>
 
As you can see that the dctitle tag exists in both files, but the files have aren't exactly the same. Why dose one work and not the other?
Post Reply