2016-03-03 89 views
0

我想从一些XML中产生一个HTML表格,通过SQL查询。生成的XML如下所示:XML/XSLT属性的嵌套循环产生HTML表格

<root> 
<form attribute1="1" attribute2="1" /> 
<form attribute1="1" attribute2="2" /> 
<form attribute1="1" attribute2="3" /> 
<form attribute1="2" attribute2="1" /> 
<form attribute1="2" attribute2="2" /> 
<form attribute1="3" attribute2="1" /> 
</root> 

表我试图产生需要有一个与下排的每个attribute2,这样的事情每一个独特的ATTRIBUTE1标题行:

<attribute1="1" /> 
<attribute2="1" /> 
<attribute2="2" /> 
<attribute2="3" /> 

<attribute1="2" /> 
<attribute2="1" /> 
<attribute2="2" /> 

<attribute1="3" /> 
<attribute2="1" /> 

我没有太多使用XML/XSLT的经验,但我希望能够通过表单循环执行某些操作,为每个唯一属性创建一个标题行1,然后创建与下面的唯一属性1关联的数据行。

+0

这是一个“分组”问题。如果您使用的是XSLT 1.0,则需要使用称为Muenchian分组的技术。请参阅http://www.jenitennison.com/xslt/grouping/muenchian.html获取解释。在这种情况下,您的密钥将是'。如果您使用XSLT 2.0,则可以使用'xsl:for-each-group'。请参阅http://www.xml.com/pub/a/2003/11/05/tr.html。 –

+0

Hi @ Tim-C。我很确定我只使用1.0,所以我将不得不查看Muenchian分组。根据我的理解,这将根据属性1为每个行提供一个键。所以为了继续进行处理,我需要''来获取标题行,然后在'获取数据行? –

+0

我已经添加了一个答案来显示Muenchian分组的实际应用 –

回答

0

如果您只能使用XSLT 1.0,试试这个XSLT对于初学者来说,它使用Muenchian分组的方法

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:key name="form" match="form" use="@attribute1" /> 

    <xsl:template match="root"> 
    <xsl:copy> 
     <!-- Get first "form" element that occurs in each group --> 
     <xsl:for-each select="form[generate-id() = generate-id(key('form',@attribute1)[1])]"> 
      <group> 
      <header><xsl:value-of select="@attribute1" /></header> 
      <!-- Get the "form" elements that make up the group --> 
      <xsl:apply-templates select="key('form', @attribute1)" /> 
      </group> 
     </xsl:for-each> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="form"> 
    <row><xsl:value-of select="@attribute2" /></row> 
    </xsl:template> 
</xsl:stylesheet> 

而且这里的一些XSLT,它可以创建一个HTML表格。这是一个更先进的,因为它计算出一行的最大列数,并使用它为较短的行创建colspan属性

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:key name="form" match="form" use="@attribute1" /> 

    <xsl:variable name="maxCells"> 
     <xsl:for-each select="/root/form[generate-id() = generate-id(key('form',@attribute1)[1])]"> 
      <xsl:sort select="count(key('form', @attribute1))" order="descending" /> 
      <xsl:if test="position() = 1"> 
       <xsl:value-of select="count(key('form', @attribute1))" /> 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:variable> 

    <xsl:template match="root"> 
    <table border="1"> 
     <!-- Get first "form" element that occurs in each group --> 
     <xsl:for-each select="form[generate-id() = generate-id(key('form',@attribute1)[1])]"> 
      <tr> 
       <th colspan="{$maxCells}"> 
        <xsl:value-of select="@attribute1" /> 
       </th> 
      </tr> 
      <tr> 
      <!-- Get the "form" elements that make up the group --> 
       <xsl:apply-templates select="key('form', @attribute1)" /> 
      </tr> 
     </xsl:for-each> 
    </table> 
    </xsl:template> 

    <xsl:template match="form"> 
    <td> 
     <xsl:if test="position() = last()"> 
      <xsl:attribute name="colspan"> 
       <xsl:value-of select="$maxCells - count(key('form', @attribute1)) + 1" /> 
      </xsl:attribute> 
     </xsl:if> 
     <xsl:value-of select="@attribute2" /> 
    </td> 
    </xsl:template> 
</xsl:stylesheet> 
+0

Hi @TimC。我无法得到这个工作 - 它需要生成一个html我会用我试过的代码编辑我的OP从你的。 –

+0

@DavidAustin我在我的答案中添加了一个示例,以显示正在输出的HTML表格。 –