1

I'm building a php script to transfer selected contents of an xml file to an sql database..

One of the hardcoded XML contents is formatted like this:

<visualURL>
id=18144083|img=http://upload.wikimedia.org/wikipedia/en/8/86/Holyrollernovacaine.jpg
</visualURL>

And I'm looking for a way to just get the contents of the URL (all text after img=).

$Image = $xpath->query("substring-after(/Playlist/PlaylistEntry[1]/visualURL[1]/text(), 'img=')", $element)->item(0)->nodeValue;

Displays a property non-object error on my php output.

There must be another way to just extract the URL contents using XPath that I want, no?

Any help would be greatly appreciated!

EDIT:

Here is the minimum code

<?php

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML('<Playlist>
<PlaylistEntry>
<visualURL>
id=12582194|img=http://upload.wikimedia.org/wikipedia/en/9/96/Sometime_around_midnight.jpg
</visualURL>
</PlaylistEntry>
</Playlist>');
$xpath = new DOMXpath($xmlDoc);
$elements = $xpath->query("/Playlist/PlaylistEntry[1]");

if (!is_null($elements)) 
foreach ($elements as $element) 
$Image = $xpath->query("substring-after(/Playlist/PlaylistEntry[1]/visualURL[1]/text(), 'img=')", $element)-  >item(0)->nodeValue;
print "Finished Item: $Image";

?>

EDIT 2:

After some research I believe I must use $xpath->evaluate instead of my current use of $xpath->query

see this link Same XPath query is working with Google docs but not PHP

I'm not exactly sure how to do this yet.. but i will investigate more in the morning. Again, any help would be appreciated.

Community
  • 1
  • 1
bbruman
  • 667
  • 4
  • 20
  • actually, xpath 1.0 has [`substring-after()`](http://www.w3.org/TR/xpath/#function-substring-after) function.. can you post [minimal codes to reproduce the problem](http://meta.stackoverflow.com/a/258849/2998271)? – har07 May 26 '15 at 07:37
  • 1
    @har07 ok I edited the main post with (what I think to be) the minimal codes. Please let me know if you can assist! Thanks – bbruman May 26 '15 at 13:55
  • For reference I built an XSL style sheet for the xml file, and used software to generate the results. What's built and is working properly is something very simple -- `` -- this works perfectly. But when I try to code it for actual output in php where it's important -- `$Image = $xpath->query("substring-after(//visualURL,'img=')", $element)->item(0)->nodeValue;` I get the the non-object error. So confusing. I've worked on it for a few hours today with little result. Did you have a chance to look it over @har07 , or anyone? – bbruman May 27 '15 at 03:19
  • +1 for adding minimal codes to reproduce the problem and (another +1 if I could) for the research effort – har07 May 27 '15 at 08:18

1 Answers1

0

You're in right direction. Use DOMXPath::evaluate() for xpath expression that doesn't return node(s) like substring-after() (it returns string as documented in the linked page). The following codes prints expected output :

$xmlDoc = new DOMDocument();
$xml = <<<XML
<Playlist>
<PlaylistEntry>
<visualURL>
id=12582194|img=http://upload.wikimedia.org/wikipedia/en/9/96/Sometime_around_midnight.jpg
</visualURL>
</PlaylistEntry>
</Playlist>
XML;

$xmlDoc->loadXML($xml);
$xpath = new DOMXpath($xmlDoc);
$elements = $xpath->query("/Playlist/PlaylistEntry");

foreach ($elements as $element) {
    $Image = $xpath->evaluate("substring-after(visualURL, 'img=')", $element);
    print "Finished Item: $Image <br>";
}

output :

Finished Item: http://upload.wikimedia.org/wikipedia/en/9/96/Sometime_around_midnight.jpg 

Demo

har07
  • 88,338
  • 12
  • 84
  • 137
  • Woo! I was really close, wasn't I? Turned out to be simpler than I thought. Anyways, that works! Thanks so much har. – bbruman May 27 '15 at 13:52