tags:

views:

58

answers:

2

The XML

        <?xml version="1.0" encoding="utf-8" ?>
<data>
    <events />
    <tour>
        <section id="3" handle="tour">Tour</section>
        <entry id="15">
            <title handle="dummy-entry-1">Dummy Entry 1</title>
            <description mode="formatted">Lorem ipsum dolor sit amet...</description>
            <photo size="24 KB" path="/images/tour" type="image/jpeg">
                <filename>no-image.jpg</filename>
                <meta creation="2010-03-07T17:00:24-08:00" width="1000" height="1000" />
            </photo>
        </entry>
    </tour>
</data>

The XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:template match="/data/tour/entry">
  <img src="{filename}"/>
  <h2>{heading}</h2>
  {description}
 </xsl:template>
</xsl:stylesheet>

Here is the code I am working with. I am a newbie to XSLT and I suppose I am not understanding how it transforms my XML into HTML entirely. This snippet of code I plan on loading via AJAX, so I really all I need it to output is a blank html document consisting only of these three items.

I realize I am omitting the xsl:output tag and that is because I really don't understand how I can get this to just simply match that information to my tags in my xml without adding , tags etc. All it outputs is a string of text in an html document.

If it helps, I am working in the Symphony CMS environment.

+1  A: 

first you have to add this

 <xsl:output method="html" encoding="UTF-8" indent="no"/>

It would tell your XSLT parser that you want to output HTML.

to output some content from the XML to HTML you have to use value-of

<xsl:value-of select="/xpath/of/xml"/>

also depending of the complexity of your output you night need to use <xsl:template name="name">, it would permit you to make reusable template in the XSLT file.

so in your example

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

<xsl:output method="html" encoding="UTF-8" indent="no"/>
<xsl:template match="/">
  <xsl:variable name="filename" select="/data/tour/entry/photo/filename"/>
  <img src="{$filename}"/>
  <h2><xsl:value-of select="/data/tour/entry/title"/></h2>
  <xsl:value-of select="/data/tour/entry/description"/>
 </xsl:template>
</xsl:stylesheet>

Hope this helps.

PHP snippet

<?php

$xml = new DOMDocument;

$xmlC = file_get_contents('data.xml');
$xml->loadXml( $xmlC);

$xsl = new DOMDocument();
$xsl->load('template.xlst');


// Configuration du transformateur
$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl); // attachement des règles xsl
$proc->registerPHPFunctions();
header('Content-Type: text/xml');
echo $proc->transformToXML($xml);

PS: updated with your XML. I would try in PHP to see if it run ok PS2: updated with running PHP sample and update template

RageZ
That is only outputting plain text. My html tags are getting removed.
jaasum
even with the `<xsl:output method="html" encoding="UTF-8" indent="no"/>` ? what are you using for doing the processing ?
RageZ
also you're html tags where ? also can you update your question with an example of the XML you are manipulationg it would helps.
RageZ
I am learning xml, xslt with the Symphony CMS. I don't know if any thing from that would be messing with my output. Rendering this in Safari and Firefox.
jaasum
@jaasum: updated
RageZ
@jaasum: I am not sure if your issue is fixed but it would have been nice to accept my answer on my PHP the code is working fine ...
RageZ
I stepped away from the project for today. I am not sure if I understand the Symphony CMS environment to feed my own php script to a page template yet. I am feeling that I may need to abandon loading this content via ajax and just go with a different on-page approach. I appreciate your help though, it's given me more insight into working with XSLT.
jaasum
A: 

This ended up being the answer. I need to change the way I went about applying my templates. Not sure why it fixed it, but it did.

<?xml version="1.0" encoding="UTF-8"?>

<xsl:template match="/">
    <xsl:apply-templates select="data/tour-entry/entry" />
</xsl:template>

<xsl:template match="/data/tour-entry/entry">
    <img src="{$root}/image/2/675/300/5/images/tour/{photo/filename}" alt="" class="image" />
    <h2><xsl:value-of select="title"/></h2>
    <xsl:copy-of select="description"/>
    <script type="text/javascript">
        $(document).ready(function(){
            // How cufon applies our font to certain elements, styles based on css document.
            Cufon.replace('#content-main h2');
        });
    </script>   
</xsl:template>

jaasum