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


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

我该如何完成此操作?

试试这个方式:

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>

相关内容

  • 没有找到相关文章