views:

379

answers:

3

I'm trying to use the Macromedia XSLTransform class to convert XML returned from Amazon Web Services to HTML. Here's the PHP page that calls the transform:

<?php
require_once('includes/MM_XSLTransform/MM_XSLTransform.class.php'); 

$restquery = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&amp;AWSAccessKeyId=[myid]&amp;Operation=ItemLookup&amp;ResponseGroup=Large&amp;ItemId=" . htmlspecialchars($_GET["asin"]);
$mm_xsl = new MM_XSLTransform();
$mm_xsl->setXML($restquery);
$mm_xsl->setXSL("aws1.xsl");
echo $mm_xsl->Transform();
?>

And here's a snippet of the aws1.xsl page

<?xml version="1.0"?>
<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2006-06-07"&gt;

  <xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes"/>

  <xsl:template match="aws:Item">
    <html>
      <body>
        <table>
          <tr>
            <td style="border-bottom:#C0C0C0 dotted 1px;padding:10px">
              <table cellpadding="0" cellspacing="0" style="width: 90%;padding:5px">
                <tr>
                  <xsl:if test="aws:SmallImage/aws:URL">
                    <td valign="top" width="50">
                      <img>
                        <xsl:attribute name="src">
                          <xsl:value-of select="aws:SmallImage/aws:URL" disable-output-escaping="yes" />
                        </xsl:attribute>
                        <xsl:attribute name="border">0</xsl:attribute>
                      </img>
                    </td>
                  </xsl:if>
                  <!-- bunch of other stuff -->
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

The basic code is working - I get data on the expected ASIN item back. And I know the XSL basically works because if I intentionally put an invalid attribute in I get a parser error. But what I get back is a big unformatted bunch of text instead of HTML. I've tried various <xsl:output method> options, but none seems to work. I thinks it's some kind of encoding or charset problem.

A: 

Set your content-type to indicate that you're sending HTML instead of XML.

header('Content-type: text/html');
Welbog
+1  A: 

You need to create a rule to match "/" or XSL will implicitly generate one for you based on a to-text conversion of the document tree.

I would rewrite the XSL to this:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2006-06-07"&gt;

  <xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <html>
      <head>...</head>
      <body>
        <table>
          <thead>...</thead>
          <tbody>
            <xsl:apply-templates select="//aws:Item"/>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="aws:Item">
    <tr>
      <td>...</td>
    </tr>
  </xsl:template>

</xsl:stylesheet>

OR add this template to your existing xsl

<xsl:template match="*|@*">
<xsl:apply-templates select="*|@*"/>
</xsl:template>

Both templates would match document root ("/") which is the ONLY implicit match that XSL does. This first would make "/" map to the html tag which would create a table in which each table row maps to aws:item. The second would match all nodes (and attributes), output nothing, and then attempt to match all children. The first is better if you only want to process aws:Item, the second is better if you want to process all nodes. The second may (depending on the optimization features of you xslt processor and your particular xslt document) take much longer to process.

KitsuneYMG
@kts: There is no such thing as a <code> tag on this site. But there is a handy "format as code" button above the editor. ;-)
Tomalak
A: 

i want to create registration form using php and vbscript and displaying data in xml foarmat?

swapnil
You should better ask this as a new question, not post it as an answer to some other question. But you should give more details about what you have done so far and where your problems are, so people can answer. The "Ask Question" button is in the top right.
sth