XML通过XSLT导出到Excel - 添加粗体格式

时间:2017-08-15 05:54:51

标签: xml excel xslt

我使用XSLT文件从某些XML数据生成.xls Excel文件 - 这很有效,并生成有效的XLS文件。

我现在需要扩展它以使一些单元格变粗。这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
    <ERRORCODE>0</ERRORCODE>
    <PRODUCT BUILD="06-06-2017" NAME="FileMaker" VERSION="ProAdvanced 16.0.2"/>
    <METADATA>
        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ColumnA" TYPE="TEXT"/>
        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ColumnB" TYPE="TEXT"/>
        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ColumnC" TYPE="TEXT"/>
        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ColumnD" TYPE="TEXT"/>
        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ColumnE" TYPE="TEXT"/>
    </METADATA>
    <RESULTSET FOUND="6">
        <ROW MODID="0" RECORDID="900">
            <COL>
                <DATA>Created: 15-August-2017</DATA>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA/>
            </COL>
        </ROW>
        <ROW MODID="0" RECORDID="901">
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA/>
            </COL>
            <COL>
                <DATA/>
            </COL>
        </ROW>
        <ROW MODID="1" RECORDID="902">
            <COL>
                <DATA>Name</DATA>
            </COL>
            <COL>
                <DATA>ID</DATA>
            </COL>
            <COL>
                <DATA>Category</DATA>
            </COL>
            <COL>
                <DATA>Number</DATA>
            </COL>
            <COL>
                <DATA>Amount</DATA>
            </COL>
        </ROW>
        <ROW MODID="1" RECORDID="904">
            <COL>
                <DATA>Acme Corp Pty Ltd</DATA>
            </COL>
            <COL>
                <DATA>123456</DATA>
            </COL>
            <COL>
                <DATA>Category 1</DATA>
            </COL>
            <COL>
                <DATA>1</DATA>
            </COL>
            <COL>
                <DATA>$456.78</DATA>
            </COL>
        </ROW>
        <ROW MODID="1" RECORDID="905">
            <COL>
                <DATA>Demo Company Pty Ltd</DATA>
            </COL>
            <COL>
                <DATA>987654</DATA>
            </COL>
            <COL>
                <DATA>Category 2</DATA>
            </COL>
            <COL>
                <DATA>1</DATA>
            </COL>
            <COL>
                <DATA>$789.32</DATA>
            </COL>
        </ROW>
    </RESULTSET>
</FMPXMLRESULT>

我希望此行具有指定的粗体属性:

<ROW MODID="1" RECORDID="902">
            <COL>
                <DATA>Name</DATA>
            </COL>
            <COL>
                <DATA>ID</DATA>
            </COL>
            <COL>
                <DATA>Category</DATA>
            </COL>
            <COL>
                <DATA>Number</DATA>
            </COL>
            <COL>
                <DATA>Amount</DATA>
            </COL>
        </ROW>

但不确定如何在我的XSLT文件中执行此操作。

处理行的XSLT部分如下:

<Style ss:ID="MyTitleStyle">
            <Alignment ss:WrapText="0" />
            <Font  ss:Bold="1"/>
        </Style>

<xsl:for-each select="fmp:FMPXMLRESULT/fmp:RESULTSET/fmp:ROW">

        <Row>
            <xsl:for-each select="fmp:COL">
                <xsl:variable name="i" select="position()" />
                <xsl:variable name="zFieldName" select="/fmp:FMPXMLRESULT/fmp:METADATA/fmp:FIELD[$i]/@NAME" />
                <xsl:variable name="zFieldType" select="/fmp:FMPXMLRESULT/fmp:METADATA/fmp:FIELD[$i]/@TYPE" />
                <!-- We are looping through rows and columns, and now we can examine the cell -->
                <Cell ss:StyleID="MyTextStyle1">
                                    <Data ss:Type="String">
                                        <xsl:value-of select="." />
                                    </Data>
                </Cell>
            </xsl:for-each><!-- next column -->
        </Row>
    </xsl:for-each><!-- next row -->

1 个答案:

答案 0 :(得分:1)

这是针对每个人的修改。如何识别“粗体”行并不是那么清楚。此示例假定标题是包含“名称”

的列的行
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fmp="http://www.filemaker.com/fmpxmlresult"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<xml 
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:fmp="http://www.filemaker.com/fmpxmlresult"
>
<xsl:element name='Table'>
    <xsl:for-each select="//fmp:ROW">
        <Row>
        <!-- If we have a cell with "Name", then consider it the header -->
        <xsl:variable name='header' select='fmp:COL[fmp:DATA="Name"]'/>
        <xsl:for-each select="fmp:COL">
        <Cell>
            <xsl:if test='$header'>
                <xsl:attribute name='ss:StyleID'>MyTextStyle1</xsl:attribute>
            </xsl:if>
            <Data ss:Type="String">
                <xsl:value-of select="." />
            </Data>
           </Cell>
        </xsl:for-each><!-- next column -->
        </Row>
    </xsl:for-each><!-- next row -->
</xsl:element>
</xml>

</xsl:template>

</xsl:stylesheet>