XSL to show parent as columns, with matching children as rows

后端 未结 1 1669
耶瑟儿~
耶瑟儿~ 2021-01-28 12:13

I am new to XML and XSL. I am trying to take a XML strings generated by our vendor software and translate it into Excel format. The XML can have repeating Rows that each have a

相关标签:
1条回答
  • 2021-01-28 12:50

    IIUC, you want to transpose the given table - rows to columns, fields to rows. The foilowing stylesheet shows how this can be done. For simplicity, this generates an HTML table - the adjustment for an Excel worksheet should be trivial.

    XSLT 1.0

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="/RpcData">
        <xsl:variable name="col" select="RepeatingFieldSet/Row"/>
        <xsl:variable name="row" select="RepeatingFieldSet/Row[1]/Fld"/>
        <table border="1">
            <!-- header row -->
            <tr>
                <th>Field</th>
                <xsl:for-each select="$col">
                    <th>
                        <xsl:value-of select="@Index"/>
                    </th>
                 </xsl:for-each>
            </tr>
            <!-- data rows -->
            <xsl:for-each select="$row">
                <xsl:variable name="i" select="position()"/>
                <tr>
                    <th><xsl:value-of select="@Nm"/></th>
                    <xsl:for-each select="$col">
                        <td>
                            <xsl:value-of select="Fld[$i]"/>
                        </td>
                    </xsl:for-each>
                </tr>    
            </xsl:for-each>
        </table>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Applied to your example input, the (rendered) result will be:

    0 讨论(0)
提交回复
热议问题