2

On my development system Php 5.3 The following SimpleXML object is working fine:

    $fs = "lectio1"
    $xmldoc = simplexml_load_file($filename);
    $ln = $xmldoc->xpath("//item[fileName/@filestem='$fs']/title");
    print_r($ln)


Array ( [0] => SimpleXMLElement Object ( [0] => Lectio 1, Prologus ) )

But the same function in php 5.4 produces the following result.

Array ( [0] => SimpleXMLElement Object ( ) )

Can anyone help me figure out what is happening?

A fragment of the XML file is as follows:

 <?xml version="1.0" encoding="UTF-8"?>
 <listofFileNames>
 <div id="body">
    <div id="principia" class="toplevel">
        <head>Principia</head>
        <item>
            <fileName filestem="principiumI">principiumI.xml</fileName>
            <title>Principium I</title>
            <hasParts>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
            </hasParts>
        </item>
        <item>
            <fileName filestem="principiumII">principiumII.xml</fileName>
            <title>Principium II</title>
            <hasParts>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
            </hasParts>
        </item>
        <item>
            <fileName filestem="principiumIII">principiumIII.xml</fileName>
            <title>Principium III</title>
            <hasParts>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
                <part>
                    <slug>sorb</slug>
                    <title>Sorbonne</title>
                    <initial>S</initial>
                </part>
            </hasParts>
        </item>
        <item>
            <fileName filestem="principiumIV">principiumIV.xml</fileName>
            <title>Principium IV</title>
            <hasParts>
                <part>
                    <slug>reims</slug>
                    <title>Reims</title>
                    <initial>R</initial>
                </part>
                <part>
                    <slug>svict</slug>
                    <title>St. Victor</title>
                    <initial>SV</initial>
                </part>
                <part>
                    <slug>sorb</slug>
                    <title>Sorbonne</title>
                    <initial>S</initial>
                </part>
            </hasParts>
        </item>
    </div>
    <div id="deFide" class="toplevel">
        <head>de Fide</head>
        <item>
            <fileName filestem="lectio1">lectio1.xml</fileName>
            <title>Lectio 1, Prologus</title>
            <hasParts>
                <part>
                    <slug>reims</slug>
                    <title>Reims</title>
                    <initial>R</initial>
                </part>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
                <part>
                    <slug>svict</slug>
                    <title>St. Victor</title>
                    <initial>SV</initial>
                </part>
                <part>
                    <slug>sorb</slug>
                    <title>Sorbonne</title>
                    <initial>S</initial>
                </part>
            </hasParts>
        </item>
        <item>
            <fileName filestem="lectio2">lectio2.xml</fileName>
            <title>Lectio 2, Prologus</title>
            <hasParts>
                <part>
                    <slug>reims</slug>
                    <title>Reims</title>
                    <initial>R</initial>
                </part>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
                <part>
                    <slug>svict</slug>
                    <title>St. Victor</title>
                    <initial>SV</initial>
                </part>
                <part>
                    <slug>sorb</slug>
                    <title>Sorbonne</title>
                    <initial>S</initial>
                </part>
            </hasParts>
        </item>
        <item>
            <fileName filestem="lectio3">lectio3.xml</fileName>
            <title>Lectio 3, Prologus</title>
            <hasParts>
                <part>
                    <slug>reims</slug>
                    <title>Reims</title>
                    <initial>R</initial>
                </part>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
                <part>
                    <slug>svict</slug>
                    <title>St. Victor</title>
                    <initial>SV</initial>
                </part>
                <part>
                    <slug>sorb</slug>
                    <title>Sorbonne</title>
                    <initial>S</initial>
                </part>
            </hasParts>
        </item>
     </div>
   </div>
 </listofFileNames>
hakre
  • 193,403
  • 52
  • 435
  • 836
Jeff
  • 3,943
  • 8
  • 45
  • 68
  • Without the XML & your `simpleXMLClean` function hard to tell. – Wrikken Mar 12 '14 at 20:26
  • @Wrikken please see my edits - xml added – Jeff Mar 12 '14 at 20:51
  • have you checked that `$filename` has expected value? have you dumped `$xmldoc` to see if it has the contents? have you checked if `$fs` has expected value? I have a sneaking suspicion this has nothing to do with moving to php5.4 and everything to do with something else, e.g. bad vars or different settings somewhere from being in a diff env – CrayonViolent Mar 12 '14 at 20:53
  • i mean, i can tell from the edit history and Wrikken's comment that you've decided to remove stuff and otherwise alter what you're *really* doing. Probably in an effort to simplify the issue, but you've over-simplified it. There's no reason this code shouldn't work in and of itself, moving from 5.3 to 5.4, so your issue is something else – CrayonViolent Mar 12 '14 at 20:56
  • Ok with your help I've narrowed down the fact that the file does load on the 5.4, but comes $ln comes up empty. So the problem lies in the xpath. See any problems there? Note that this xml file has no namespace therefore I have not provided any namespace declaration (Which works on the 5.3 system) – Jeff Mar 12 '14 at 21:31
  • the systems are also running slightly different version of libxml -- the one that is working is running: libXML Compiled Version => 2.8.0, the one that is not working is running: libXML Compiled Version => 2.7.6 – Jeff Mar 12 '14 at 22:14
  • 1
    @Jeff: as far as I know, this is just a difference in representation in `print_r` (and shouldn't be relied upon): no match is `Array( )`, with a match is indeed `Array ( [0] => SimpleXMLElement Object ( ) )` (libxml 2.9.1), but doing an `echo (string)$ln[0]` _does_ give the text content. So, it matches, it finds it, it just represents this differently in `print_r` / `var_dump`. If you've got an error in 5.4 code, most likely this is at another location. – Wrikken Mar 13 '14 at 07:15
  • that's very helpful Wrikken thanks very much. So much for a hobbiest to learn :) – Jeff Mar 13 '14 at 12:14
  • [this answer may be helpful to you](http://stackoverflow.com/questions/6167279/converting-a-simplexml-object-to-an-array) – Wrikken Mar 13 '14 at 17:20

1 Answers1

0

Don't rely on print_r or var_dump when it comes to SimpleXml, rather do:

foreach ($ln as $l)
    echo htmlentities($l->asXML());

With this method, the result with 5.3.10 vs. 5.4.9 is the same:

<title>Lectio 1, Prologus</title>

PHP 5.3.10: see http://codepad.viper-7.com/RL8L0i

PHP 5.4.9: see http://codepad.viper-7.com/0JgAmQ

michi
  • 6,565
  • 4
  • 33
  • 56
  • Thank you so much for your help. Indeed this is not a problem. There must be something I really don't understand about simpleXMLbehaviour. I'm stil don't understand why print_r gives me different results on different systems. But I guess I'l switch my functions to use htmlentities instead of using `json_decode(json_encode($ln, true)` to remove the info from its simpleXML object state. Turn it into a simple array and then extract the info. Your suggestion seems much cleaner. – Jeff Mar 13 '14 at 12:13