Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda