Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Getting the value of XML field matching a property via XPATH

I've got a really odd XML schema that's causing me unecessary grief and woe.

I need to get the value of an IMAGEFILENAME node that has a property of "hide".

The XML schema looks something like:

<PHOTOS>
    <IMAGETHUMBFILENAME/>
    <IMAGECAPTION>
        This is a caption
    </IMAGECAPTION>
    <PRINTQUALITYIMAGE>
        /mylocation/filename1.jpg
    </PRINTQUALITYIMAGE>
    <IMAGEFILENAME pictype="show">
        /mylocation/filename2.jpg
    </IMAGEFILENAME>
    <IMAGETHUMBFILENAME/>
    <IMAGECAPTION>This is another caption</IMAGECAPTION>
    <PRINTQUALITYIMAGE>
        /mylocation/filename3.jpg
    </PRINTQUALITYIMAGE>
    <IMAGEFILENAME pictype="hide">
        /mylocation/filename4.jpg
    </IMAGEFILENAME>
    <IMAGETHUMBFILENAME/>
</PHOTOS>

And I've managed to come up with the following XPATH using PHP:

$nodes = $xml->xpath('/PHOTOS/IMAGEFILENAME[@pictype="hide"]');
var_dump($nodes);

When I do a dump of the $nodes var what I'd hope to see (and what I want) is to get the value /mylocation/filename4.jpg. Instead what I'm getting is:

array(1) {
  [0]=>
  object(SimpleXMLElement)#333 (1) {
    ["@attributes"]=>
    array(1) {
      ["pictype"]=>
      string(10) "hide"
    }
  }
}

I've tried various combinations of /parent, /text() and /node() but with no joy at all.

Please somebody tell me what a muppet I'm being and put me out of my misery. Either that or is the schema being problematic?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

So, you have array of SimpleXMLElements.

To get string representation of SimpleXMLElement you can just echo it:

$nodes = $xml->xpath('/PHOTOS/IMAGEFILENAME[@pictype="hide"]');
echo $nodes[0];   // I used `[]` notation to get first element of array

To use string representation of SimpleXMLElement later in your code you can convert it to string explicitly:

$nodes = $xml->xpath('/PHOTOS/IMAGEFILENAME[@pictype="hide"]');
$node_str = strval($nodes[0]);  // still `[]` notation
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!