0

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.

1
  • It would be much better if you could actually ask a question. I see you have "answered" this yourself, but you don't understand why. At least in your answer, you seem to have grasped that to address the filename element while your template is processing an entry element, the attribute template should be {photo/filename}, and (presumably inspired by @Ragez) that you need to use <xsl:value-of/> to get values in places where attribute templates don't work. Commented Dec 26, 2012 at 16:43

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

That is only outputting plain text. My html tags are getting removed.
even with the <xsl:output method="html" encoding="UTF-8" indent="no"/> ? what are you using for doing the processing ?
also you're html tags where ? also can you update your question with an example of the XML you are manipulationg it would helps.
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: 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 ...
|
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.