Re: [xsl] where's here the error

Subject: Re: [xsl] where's here the error
From: Matthieu Ricaud-Dussarget <matthieu.ricaud@xxxxxxxxx>
Date: Mon, 02 Jan 2012 11:17:21 +0100
Hello Roelof,

I'm not sure about what's going wrong with your script (http://symphony-cms.com/learn/articles/view/overriding-templates/ looks ok ?) but let me just suggest a tip with your XSLT.

Le 31/12/2011 10:07, Roelof Wobben a C)crit :
frontpage-article.xsl :



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

<xsl:template match="section/entry">
    <h2><value-of select="title" /></h2>
</xsl:template>

I guess you expect here that each node will be ignored in the output except "section/entry", but you forgot about XSLT default's templates :
see http://www.dpawson.co.uk/xsl/sect2/defaultrule.html#d3635e76


any other elements of your xml will passed trough these (default) templates :

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

<xsl:template match="text()">
  <xsl:value-of select="."/>
</xsl:template>

that means any text element which is not within a section/entry will go to the ouput.

I suggest you ALWAYS match the root element in any XSLT, then you say what to do for nested elements.

In your case, you can try this :

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

<xsl:template match="/">
	<xsl:apply-templates select="data/recent-posts/section/entry | data/section/section/entry"/>
	<!-- I ignore image/section cause there is no title element here-->
</xsl:template>

<xsl:template match="section/entry">
   <h2><value-of select="title" /></h2>
</xsl:template>

Here you can choose exactly which section element to output.

You can also be more generic :

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

<xsl:template match="/">
	<xsl:apply-templates />
</xsl:template>

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

<xsl:template match="text()">
	<!-- don't ouput text nodes-->
</xsl:template>

<xsl:template match="section/entry">
   <h2><value-of select="title" /></h2>
</xsl:template>


The first 2 templates can be ommited because they do the same as the default xslt templates, but it makes it clear what's happen : - when you encounter the root element, don't ouput anything, see what's happen inside - when you encounter any element, don't ouput anything, see what's happen inside - when you encounter any text node just don't ouput anything - when you encounter section/entry, forget the 2nd template and display the title.

You can do the same more simple :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="text()"/>

<xsl:template match="section/entry">
   <h2><value-of select="title" /></h2>
</xsl:template>


The problem with this generic solution is that if there is no title element (as this is the case for image/section/entry), you will get an empty h2 element. You can add a test here or just deal with the first solution.

Hope this help,

Regards,
Matthieu



--
Matthieu Ricaud
IGS-CP
Service Livre numC)rique

Current Thread