Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update content dynamically with XML 2

Status
Not open for further replies.

joshbates15

Technical User
Jun 3, 2005
8
US
This is what I'm trying to do... On my website I would like to update content through an XML document. The XML document would look something like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<aaronsays>
<date>June 13th,2005</date>
<saying>I got wine on my sterling silver tie, trade me ties.</saying>
<saying>I think my mom steals things from my room when I'm gone!</saying>
...
</aaronsays>

Then my HTML document would look like this:
<html>
...
<div id="aaronsays">
<span id="date" style="display:block;;"> *** Date Info from XML goes Here *** </span>
<ul id="sayings">

*** List the saying XML Data as <li>'s in here ***

</ul>
</div>
...
</html>

I know I'm going to need to use some javascript (something I'm not too strong in) to import the XML data into the HTML document, probably through some XMLHttpRequest. I've looked at countless examples, apple, macrumorslive.com, but it just doesn't seem to totally click with me. I've tried the example in the faq, but didn't seem to work corectly. I would also like to make sure it will work in both Firefox and Internet Explorer. Any advice would be much appreciated!
 
XSL is the best method for this:


XSL will transform the XML into HTML. You can do the transform client-side with javascript (a bit messy), server-side with ASP or similar (cleaner, but requires server-side technologies) or you can simply link the stylesheet to the XML file and the browser will apply the transform. I prefer the last option, eg:

XML:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="aaronsays.xsl"?>
<aaronsays>
    <date>June 13th,2005</date>
    <saying>I got wine on my sterling silver tie, trade me ties.</saying>
    <saying>I think my mom steals things from my room when I'm gone!</saying>
</aaronsays>
XSL (aaronsays.xsl):
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
    <xsl:template match="/">
        <html>
            <head>
                <title>Aaron's sayings</title>
            </head>
            <body>
                <div id="aaronsays">
                    <span id="date" style="display:block;">
                        <xsl:value-of select="aaronsays/date"/>
                    </span>
                    <ul id="sayings">
                        <xsl:for-each select="aaronsays/saying">
                            <li>
                                <xsl:value-of select="."/>
                            </li>
                        </xsl:for-each>
                    </ul>
                </div>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top