views:

39

answers:

3

Hello,

I'm trying to do a easy dynamic with an input XML. My XML code looks like:

    <table>
        <col size="5%">#</col>
        <col size="55%">Title</col>
        <col size="10%">Author</col>
        <col size="10%">Date</col>
        <col size="10%">Modification</col>
        <col size="10%">Actions</col>

        <output>
            <row>1</row>
            <row>Title of the entry</row>
            <row>Administrator</row>
            <row>dd/mm/yyyy hh:mm</row>
            <row>dd/mm/yyyy hh:mm</row>
            <row>Edit Delete</row>
        </output>
    </table>

I'm generating this because I wan't to have one XSLT for the backend panel, that transforms my output XML to a depending on the variables of the different sections.

I did this XSLT code:

                <table>
                    <tr>
                        <xsl:for-each select="page/index/table/col">
                            <td>
                                <xsl:attribute name="width"><xsl:value-of select="size" /></xsl:attribute>
                                <xsl:value-of select="." />
                            </td>
                        </xsl:for-each>
                    </tr>
                    <xsl:for-each select="page/index/table/output">
                        <tr>
                            <xsl:for-each select="row">
                                <td>
                                    <xsl:value-of select="." />
                                </td>
                            </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                </table>

I generate the table, but I can't get the attribute WIDTH filled with the value of col/size.

How can I do it?

Thanks in advance, Isern

+1  A: 

The selector for the size attribute should read @size rather than just size, i.e.:

<xsl:value-of select="@size" />
Will Prescott
A: 

Try @size instead of size: You're looking for an attribute, not an element.

tdammers
+4  A: 

Use:

  <td width="{@size}"><xsl:value-of select="."/></td>

In XSLT it is recommended to avoid using <xsl:for-each> if possible.

The complete transformation without <xsl:for-each> is:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="table">
   <table>
    <tr>
      <xsl:apply-templates select="col"/>
    </tr>
    <xsl:apply-templates select="output"/>
   </table>
 </xsl:template>

 <xsl:template match="col">
  <td width="{@size}"><xsl:value-of select="."/></td>
 </xsl:template>

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

 <xsl:template match="row">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document (the original XML fragment, wrapped in index and page elements):

<page>
    <index>
        <table>
            <col size="5%">#</col>
            <col size="55%">Title</col>
            <col size="10%">Author</col>
            <col size="10%">Date</col>
            <col size="10%">Modification</col>
            <col size="10%">Actions</col>
            <output>
                <row>1</row>
                <row>Title of the entry</row>
                <row>Administrator</row>
                <row>dd/mm/yyyy hh:mm</row>
                <row>dd/mm/yyyy hh:mm</row>
                <row>Edit Delete</row>
            </output>
        </table>
    </index>
</page>

the wanted, correct result is produced:

<table>
   <tr>
      <td width="5%">#</td>
      <td width="55%">Title</td>
      <td width="10%">Author</td>
      <td width="10%">Date</td>
      <td width="10%">Modification</td>
      <td width="10%">Actions</td>
   </tr>
   <tr>
      <td>1</td>
      <td>Title of the entry</td>
      <td>Administrator</td>
      <td>dd/mm/yyyy hh:mm</td>
      <td>dd/mm/yyyy hh:mm</td>
      <td>Edit Delete</td>
   </tr>
</table>
Dimitre Novatchev
@Dimitre: +1 Good answer. Using AVT in literal result makes compact code.
Alejandro