XML Parsing

He has: 1,380 posts

Joined: Feb 2002

Hey this is a continuation of the "pop-up window" thread...

i basically want to pull info out of another site that is updated all the time, its in XML...i know i need to parse it and format it or something. i would like to use PHP if possible... the XML is structured as such:

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <weather ver="2.0">
- <head>
  <locale>en_US</locale>
  <form>MEDIUM</form>
  <ut>F</ut>
  <ud>mi</ud>
  <us>mph</us>
  <up>in</up>
  <ur>in</ur>
  </head>
- <loc id="USGA0028">
  <dnam>Atlanta, GA</dnam>
  <tm>4:53 PM</tm>
  <lat>33.75</lat>
  <lon>-84.39</lon>
  <sunr>7:00 AM</sunr>
  <suns>8:23 PM</suns>
  <zone>-4</zone>
  </loc>
  </weather>
'

that is pulled off of weather.com, via their site, not the one i will be using, but still the same format. any help would be great.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

What format do you want it in?!

For example:

,

Where is the weather information? I see latitude, longitude, sunrise, sunset and zone. No temperature or weather forcast? That's the information that would be needed to set up the XSL to translate the XML to XHTML.

He has: 1,380 posts

Joined: Feb 2002

i don't know why that all didn't show up, and i cant get it to...just download the PDF, goto page 10 at http://westernciv.sarvihosting.com/guide.pdf

how i would like it:

<p><dnam>-&nbsp;&nbsp;<lsup><br /></p>
<p>&nbsp;&nbsp;&nbsp;<tmp></p>
<p>&nbsp;&nbsp;&nbsp;<t></P>
'

stuff like that

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Kyle, you need to be more specific -- is an element in many of the elements.

Current conditions () or Daily Forecast ()? I'm assuming

Page 15 indicates you need to include and at a minimum for . (section 8.5)

ANYWAY.

To write the XSLT:

<?php
// this is a subset of what you get from weather.com called weather.xml
xml version=\"1.0\" encoding=\"UTF-8\"
<weather>
    <loc id=\"30066\">
        <dnam>Marietta, GA (30066)</dnam>
    </loc>
    <cc>
        <lsup>3/5/03 9:50 AM EST</lsup>
        <obst>Kennesaw, GA</obst>
        <tmp>55</tmp>
        <t>Haze</t>
        <icon>21</icon>
     </cc>
</weather>

// this is your xsl stylesheet, called weather.xsl
xml version=\"1.0\" encoding=\"UTF-8\"
<xsl:stylesheet version=\"1.0\"
    xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">

    <xsl:output method=\"html\"/>

    <xsl:template match=\"weather\">
        <div id=\"weather\">
        <div id=\"weathericon\"><xsl:value-of select=\"cc/icon\"></div>
        <p><xsl:value-of select=\"loc/dnam\"/> - <xsl:value-of select=\"cc/lsup\"/></p>
        <p><xsl:value-of select=\"cc/tmp\"/></p>
        <p><xsl:value-of select=\"cc/t\"/></p>
        </div>
    </xsl:template>

</xsl:stylesheet>

// this is your php program that makes it work

    // Create an XSLT processor
   
$xsltproc = xslt_create();

    // Perform the transformation
   
$html = xslt_process($xsltproc, 'weather.xml', 'weather.xsl');

    // Detect errors
    if (!
$html) die('XSLT processing error: '.xslt_error($xsltproc));

    // Destroy the XSLT processor
    xslt_free(
$xsltproc);

    // Output the resulting HTML
    echo
$html;
    // you may want to write it to a file instead, that's then included on your page.

?>

http://www.sitepoint.com/article/602/3 for more information. You can use a cronjob on the server to perform the action as needed.

He has: 1,380 posts

Joined: Feb 2002

ok thanks...i'm currently trying it out

He has: 1,380 posts

Joined: Feb 2002

alright...i hooked it up, and here's what came out:

Fatal error: Call to undefined function: xslt_create() in /home/.../php/weather.php on line 3'

i checked with my hosting provider (SarviHosting...wonderful job I must say), and i do have XML/XSL support. what happened?

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.