Darren Cook
Darren Cook

Reputation: 721

Displaying XML in asp page

I use Get Clicky for my web analytics and there is an API for generating custom reports.

The output of the request is sent back in either XML CSV JSON or PHP.

My site uses ASP and I would like to know if I request the output to be sent back to my page as XML how do I (can I) display it.

This is an example of a response from the Get Clicky API.

<response status="ok">
<type type="visitors">
<date date="2011-11-10">
<item>
<value>54</value>
</item>
</date>
</type>
<type type="actions">
<date date="2011-11-10">
<item>
<value>102</value>
</item>
</date>
</type>
<type type="actions-average">
<date date="2011-11-10">
<item>
<value>1.9</value>
</item>
</date>
</type>
<type type="time-average">
<date date="2011-11-10">
<item>
<value>209</value>
</item>
</date>
</type>
<type type="bounce-rate">
<date date="2011-11-10">
<item>
<value>39</value>
</item>
</date>
</type>
</response>

Upvotes: 2

Views: 1293

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189525

Best way to handle this is with some XSLT.

I'll start with the assumption that you've got an MSXML DOM loaded with the XML you included in your question.

Here is the code to take that and generate some HTML output:-

Dim xsl : Set xsl = CreateObject("MSXML2.DOMDocument.3.0")

xsl.async = false
xsl.load Server.MapPath("/ClickyResponseRender.xsl")

Response.Write yourLoadedDOM.TransformNode(xsl)

Here is some basic content for the "ClickyResponseRender.xsl" file that the above expects to be in the root folder.

<?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="/response">
    <html>
    <body>
      <table>
        <thead>
          <tr>
            <th>Type</th>
            <th>Date</th>
            <th>Value</th>
          </tr>
        </thead>
        <tbody>
          <xsl:apply-templates select="type" />
        </tbody>
      </table>
    </body>
    </html>
</xsl:template>

  <xsl:template match="type">
    <tr>
      <td>
        <xsl:value-of select="@type"/>
      </td>
      <td>
        <xsl:value-of select="date/@date"/>
      </td>
      <td align="right">
        <xsl:value-of select="date/item/value"/>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet> 

This just generates a simple table listing each type with its date and value. However I suspect the schema may be a little more complex (e.g multiple dates per type) so the XSL would need to be modified to match the needs.

Upvotes: 2

Related Questions