Link to home
Start Free TrialLog in
Avatar of P-Daddy
P-Daddy

asked on

XML to HTML table using Classic ASP (XSLT?)

I have a project where I need to create a HTML table using data I get from an XML file. I'm using classic ASP to load the XML.

Here's an example of what the HTML table needs to look like in the final output.

User generated image

Below is an example of the type of XML data I receive. The repeating "days" in each "transit-time" node need to be consolidated (they're the row headers). The various sizes in the "description" are the column headers.

How do I go about doing this and is classic ASP enough? Or, do I need to use XSLT with some kind of grouping?

Thanks!

<?xml version="1.0"?>
<inv-balance>

<item color-code="50" description="G200 ASH.GREY S" item-number="0620404A3" price="$2.60" size-code="3" special-expiry="06/30/12" specialPrice="$1.92" style-code="G200">
  <transit-time days="0">386</transit-time>
  <transit-time days="1">6602</transit-time>
  <transit-time days="2">9090</transit-time>
  <transit-time days="3">88755</transit-time>
  <transit-time days="5">2394</transit-time>
  <transit-time days="All">107227</transit-time>
  </item>

  <item color-code="50" description="G200 ASH.GREY M" item-number="0620404A4" price="$2.60" size-code="4" special-expiry="06/30/12" specialPrice="$1.92" style-code="G200">
  <transit-time days="0">422</transit-time>
  <transit-time days="1">9428</transit-time>
  <transit-time days="2">12162</transit-time>
  <transit-time days="3">86798</transit-time>
  <transit-time days="5">3523</transit-time>
  <transit-time days="All">112333</transit-time>
  </item>

  <item color-code="50" description="G200 ASH.GREY L" item-number="0620404A5" price="$2.60" size-code="5" special-expiry="06/30/12" specialPrice="$1.92" style-code="G200">
  <transit-time days="0">548</transit-time>
  <transit-time days="1">14810</transit-time>
  <transit-time days="2">17335</transit-time>
  <transit-time days="3">84832</transit-time>
  <transit-time days="5">5611</transit-time>
  <transit-time days="All">123136</transit-time>
  </item>

  <item color-code="50" description="G200 ASH.GREY XL" item-number="0620404A6" price="$2.60" size-code="6" special-expiry="06/30/12" specialPrice="$1.92" style-code="G200">
  <transit-time days="0">644</transit-time>
  <transit-time days="1">15040</transit-time>
  <transit-time days="2">16954</transit-time>
  <transit-time days="3">50635</transit-time>
  <transit-time days="5">5995</transit-time>
  <transit-time days="All">89268</transit-time>
  </item>

  <item color-code="50" description="G200 ASH.GREY 2XL" item-number="0620404A7" price="$4.19" size-code="7" special-expiry="06/30/12" specialPrice="$3.39" style-code="G200">
  <transit-time days="0">343</transit-time>
  <transit-time days="1">5374</transit-time>
  <transit-time days="2">7016</transit-time>
  <transit-time days="3">26033</transit-time>
  <transit-time days="5">2592</transit-time>
  <transit-time days="All">41358</transit-time>
  </item>

  <item color-code="50" description="G200 ASH.GREY 3XL" item-number="0620404A8" price="$4.33" size-code="8" special-expiry="06/30/12" specialPrice="$3.39" style-code="G200">
  <transit-time days="0">153</transit-time>
  <transit-time days="1">1242</transit-time>
  <transit-time days="2">1896</transit-time>
  <transit-time days="3">4699</transit-time>
  <transit-time days="5">803</transit-time>
  <transit-time days="All">8793</transit-time>
  </item>

  <item color-code="50" description="G200 ASH.GREY 4XL" item-number="0620404A9" price="$4.49" size-code="9" special-expiry="06/30/12" specialPrice="$3.39" style-code="G200">
  <transit-time days="0">18</transit-time>
  <transit-time days="1">78</transit-time>
  <transit-time days="2">159</transit-time>
  <transit-time days="3">237</transit-time>
  <transit-time days="5">62</transit-time>
  <transit-time days="All">554</transit-time>
  </item>
  <item color-code="50" description="G200 ASH.GREY 5XL" item-number="0620404AA" price="$4.63" size-code="0" special-expiry="06/30/12" specialPrice="$3.39" style-code="G200">
  <transit-time days="0">16</transit-time>
  <transit-time days="1">295</transit-time>
  <transit-time days="2">215</transit-time>
  <transit-time days="3">2127</transit-time>
  <transit-time days="5">119</transit-time>
  <transit-time days="All">2772</transit-time>
  </item>

  </inv-balance>

Open in new window

Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

I would simply use an XSLT to get that done

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:output indent="yes"/>
    
    <xsl:template match="inv-balance">
        <table>
            <tr>
                <th></th>
                <xsl:apply-templates select="item" mode="header-row"/>
            </tr>
            <xsl:apply-templates select="item[1]/transit-time"/>
        </table>
    </xsl:template>
    
    <xsl:template match="item" mode="header-row">
        <th>
            <xsl:value-of select="substring-after(substring-after(@description, ' '), ' ')"></xsl:value-of>
        </th>
    </xsl:template>
    
    <xsl:template match="transit-time">
        <xsl:variable name="this-days" select="@days"/>
        <tr>
            <xsl:attribute name="class">
                <xsl:choose>
                    <xsl:when test="position() mod 2 = 1">odd</xsl:when>
                    <xsl:otherwise>even</xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <td><xsl:value-of select="$this-days"/></td>
            <xsl:for-each select="//item/transit-time[@days = $this-days]">
                <td><xsl:value-of select="."/></td>
            </xsl:for-each>
        </tr>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of neeraj523
neeraj523
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Having done both XSLT and DOM manipulation a lot, I have to disagree that for data manipulation DOM processing is the better choice... for most transformations other than the really straightforward ones.

There is one gotcha in the proposed solution from neeraj
Set oXML = Server.CreateObject("Microsoft.XMLDOM")
I really don't understand why people are still using this early DOM object (it has been abandonned by Microsoft over 10 years ago and only still works for backwards compatibility reasons)
For more than 10 years already this is the better choice, since it is more robust, performs better and has less errors in it
Set oXML = Server.CreateObject("MSXML2.DOMDocument")
I am telling you this because if you are doing the XSLT, you will need the same object, so please use the modern version
In order to run the XSLT (untested)

<%
Dim objXML
Dim objXSLT
Dim objTpl
Dim objProc

Set objXML = Server.CreateObject("MSXML2.DOMDocument")
objXML.async = False
objXML.load(Server.MapPath("your-xml.xml"))

Set objXSLT = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
objXSLT.async = False
objXSLT.load(Server.MapPath("your-xsl.xsl"))

Set objTpl = Server.CreateObject("MSXML2.XSLTemplate")
objTpl.stylesheet = objXSLT

Set objProc = objTpl.createProcessor()
objProc.input = objXML
objProc.transform()

Response.Write objProc.output

%>

Open in new window

Avatar of P-Daddy
P-Daddy

ASKER

I tried the ASP solution first since that's what I'm most familiar with. It worked. The XSLT solution looks like another viable alternative. However, I did not test it.

Thanks all for the help.
welcome,
I hope you did use the correct ActiveX object in your solution... you should since it is superior

(and you could have at least rewarded the first and perfectly viable solution with an assist, even if you don't use it)
Avatar of P-Daddy

ASKER

My bad. Is it too late to do that?

I did use the correct ActiveX object.
OK, is good that you do use the correct object

don't bother breaking the question open again, it would have been a pleasant appreciation, nothing more :-)