改变奇怪

时间:2016-12-25 20:51:00

标签: html xml excel xslt openxml

我不明白为什么某些输出范围的样式属性设置为空字符串。

另外,如何简化这种方法来删除循环和条件?

我已经挣扎了好几天了。我感谢任何提供的帮助。我知道xlst距离理想还很远。我只是在学习。

XSLT

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:x="urn:schemas-microsoft-com:office:excel"
                xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
                xmlns:html="http://www.w3.org/TR/REC-html40"
                version="1.0">



    <xsl:output method="html"/>


    <xsl:template match="/">

        <xsl:for-each select="ss:Workbook/ss:Worksheet/ss:Table/ss:Row">
            <xsl:element name="tr">
                <xsl:for-each select="ss:Cell">
                    <xsl:element name="td">

                        <xsl:if test="not(ss:Data/*)"><xsl:value-of select="."/></xsl:if>                         
                        <xsl:for-each select="ss:Data/*">

                            <xsl:call-template name="recursive-template">
                                <xsl:with-param name="myElement" select="."/>
                            </xsl:call-template>  

                        </xsl:for-each>

                    </xsl:element>
                </xsl:for-each>
            </xsl:element>
        </xsl:for-each>            

    </xsl:template>                  


    <xsl:template name="recursive-template">
        <xsl:param name="myElement" />
        <xsl:if test="name($myElement)='Font'">
            <xsl:element name="span">                                                                          
                <xsl:attribute name="style">
                    <xsl:if test="@html:Color">color:<xsl:value-of select="@html:Color"/>; </xsl:if>
                    <xsl:if test="@html:Size">font-size:<xsl:value-of select="@html:Size"/>pt; </xsl:if>
                </xsl:attribute>
                <xsl:choose>
                    <xsl:when test="$myElement/*">
                        <xsl:call-template name="recursive-template">
                            <xsl:with-param name="myElement" select="$myElement/*"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$myElement"/>
                    </xsl:otherwise>
                </xsl:choose>                  
            </xsl:element>                               
        </xsl:if>
        <xsl:if test="name($myElement)!='Font'">
            <xsl:element name="{lower-case(name($myElement))}">
                <xsl:choose>
                    <xsl:when test="$myElement/*">
                        <xsl:call-template name="recursive-template">
                            <xsl:with-param name="myElement" select="$myElement/*"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$myElement"/>
                    </xsl:otherwise>
                </xsl:choose>            
            </xsl:element>
        </xsl:if>
    </xsl:template>    


</xsl:stylesheet>

输出

以下输出是正确的,但具有空样式属性的少数跨度除外。请注意,CSS必须是内联的,以便可以通过电子邮件发送HTML并在GMail中正确显示。

<tr>
   <td>ID</td>
   <td>Date</td>
   <td>Quantity</td>
   <td>Customer</td>
</tr>
<tr>
   <td>754</td>
   <td>2016-11-15T00:00:00.000</td>
   <td>95</td>
   <td><s><span style="">A</span></s><span style="color:#000000; ">jax</span></td>
</tr>
<tr>
   <td>432</td>
   <td>2016-11-03T00:00:00.000</td>
   <td>14</td>
   <td><span style="color:#000000; ">Kr</span><i><span style="">oo</span></i><span style="color:#000000; ">n</span></td>
</tr>
<tr>
   <td>322</td>
   <td>2016-11-01T00:00:00.000</td>
   <td>52</td>
   <td><b><s><span style="">S</span></s></b><span style="color:#000000; ">hou</span><span style="color:#FF0000; ">t</span><span style="color:#000000; ">er K</span><span style="color:#C65911; ">res</span><span style="color:#000000; ">t</span></td>
</tr>

XML

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s151">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="2"/>
   </Borders>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"
    ss:Bold="1"/>
  </Style>
  <Style ss:ID="s152">
   <Alignment ss:Horizontal="Left" ss:Vertical="Bottom" ss:Indent="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="2"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="2"/>
   </Borders>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"
    ss:Bold="1"/>
  </Style>
  <Style ss:ID="s153">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
   <Borders>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="2"/>
   </Borders>
  </Style>
  <Style ss:ID="s154">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
   <Borders/>
   <NumberFormat ss:Format="d\-mmm"/>
  </Style>
  <Style ss:ID="s155">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
   <Borders/>
  </Style>
  <Style ss:ID="s156">
   <Alignment ss:Horizontal="Left" ss:Vertical="Bottom" ss:Indent="1"/>
   <Borders>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="2"/>
   </Borders>
  </Style>
  <Style ss:ID="s161">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="2"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="2"/>
   </Borders>
  </Style>
  <Style ss:ID="s162">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="2"/>
   </Borders>
   <NumberFormat ss:Format="d\-mmm"/>
  </Style>
  <Style ss:ID="s163">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="2"/>
   </Borders>
  </Style>
  <Style ss:ID="s164">
   <Alignment ss:Horizontal="Left" ss:Vertical="Bottom" ss:Indent="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="2"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="2"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
  </Style>
  <Style ss:ID="s165">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
   <Borders>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="2"/>
   </Borders>
   <Interior ss:Color="#92D050" ss:Pattern="Solid"/>
  </Style>
  <Style ss:ID="s166">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
   <Borders/>
   <Interior ss:Color="#92D050" ss:Pattern="Solid"/>
   <NumberFormat ss:Format="d\-mmm"/>
  </Style>
  <Style ss:ID="s167">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
   <Borders/>
   <Interior ss:Color="#92D050" ss:Pattern="Solid"/>
  </Style>
  <Style ss:ID="s170">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="2"
     ss:Color="#FF0000"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="2"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="2"/>
   </Borders>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"
    ss:Bold="1"/>
  </Style>
  <Style ss:ID="s185">
   <Alignment ss:Horizontal="Right" ss:Vertical="Center" ss:Indent="2"/>
   <Borders>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="2"/>
   </Borders>
   <Interior ss:Color="#92D050" ss:Pattern="Solid"/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet4">
  <Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="4"
   ss:DefaultRowHeight="15">
   <Column ss:AutoFitWidth="0" ss:Width="54.75"/>
   <Column ss:AutoFitWidth="0" ss:Width="122.25"/>
   <Column ss:AutoFitWidth="0" ss:Width="54.75"/>
   <Column ss:AutoFitWidth="0" ss:Width="101.25"/>
   <Row ss:Height="15.75">
    <Cell ss:StyleID="s170"><Data ss:Type="String">ID</Data></Cell>
    <Cell ss:StyleID="s151"><Data ss:Type="String">Date</Data></Cell>
    <Cell ss:StyleID="s151"><Data ss:Type="String">Quantity</Data></Cell>
    <Cell ss:StyleID="s152"><Data ss:Type="String">Customer</Data></Cell>
   </Row>
   <Row>
    <Cell ss:StyleID="s153"><Data ss:Type="Number">754</Data></Cell>
    <Cell ss:StyleID="s154"><Data ss:Type="DateTime">2016-11-15T00:00:00.000</Data></Cell>
    <Cell ss:StyleID="s155"><Data ss:Type="Number">95</Data></Cell>
    <Cell ss:StyleID="s156"><ss:Data ss:Type="String"
      xmlns="http://www.w3.org/TR/REC-html40"><S><Font html:Color="#000000">A</Font></S><Font
       html:Color="#000000">jax</Font></ss:Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0" ss:Height="27">
    <Cell ss:StyleID="s165"><Data ss:Type="Number">432</Data></Cell>
    <Cell ss:StyleID="s166"><Data ss:Type="DateTime">2016-11-03T00:00:00.000</Data></Cell>
    <Cell ss:StyleID="s167"><Data ss:Type="Number">14</Data></Cell>
    <Cell ss:StyleID="s185"><ss:Data ss:Type="String"
      xmlns="http://www.w3.org/TR/REC-html40"><Font html:Color="#000000">Kr</Font><I><Font
        html:Color="#000000">oo</Font></I><Font html:Color="#000000">n</Font></ss:Data></Cell>
   </Row>
   <Row ss:Height="19.5">
    <Cell ss:StyleID="s161"><Data ss:Type="Number">322</Data></Cell>
    <Cell ss:StyleID="s162"><Data ss:Type="DateTime">2016-11-01T00:00:00.000</Data></Cell>
    <Cell ss:StyleID="s163"><Data ss:Type="Number">52</Data></Cell>
    <Cell ss:StyleID="s164"><ss:Data ss:Type="String"
      xmlns="http://www.w3.org/TR/REC-html40"><B><S><Font html:Size="14"
         html:Color="#548235">S</Font></S></B><Font html:Color="#000000">hou</Font><Font
       html:Color="#FF0000">t</Font><Font html:Color="#000000">er K</Font><Font
       html:Color="#C65911">res</Font><Font html:Color="#000000">t</Font></ss:Data></Cell>
   </Row>
  </Table>
 </Worksheet>
</Workbook>

1 个答案:

答案 0 :(得分:0)

调用模板时,当前节点不会更改。您需要直接使用myElement参数来实现您的目标。而不是@html:Color使用$myElement/@html:Color

<xsl:attribute name="style">
    <xsl:if test="$myElement/@html:Color">color:<xsl:value-of select="$myElement/@html:Color"/>; </xsl:if>
    <xsl:if test="$myElement/@html:Size">font-size:<xsl:value-of select="$myElement/@html:Size"/>pt; </xsl:if>
</xsl:attribute>

如果您使用匹配,则当前节点将是匹配的节点,并且您的测试将是正确的。

当您的输入结构与输出结构匹配时,匹配提供了一种生成所需输出的简单方法:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:x="urn:schemas-microsoft-com:office:excel"
                xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
                xmlns:html="http://www.w3.org/TR/REC-html40"
                exclude-result-prefixes="x ss html"
                version="2.0">

    <xsl:output method="html"/>

    <xsl:template match="/">
        <xsl:apply-templates select="ss:Workbook/ss:Worksheet/ss:Table/ss:Row"/>
    </xsl:template>

    <xsl:template match="ss:Row">
        <tr><xsl:apply-templates select="ss:Cell"/></tr>
    </xsl:template>

    <xsl:template match="ss:Cell">
        <td><xsl:apply-templates select="ss:Data"/></td>
    </xsl:template>

    <!-- lower case names for embedded html elements -->
    <xsl:template match="html:*">
        <xsl:element name="{lower-case(local-name())}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <!-- turn html font tags into spans with style attributes -->
    <xsl:template match="html:Font">
        <span>
            <xsl:if test="@html:Color or @html:Size">
                <xsl:attribute name="style">
                    <xsl:if test="@html:Color">color: <xsl:value-of select="@html:Color"/>;</xsl:if>
                    <xsl:if test="@html:Size">font-size: <xsl:value-of select="@html:Size"/>pt;</xsl:if>
                </xsl:attribute>
            </xsl:if>
            <xsl:apply-templates/>
        </span>
    </xsl:template>

</xsl:stylesheet>