使用XSLT <table> </table>从XML生成动态

时间:2010-08-03 12:26:45

标签: xml xslt xhtml html-table

我正在尝试使用输入XML做一个简单的动态<table>。我的XML代码如下:

    <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>

我正在生成这个,因为我不想为后端面板安装一个XSLT,它会根据不同部分的变量将输出XML转换为<table>

我做了这个XSLT代码:

                <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>

我生成了表格,但是我无法使用col / size的值填充WIDTH属性。

我该怎么做?

3 个答案:

答案 0 :(得分:3)

使用

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

在XSLT中,建议尽可能避免使用<xsl:for-each>

没有<xsl:for-each>的完整转换是:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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>

将此转换应用于以下XML文档(原始XML片段,包含在indexpage元素中):

<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>

产生了想要的正确结果

<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>

答案 1 :(得分:1)

size属性的选择器应该是@size,而不仅仅是size,即:

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

答案 2 :(得分:0)

尝试使用@size代替size:您正在寻找属性,而不是元素。