0

I am using xpath for the first time to query an xml document for a value and return the corresponding name value. I have been reading numerous different sources on the topic but I can't seem to get it to output correctly. Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<books>
  <book>
    <name>Genesis</name>
    <abrv>GEN</abrv>
    <title>The First Book of Moses</title>
  </book>
  <book>
    <name>Exodus</name>
    <abrv>EXO</abrv>
    <title>The Second Book of Moses</title>
  </book>
  <book>
    <name>Leviticus</name>
    <abrv>LEV</abrv>
    <title>The Third Book of Moses</title>
  </book>
</books>

Here is my PHP/HTML:

<?php $root = $_SERVER['DOCUMENT_ROOT'] . "/";
$bookID = 'GEN';
$xml = simplexml_load_file( $root . 'assets/xml/books.xml' ) or die( "No Page Data Found" );
$bookName = $xml->xpath( '//book[abrv="'.$bookID.'"]/name' );
?>
<article id="verseOfDay">
  <div class="container">
    <h2>verse of the day</h2>
    <h6><?php echo $bookName; ?></h6> // Line 19
    <p></p>
  </div>
</article>

Finally, here is the notice that I am receiving:

NOTICE: ARRAY TO STRING CONVERSION IN /HOME/#####/MYWEBSITE.COM/ASSETS/INCLUDES/VERSEOFTHEDAY.PHP ON LINE 19 ARRAY

What am I doing wrong? In this example I am expecting it to output Genesis in the tag.

7
  • 2
    Firstly, this is not an error, it is a notice. Secondly, you should give us what the output is and what the output should be, as the PHP can easily disregard notices. You have to give us also where is this array to string conversion as it is unclear which line is 19. Commented Aug 7, 2019 at 8:50
  • 2
    Take a look at this. When you apply your xpath, it returns an array but you echo it in your HTML, causing a conversion from array to string. Commented Aug 7, 2019 at 8:53
  • 2
    echo $bookName[0]; or foreach with that array if in real task you will get more than one result Commented Aug 7, 2019 at 8:53
  • @Martin My apologies. I have edited my question to include that information. Commented Aug 7, 2019 at 8:57
  • 1
    @Angeliss44 I've run your code on my server and get the expected result Commented Aug 7, 2019 at 9:16

1 Answer 1

1

The SimpleXML xpath function returns an array of results, which will be empty if no result is found. So you need to check for that, and if you get a result, it will be at $bookName[0]. Something like this will work:

$bookNames = $xml->xpath( '//book[abrv="'.$bookID.'"]/name' );
$bookName = empty($bookNames) ? 'Not Found' : $bookNames[0];
?>
<article id="verseOfDay">
  <div class="container">
    <h2>verse of the day</h2>
    <h6><?php echo $bookName; ?></h6>
    <p></p>
  </div>
</article>

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

1 Comment

This works beautifully! I think when I had tried using $bookName[0] before, my xpath query was different--something along the lines of "/books/book[abrv=$bookID]/name. Being 1am as I was working on this and posted this question I made the age old mistake of changing more than one thing at a time when trying to fix it. Thank you for the help as well as the explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.