使用XSLT将具有列方式数据的XML转换为逐行表格形式

时间:2014-12-24 17:13:38

标签: xml xslt xslt-1.0

I have below XML which is coming from client side , I need to transform that into table using XSLT. The data is coming Column wise and I need create a table by selecting first Column Data of every Column element and then second Column Data of every Column element and put that in <tr>.

注意:元素列包含第一,第二,第三列数据 嵌套元素列数据包含每列的第1行...行N数据。

<?xml version="1.0" encoding="utf-8"?>
<ColumnSet> /* This is group of all columns*/
  <Columnn value="ColumnName1" > /*This contains first column's rows*/
    <ColumnData value="R1C1"/> /*This is Row 1 of Column 1 */
    <ColumnData value="R2C1"/>  
  </Columnn>
  <Columnn value="ColumnName2" > /*This element has second column's all rows*/
    <ColumnData value="R1C2"/>
    <ColumnData value="R2C2"/>  
  </Columnn>
</ColumnSet>                  

I want to see the result in below form
ColumnName1      ColumnaName2
R1C1                 R1C2
R2C1                 R2C2

我如何做到这一点?

1 个答案:

答案 0 :(得分:2)

以这种方式尝试:

XSLT 1.0

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

<xsl:template match="/ColumnSet">
    <xsl:variable name="cols" select="Columnn"/>
    <xsl:variable name="rows" select="Columnn[1]/ColumnData"/>
    <table>
        <tr>
            <xsl:for-each select="$cols">
                <th><xsl:value-of select="@value"/></th>
             </xsl:for-each>
        </tr>
        <xsl:for-each select="$rows">
            <xsl:variable name="i" select="position()"/>
            <tr>
                <xsl:for-each select="$cols">
                    <td><xsl:value-of select="ColumnData[$i]/@value"/></td>
                </xsl:for-each>
            </tr>    
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>