You won’t mind a little coding, will you? Just a little? Good.
Here’s the thing. Let’s suppose you have an XML feed, and you want a very specific item out of that XML feed to display on your WordPress blog.
(Everyone who doesn’t know what XML is can stop reading now.)
Let’s say that you’re working out of a large XML file. I’m going to use one of the XML files out of the Hollywood Stock Exchange; this one lists all the stock information for different upcoming movies. (Know before you click that it’s a very large file.) Let’s suppose I want to pull out the release date for the next Indiana Jones movie (which has the stock symbol in HSX of INDI4). In fact, I’ll go ahead and copy out the relevant clip of XML from the file, so you can see it (although I won’t go to the trouble of explaining what all the codes mean):
<sec tkr=”INDI4″>
<name>Indiana Jones and the Kingdom of the Crystal Skull</name>
<sts>ACT</sts>
<yc>256.32</yc>
<ipo>18.25</ipo>
<rls>22-MAY-2008</rls>
<phase>W</phase>
<genre>Adventure</genre>
<gross>0</gross>
<wh>256.95</wh>
<wl>255.69</wl>
<mh>256.95</mh>
<ml>247.5</ml>
<sh>256.95</sh>
<sl>219.78</sl>
<yh>256.95</yh>
<yl>132.21</yl>
</sec>
It’s very easy to pull out one specific bit of text from the XML within WordPress. Within a PHP statement, you type the following:
$mst = simplexml_load_file(‘all_hsx_mst.xml’);
$releasedatearray = $mst->xpath(//sec[@tkr="INDI4"]/rls);
$releasedate = $releasedatearray["0"];
echo $releasedate;
Cool, huh? Let’s explain that in English.
$mst = simplexml_load_file(‘all_hsx_mst.xml’);
This line just sets a PHP string called “$mst”. (It could have been called anything, but it has to start with a dollar sign.) It assigns that string to a PHP 5 command called Simple XML, which loads the URL of the XML file we want to start off with. (I have actually omitted most of the real URL for the real file here a little so as not to screw up the formatting of the blog.) (It matters muchly that we’re using PHP 5 — check with your hosting service — because if you’re not, you can’t use Simple XML, you have to do something incredibly cumbersome called a DOM statement.) Okay? Second line:
$releasedatearray = $mst->xpath(//sec[@tkr="INDI4"]/rls);
This is our XPath line. It sets up a string called “$releasedatearray” — and again, that’s just what I call it, you can call it $snookums if you can remember it better that way. The XPath line first accesses the XML file (through the $mst string we did in the first line) and then does an XPath search for the information we want — in this case, the ticker symbol (“INDI4″, which is an attribute of the “sec” value of the XML quoted above), and the release date that’s associated with that symbol (rls). (If you want to know more about XPath, this is a good tutorial, and this testbed site will help you practice forming XPath searches.)
Third line:
$releasedate = $releasedatearray["0"];
What you get back when you do an XPath search is an array — even though in this case, the array is really just one item. You don’t want to show the array, just the one item, so you set a string (arbitrary again) “$releasedate” to equal the first cell in the array (which is zero, don’t ask me why). Then your fourth line just prints out the “$releasedate” string (22-MAY-2008), and you’re done.
Okay, but what happens when you don’t want just the INDI4 release date, but release dates for whatever symbol you want? What happens when you want there to be a variable there instead of INDI4?
Well, the way I would have thought that you do it is how you do things in WordPress generally. Let’s say the title of your given post was INDI4, and you wanted the title of your post to appear within the XPath. (It doesn’t have to be the title — in WordPress lingo, the_title — but let’s say.) What if you tried this as your second line:
$releasedatearray = $mst->xpath(//sec[@tkr="<?php the_title(); ?>"]/rls);
Looks sensible, right? You’re putting in the PHP expression that returns your title in the XPath; it ought to turn up that variable in every XPath search on every post. Right? Wrong. This doesn’t work. You can’t bring up a PHP command inside an XPath search expression.
So here’s what you do.
$ticker = get_the_title($post->post_parent);
$first = “(//sec[@tkr='";
$last = "']/rls)”;
$path = $first.$ticker.$last;
$mst = simplexml_load_file(‘all_hsx_mst.xml’);
$releasedatearray = $mst->xpath($path);
$releasedate = $releasedatearray["0"];
echo $releasedate;
Again, line-by-line analysis. First line:
$ticker = get_the_title($post->post_parent);
This sets the WordPress “the_title” variable to a text string called “$ticker”.
$first = “(//sec[@tkr='";
$last = "']/rls)”;
These lines set up text strings that correspond to eveything that goes in front of the ticker (“$first”) and behind the ticker (“$last”) in our XPath command. Then you concatenate the three strings into another string, as follows:
$path = $first.$ticker.$last;
This creates a text string called “$path” that creates your XPath expression, with the “$ticker” symbol (the variable, that we got from the_title) in the middle. Then, in our XPath line:
$releasedatearray = $mst->xpath($path);
And that works. You just plug your text string in your XPath command, and you can plug whatever variable you want.
Thank you if you read this far. I truly hope this was helpful for someone, because I was absolutely pulling my hair out trying to get this to work last week, and the answer for the question wasn’t anywhere on line that I could find easily, at least not within the WordPress context.