Search

This is not directly related to symphony but this is one of the better venues for finding XSL help that I know of. To start with I have some XML that looks like this:

<page-layout> 
   <record> 
     <type>data-list</type> 
     <name>users</name> 
     <content> 
       <headers> 
     <record> 
       <name>ID</name> 
       <column>id</column> 
     </record> 
     <record> 
       <name>Name</name> 
       <column>name</column> 
     </record> 
       </headers> 
       <data> 
     <record> 
       <id>1</id> 
       <name>nick</name> 
       <pass>xxx</pass> 
       <salt>xxx</salt> 
       <auto_pass>xxx</auto_pass> 
     </record> 
       </data> 
     </content> 
     <settings/> 
   </record> 
</page-layout> 

and what I am wanting to do is use the //page-layout/*/content/headers/*/column to designate how to create my table of data. Right now there is only one record in the //page-layout/*/content/data node set but the idea is to have all the data for that data-list in that node set.

right now my xsl looks like this:

<xsl:template match="page-layout/*[type='data-list']">

    <table class="tablemain" cellspacing="0" cellpadding="0">

        <thead>
            <xsl:apply-templates select="content/headers" />
        </thead>

        <tr>
            <xsl:apply-templates select="content/data">
                <xsl:with-param name="columns" select="//content/headers/*/column" />
            </xsl:apply-templates>
        </tr>

    </table>

</xsl:template>

<xsl:template match="page-layout/*[type='data-list']/content/headers/record">
    <th><xsl:apply-templates select="name" /></th>
</xsl:template>

<xsl:template match="page-layout/*[type='data-list']/content/data/record">
    <xsl:param name="columns" />

    <xsl:variable name="data" select="." />

    <tr>
        <xsl:for-each select="$columns">
            <td><xsl:value-of select="." /><xsl:text> ( </xsl:text><xsl:value-of select="name()" /><xsl:text> ) </xsl:text><xsl:text>:</xsl:text><xsl:value-of select="$data/*[name(.) = .]" /></td>
        </xsl:for-each>
    </tr>
</xsl:template>

The last template is what I'm having issues with. Any help would be appreciated :)

Which bit isn't working exactly? The only thing that might be causing issue is the use of . in your very final expression. You could try replacing it with current(). I can never remember when to use which, since there are subtle differences between the two.

Anyway, I copied your XML into a Symphony Static Data Source and got this XSLT working.

<xsl:template match="/">
    <xsl:apply-templates select="//page-layout/record[type='data-list']" mode="table"/>
</xsl:template>

<xsl:template match="record" mode="table">
    <table>
        <thead>
            <xsl:apply-templates select="content/headers/record" mode="header"/>
        </thead>
        <tbody>
            <xsl:apply-templates select="content/data/record" mode="row"/>
        </tbody>
    </table>
</xsl:template>

<xsl:template match="record" mode="header">
    <th><xsl:apply-templates select="name"/></th>
</xsl:template>

<xsl:template match="record" mode="row">
    <xsl:variable name="row-data" select="."/>
    <tr>
        <xsl:for-each select="//content/headers/record/column">
            <td><xsl:value-of select="$row-data/*[name()=current()]"/></td>
        </xsl:for-each>
    </tr>
</xsl:template>

I simplified things somewhat by recognising patterns in the original XML. Each table, header and body row are record nodes, so the templates to create each of these matches simply on a record. A mode is passed to each to ensure the correct one is used. I'm starting to use this approach more often — make matched templates very generic using modes, but make the apply-templates calling them very specific. This way, the more generic templates can be re-used (e.g. you can re-use the heading template anywhere else providing you have a record node in your XML).

Thanks Nick,

I knew this was the right place to come, the secret key was current(), I too mix the two and with all of the things I've been working on today I just couldn't get passed that brick wall. This works wonders, and your idea with the modes simplifies things immensely! Thanks Tons!

Nick

No worries, glad it's sorted.

A couple of references I have bookmarked but always fail to return to:

http://www.devguru.com/Technologies/XSLT/quickref/xslt_function_current.html http://www.stylusstudio.com/docs/v60/d_xpath78.html

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details