2017-02-14 59 views
0

我试图将大型项目中的XML表转换为HTML表格,而且我对这个XSLT游戏很新。我发现了大量的材料,但我还没有看到任何东西,是相当类似这样的问题,所以我认为我会问:具有相同属性值的XSLT wrap元素(具有行attr的表格)

<table name="my table (2 columns)"> 
    <!-- some column headers --> 
    <colhddef colnum="1">This is column 1</colhddef> 
    <colhddef colnum="2">This is column 2</colhddef> 
    <entry row="1" colnum="1">entry 1</entry> 
    <entry row="1" colnum="2">entry 2</entry> 
    <entry row="2" colnum="1">entry 3</entry> 
    <entry row="2" colnum="2">entry 4</entry> 
    <entry row="3" colnum="1">entry 5</entry> 
    <entry row="3" colnum="2">entry 6</entry> 
    <entry row="4" colnum="1">entry 7</entry> 
    <entry row="4" colnum="2">entry 8</entry> 
</table> 

我期待包裹每一组具有共同属性的行条目与<tr></tr>,并确保列适当放置在表中不会伤害。这可能比我做得更简单...但任何帮助都非常感谢!

奖励积分:哪里可以找到XSLT的优质学习资源?推荐书籍?等等?

再次提前致谢!

+0

至于学习,看到http://stackoverflow.com/tags/xslt/info末尾的资源部分。 –

+0

至于没有找到类似的东西,今天http://stackoverflow.com/questions/42228190/how-do-i-sort-a-list-of-xml-items-into-rows-for-an-html-表似乎是相同的要求,稍微不同的格式,所以尝试适应,如果你坚持发布你的尝试。 –

回答

0

这可能让你开始:

<xsl:template match="table"> 
<table> 
    <xsl:apply-templates select="entry[@colnum='1']"/> 
</table> 
</xsl:template> 

<xsl:template match="entry[@colnum='1']"> 
<xsl:param name='row'><xsl:value-of select='@row'/></xsl:param> 
<tr> 
    <td><xsl:value-of select="."/></td> 
    <xsl:apply-templates select="../entry[@row=$row][@colnum!=1]"/> 
</tr> 
</xsl:template> 

<xsl:template match="entry[@colnum!='1']"> 
<td><xsl:value-of select="."/></td> 
</xsl:template> 

第一个模板创建了一个<table></table>,并填充它选择只从table节点<entry colnum='1'/>节点。

第二个模板将参数$row设置为<entry colnum='1'/>节点的值row属性。然后它创建一个<tr></tr>容器,并添加一个包含此条目文本的节点<td></td>。最后,它从其中row属性匹配$row参数父表选择entry节点和colnum属性不是1

最后模板变成这些选定<entry>节点,具有colnum属性,该属性不是1时,进入一个<td></td>节点。

输出:

<table> 
    <tr> 
    <td>entry 1</td> 
    <td>entry 2</td> 
    </tr> 
    <tr> 
    <td>entry 3</td> 
    <td>entry 4</td> 
    </tr> 
    <tr> 
    <td>entry 5</td> 
    <td>entry 6</td> 
    </tr> 
    <tr> 
    <td>entry 7</td> 
    <td>entry 8</td> 
    </tr> 
</table> 
+0

谢谢!我发现我需要使用这个项目的函数式编程风格来改善。我需要解析的XML文件非常大,XSLT很容易失控并且无法组织。这比我每次尝试都要干净得多。 – pattersonpatterson

相关问题