Reputation: 558
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">
<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.
Upvotes: 0
Views: 550
Reputation: 558
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>
Upvotes: 0
Reputation: 27313
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">
<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
Upvotes: 1