2016-08-10 126 views
0

我正在创建一个Excel文档,其中单元格内的某些文本将以不同的颜色或粗体格式化。以单元格文本格式将XSLT XML转换为Excel

例如我的电子表格应该是这样的: 。

我想格式化的文本在我的XML文件中包含标签。 &lt;strong>。我已经编写了一个函数来提取这些标签,并用 替换它们的颜色或粗体格式,例如<Font html:color="#0000FF"> text </Font>创建xml输出以在Excel中打开时。

问题是,打开电子表格时,文本不会被着色,而是会被格式化命令<Font html:color="#0000FF">: 环绕。

这里是我的输入XML

<?xml version="1.0" encoding="UTF-8"?> 
<analysis> 
    <datasetList> 
     <dataset> 
     <datasetLabel>Subject Level Analysis</datasetLabel> 
     <datasetName>ADSL</datasetName> 
     <datasetOrdinal>1</datasetOrdinal> 
     <datasetStructure>One record per subject</datasetStructure> 
     <datasetContext> 
      <datasetClass> 
       <datasetClass>ADSL</datasetClass> 
      </datasetClass> 
     </datasetContext> 
     <columnList>    
      <column> 
       <columnLabel>Analysis Visit</columnLabel>    
       <columnName>AVISIT</columnName>    
       <columnDerivationList> 
        <columnDerivation> 
        <columnDerivationDescription>Set to collected visit name [EG.VISIT] &lt;keepordrop>Set to 'POST-BASELINE MINIMUM'&lt;/keepordrop></columnDerivationDescription>      
        </columnDerivation> 
        <columnDerivation>      
        <columnDerivationDescription>Set to a re-defined visit range based on user-defined input. &lt;strong>Set to 'POST-BASELINE MINIMUM'&lt;/strong></columnDerivationDescription> 
        </columnDerivation> 
       </columnDerivationList> 
      </column> 
      <column> 
       <columnLabel>Analysis Visit (N)</columnLabel>    
       <columnName>AVISITN</columnName>    
       <columnDerivationList> 
        <columnDerivation>      
        <columnDerivationDescription>Set to collected &lt;edit>Set to 9997&lt;/edit></columnDerivationDescription> 
        </columnDerivation> 
        <columnDerivation>      
        <columnDerivationDescription>Set to a user defined numeric value &lt;select>Set to 9997 wih the analysis visit &lt;/select></columnDerivationDescription> 
        </columnDerivation> 
       </columnDerivationList> 
      </column>   
     </columnList>   
     </dataset> 
    </datasetList> 
</analysis> 

这里是我的完整XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<?mso-application progid="Excel.Sheet"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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" xmlns:gdsr="http://somethinghere.com" > 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 


    <!-- 
*************************************************************************************** 
            START OF WORKBOOK LAYOUT 
*************************************************************************************** 
--> 
    <xsl:template match="/" > 

     <xsl:processing-instruction name="mso-application">progid="Excel.Sheet"</xsl:processing-instruction> 
     <ss: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"> 

      <xsl:call-template name="document-properties"/> 
      <xsl:call-template name="document-styles"/> 



      <!-- ****************************************************************************************** --> 
      <!-- CREATE THE TABS IN THE WORKBOOK --> 
      <!-- ****************************************************************************************** -->   
      <xsl:call-template name="getDset"/> 


      <!-- ****************************************************************************************** --> 

     </ss:Workbook> 
    </xsl:template> 


    <!-- TEMPLATE FOR DATASET --> 


    <xsl:template name="getDset"> 
     <xsl:for-each select="//dataset"> 
      <ss:Worksheet ss:Name="{.//datasetName}"><!-- Add filters to the columns --> 
       <Names> 
        <NamedRange ss:Name="_FilterDatabase" ss:RefersTo="={.//datasetName}!R1C1:R1C2" ss:Hidden="1"/> 
       </Names> 
       <ss:Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="200" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="30">  
        <Column ss:Width="70"/> 
        <Column ss:Width="70"/> 


        <Row> 
         <Cell ss:StyleID="sHead"> 
          <Data ss:Type="String"> 
           Header1 
          </Data> 
          <NamedCell ss:Name="_FilterDatabase"/> 
         </Cell> 
         <Cell ss:StyleID="sHead"> 
          <Data ss:Type="String"> 
           Header2 
          </Data> 
          <NamedCell ss:Name="_FilterDatabase"/> 
         </Cell>   
        </Row> 


        <xsl:for-each select="columnList/column"> 
         <xsl:if test="columnName !=' ' "> 

          <Row> 
           <Cell ss:StyleID="sBody" > 
            <Data ss:Type="String"> 
             <xsl:value-of select="columnName"/> 
            </Data> 
           </Cell> 

<!-- SET COLOR OF TEXT WHEN TAGGED --> 
           <xsl:variable name="columnDerivationDescription"> 
            <xsl:value-of select="columnDerivationList/columnDerivation/columnDerivationDescription"/> 
           </xsl:variable> 
           <xsl:variable name="columnDerivationDescription1"> 
            <xsl:value-of select="gdsr:set-font($columnDerivationDescription)"/> 
           </xsl:variable> 

           <xsl:choose> 
            <xsl:when test="contains($columnDerivationDescription1, 'Font')">  
           <Cell ss:StyleID="sBody"> 
            <ss:Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40"> 
             <xsl:for-each select="columnDerivationList/columnDerivation">       
              <xsl:if test="columnDerivationDescription !=' ' "> 
               <xsl:call-template name="LFsToBRs"> 
                <xsl:with-param name="input" select="gdsr:set-font(columnDerivationDescription)"/> 
               </xsl:call-template> 
              </xsl:if>           
             </xsl:for-each> 
            </ss:Data> 
           </Cell> 
           </xsl:when> 

           <xsl:otherwise> 
            <Cell ss:StyleID="sBody"> 
             <Data ss:Type="String"> 
              <xsl:for-each select="columnDerivationList/columnDerivation">             
               <xsl:if test="columnDerivationDescription !=' ' "> 
                <xsl:call-template name="LFsToBRs"> 
                 <xsl:with-param name="input" select="columnDerivationDescription"/> 
                </xsl:call-template> 
               </xsl:if>            
              </xsl:for-each> 
             </Data> 
            </Cell> 
           </xsl:otherwise>  
           </xsl:choose>  
<!-- COLOUR TAGGED. NICELY DONE --> 
          </Row> 

         </xsl:if> 
        </xsl:for-each> 
       </ss:Table> 
       <xsl:call-template name="worksheet-options"/> 

       <AutoFilter x:Range="R1C1:R1C2" xmlns="urn:schemas-microsoft-com:office:excel"/> 
      </ss:Worksheet> 
     </xsl:for-each> 
    </xsl:template> 


    <!--FUNCTIONS--> 

    <xsl:function name="gdsr:set-font"> 
     <xsl:param name="text"/> 

     <xsl:variable name="blue-font">&lt;Font html:color="#0000FF"&gt;</xsl:variable> 
     <xsl:variable name="blue-font-end"><xsl:text disable-output-escaping="no">&lt;/Font&gt;</xsl:text></xsl:variable> 


     <xsl:variable name="pink-font">&lt;Font html:color="#7030A0"&gt;</xsl:variable> 
     <xsl:variable name="pink-font-end"><xsl:text disable-output-escaping="no">&lt;/Font&gt;</xsl:text></xsl:variable> 

     <xsl:variable name="var-1" select="replace($text,'&lt;select&gt;',$blue-font)"/> 
     <xsl:variable name="var-2" select="replace($var-1,'&lt;/select&gt;',$blue-font-end)"/> 

     <xsl:variable name="var-3" select="replace($var-2,'&lt;strong&gt;','&lt;b&gt;')"/> 
     <xsl:variable name="var-4" select="replace($var-3,'&lt;/strong&gt;','&lt;/b&gt;')"/> 

     <xsl:variable name="var-5" select="replace($var-4,'&lt;keepordrop&gt;',$pink-font)"/> 
     <xsl:variable name="var-6" select="replace($var-5,'&lt;/keepordrop&gt;',$pink-font-end)"/> 

     <xsl:variable name="var-7" select="replace($var-6,'&lt;edit&gt;',$blue-font)"/> 
     <xsl:variable name="var-8" select="replace($var-7,'&lt;/edit&gt;',$blue-font-end)"/> 

     <xsl:variable name="var-81" select="replace($var-8,'&lt;select&gt;&lt;option&gt;',$blue-font)"/> 
     <xsl:variable name="var-82" select="replace($var-81,'&lt;/option&gt;&lt;option&gt;','BLUEFONTENDOP1 | BLUEFONTSTARTOP2')"/> 
     <xsl:variable name="var-83" select="replace($var-82,'BLUEFONTENDOP1',$blue-font-end)"/> 
     <xsl:variable name="var-84" select="replace($var-83,'BLUEFONTSTARTOP2',$blue-font)"/> 
     <xsl:variable name="var-85" select="replace($var-84,'&lt;/option&gt;&lt;/select&gt;',$blue-font-end)"/> 

     <xsl:variable name="var-9"> 
      <xsl:analyze-string select="$var-85" 
       regex="({$blue-font})(.*?)({$blue-font-end}|{$blue-font})"> 
       <xsl:matching-substring> 
        <xsl:choose> 
         <xsl:when test="regex-group(3)=$blue-font-end"> 
          <xsl:value-of select="regex-group(0)"/> 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:value-of select="regex-group(1)"/> 
          <xsl:value-of select="regex-group(2)"/> 
         </xsl:otherwise> 
        </xsl:choose> 
       </xsl:matching-substring> 
       <xsl:non-matching-substring> 
        <xsl:value-of select="."/> 
       </xsl:non-matching-substring> 
      </xsl:analyze-string> 
     </xsl:variable> 

     <xsl:variable name="var-10"> 
      <xsl:analyze-string select="$var-9" 
       regex="({$blue-font-end})(.*?)({$blue-font-end}|{$blue-font})"> 
       <xsl:matching-substring> 
        <xsl:choose> 
         <xsl:when test="regex-group(3)=$blue-font"> 
          <xsl:value-of select="regex-group(0)"/> 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:value-of select="regex-group(2)"/> 
          <xsl:value-of select="regex-group(3)"/> 
         </xsl:otherwise> 
        </xsl:choose> 
       </xsl:matching-substring> 
       <xsl:non-matching-substring> 
        <xsl:value-of select="."/> 
       </xsl:non-matching-substring> 
      </xsl:analyze-string> 
     </xsl:variable> 

     <xsl:value-of select="$var-10"/> 
    </xsl:function>  


    <!-- 
    *************************************GENERAL TEMPLATES************************************************** 
    --> 

    <xsl:template name="LFsToBRs"> 
     <xsl:param name="input"/> 
     <xsl:choose> 
      <xsl:when test="contains($input, '&#10;')"> 
       <xsl:value-of select="substring-before($input, '&#10;')"/> 
       <xsl:text disable-output-escaping="yes">&amp;#10;</xsl:text>  
       <xsl:call-template name="LFsToBRs"> 
        <xsl:with-param name="input" select="substring-after($input, '&#10;')"/> 
       </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 

       <xsl:value-of select="$input"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

    <!-- EXCEL WORKSHEET OPTIONS -->   
    <xsl:template name="worksheet-options"> 
     <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
      <PageSetup> 
       <Header x:Margin="0.3"/> 
       <Footer x:Margin="0.3"/> 
       <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> 
      </PageSetup> 
      <Selected/> 
      <FreezePanes/> 
      <FrozenNoSplit/> 
      <SplitHorizontal>1</SplitHorizontal> 
      <TopRowBottomPane>1</TopRowBottomPane> 
      <ActivePane>2</ActivePane> 
      <Panes> 
       <Pane> 
        <Number>3</Number> 
        <ActiveRow>18</ActiveRow> 
        <ActiveCol>2</ActiveCol> 
       </Pane> 
      </Panes> 
      <ProtectObjects>False</ProtectObjects> 
      <ProtectScenarios>False</ProtectScenarios> 
     </WorksheetOptions> 
    </xsl:template> 

    <!-- EXCEL DOCUMENT PROPERTIES --> 
    <xsl:template name="document-properties"> 
     <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> 
      <Author>Mason, Huw {MDBZ~Basel}</Author> 
      <LastAuthor>Mason, Huw {MDBZ~Basel}</LastAuthor> 
      <Created></Created> 
      <Company></Company> 
      <Version>1</Version> 
     </DocumentProperties> 
     <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> 
      <WindowHeight>14370</WindowHeight> 
      <WindowWidth>27795</WindowWidth> 
      <WindowTopX>480</WindowTopX> 
      <WindowTopY>120</WindowTopY> 
      <ProtectStructure>False</ProtectStructure> 
      <ProtectWindows>False</ProtectWindows> 
     </ExcelWorkbook> 
    </xsl:template> 

    <!-- EXCEL DOCUMENT STYLES --> 
    <xsl:template name="document-styles"> 
     <ss:Styles> 
      <ss:Style ss:ID="Default" ss:Name="Normal"> 
       <ss:Alignment ss:Vertical="Bottom" ss:WrapText="1"/> 
       <ss:Borders/> 
       <ss:Font ss:FontName="Arial" x:Family="Swiss" ss:Size="10" ss:Color="#000000"/> 
       <ss:Interior/> 
       <ss:NumberFormat/> 
       <ss:Protection/> 
      </ss:Style><!-- **STYLES CREATED TO REF LATER ON e.g. STYLE FOR COL HEADER**--> 
      <ss:Style ss:ID="sHead"> 
       <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/> 
       <ss:Borders> 
        <ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> 
       </ss:Borders> 
       <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="9" ss:Color="#FFFFFF"/> 
       <ss:Interior ss:Color="#538DD5" ss:Pattern="Solid"/> 
      </ss:Style> 
      <ss:Style ss:ID="sHeadAC"> 
       <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/> 
       <ss:Borders> 
        <ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> 
       </ss:Borders> 
       <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="9" ss:Color="#000000"/> 
       <ss:Interior ss:Color="#FFFF66" ss:Pattern="Solid"/> 
      </ss:Style> 
      <ss:Style ss:ID="sHeadCT"> 
       <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/> 
       <ss:Borders> 
        <ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> 
       </ss:Borders> 
       <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="9" ss:Color="#FFFFFF"/> 
       <ss:Interior ss:Color="#FF9900" ss:Pattern="Solid"/> 
      </ss:Style> 
      <ss:Style ss:ID="sHeadGD"> 
       <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/> 
       <ss:Borders> 
        <ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> 
       </ss:Borders> 
       <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="9" ss:Color="#DA9694"/> 
       <ss:Interior ss:Color="#000000" ss:Pattern="Solid"/> 
      </ss:Style> 
      <ss:Style ss:ID="sHeadPRM"> 
       <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/> 
       <ss:Borders> 
        <ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> 
       </ss:Borders> 
       <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="9" ss:Color="#FFFFFF"/> 
       <ss:Interior ss:Color="#92D050" ss:Pattern="Solid"/> 
      </ss:Style>   
      <ss:Style ss:ID="sHeadALG"> 
       <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/> 
       <ss:Borders> 
        <ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> 
       </ss:Borders> 
       <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="9" ss:Color="#FFFFFF"/> 
       <ss:Interior ss:Color="#92D050" ss:Pattern="Solid"/> 
      </ss:Style>   
      <ss:Style ss:ID="sBody"> 
       <Alignment ss:Horizontal="Left" ss:Vertical="Top" ss:WrapText="1"/> 
       <ss:Borders> 
        <ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> 
       </ss:Borders> 
       <ss:Interior ss:Pattern="Solid"/> 
      </ss:Style> 
      <Style ss:ID="SHyperlink" ss:Name="Hyperlink"> 
       <Alignment ss:Horizontal="Left" ss:Vertical="Top" ss:WrapText="1"/> 
       <Borders> 
        <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> 
        <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> 
       </Borders> 
       <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="10" ss:Color="#0000FF" ss:Underline="Single"/> 
      </Style> 
     </ss:Styles> 
    </xsl:template> 


</xsl:stylesheet> 

这里是我的输出XML

<Row> 
      <Cell ss:StyleID="sBody"> 
       <Data ss:Type="String">AVISIT</Data> 
      </Cell> 
      <Cell ss:StyleID="sBody"> 
       <ss:Data xmlns="http://www.w3.org/TR/REC-html40" ss:Type="String">Set to collected visit name [EG.VISIT] &lt;Font html:color="#7030A0"&gt;Set to 'POST-BASELINE MINIMUM'&lt;/Font&gt;Set to a re-defined visit range based on user-defined input. &lt;b&gt;Set to 'POST-BASELINE MINIMUM'&lt;/b&gt;</ss:Data> 
      </Cell> 
     </Row> 
     <Row> 
      <Cell ss:StyleID="sBody"> 
       <Data ss:Type="String">AVISITN</Data> 
      </Cell> 
      <Cell ss:StyleID="sBody"> 
       <ss:Data xmlns="http://www.w3.org/TR/REC-html40" ss:Type="String">Set to collected &lt;Font html:color="#0000FF"&gt;Set to 9997&lt;/Font&gt;Set to a user defined numeric value &lt;Font html:color="#0000FF"&gt;Set to 9997 wih the analysis visit &lt;/Font&gt;</ss:Data> 
      </Cell> 
     </Row> 

我创建了一个XSL和样本保存为XML电子表格2003以检查代码并如预期。当我更换<>与&lt;&gt;我得到我与我的输出

我不知道,如果遇到有关创建XML时使用&lt;而非<问题。有没有人有类似的问题,并解决了它?或者可以看到我的问题在哪里?

编辑:

我做了更新XSLT公司。 html命名空间,正如Martin所建议的那样。这创建了我期望并且看起来很好的XML。

现在的问题是,当在Excel中打开格式不显示,我得到普通的黑色文本。

Output without formatting

我创建的它应该是什么的例子,并保存为XML电子表格2003,比较两个和他们是一样的。有趣的是:如果我从Excel创建的示例复制代码并将其粘贴到xslt创建的XML中,然后在Excel中打开它,格式化工作。 如果我从xslt创建的xml中复制代码并将其粘贴到我创建的excel示例中(该功能工作正常),现在格式化不再有效。

Excel created example

了Excel的XML代码创造价值

<Row ss:AutoFitHeight="0"> 
    <Cell ss:StyleID="s64"><Data ss:Type="String">AVISIT</Data></Cell> 
    <Cell ss:StyleID="s65"><ss:Data ss:Type="String" 
     xmlns="http://www.w3.org/TR/REC-html40">Set to collected visit name [EG.VISIT] <Font 
     html:Color="#7030A0">Set to 'POST-BASELINE MINIMUM'</Font>Set to a re-defined visit range based on user-defined input. <B>Set to 'POST-BASELINE MINIMUM'</B></ss:Data></Cell> 
    </Row> 
    <Row ss:AutoFitHeight="0"> 
    <Cell ss:StyleID="s64"><Data ss:Type="String">AVISITN</Data></Cell> 
    <Cell ss:StyleID="s65"><ss:Data ss:Type="String" 
     xmlns="http://www.w3.org/TR/REC-html40">Set to collected <Font 
     html:Color="#0000FF">Set to 9997</Font>Set to a user defined numeric value<Font 
     html:Color="#0000FF">Set to 9997 wih the analysis visit</Font></ss:Data></Cell> 
    </Row> 

这是我的XSLT创建的XML的外观:

<Row> 
    <Cell ss:StyleID="sBody"> 
     <Data ss:Type="String">AVISIT</Data> 
    </Cell> 
    <Cell ss:StyleID="sBody"> 
     <ss:Data xmlns="http://www.w3.org/TR/REC-html40" ss:Type="String">Set to collected visit name [EG.VISIT] <Font html:Color="#7030A0">Set to 'POST-BASELINE MINIMUM'</Font>Set to a re-defined visit range based on user-defined input. <B>Set to 'POST-BASELINE MINIMUM'</B> 
     </ss:Data> 
    </Cell> 
</Row> 
<Row> 
    <Cell ss:StyleID="sBody"> 
     <Data ss:Type="String">AVISITN</Data> 
    </Cell> 
    <Cell ss:StyleID="sBody"> 
     <ss:Data xmlns="http://www.w3.org/TR/REC-html40" ss:Type="String">Set to collected <Font html:Color="#0000FF">Set to 9997</Font>Set to a user defined numeric value <Font html:Color="#0000FF">Set to 9997 wih the analysis visit </Font> 
     </ss:Data> 
    </Cell> 
</Row> 

所以尽管XML代码是一样的,取决于它是如何创建的,它定义了它是否有效。

问题: 难道会有一些编码差异来解释这种行为吗?

+0

我首先手动创建你想在Excel中看到的内容,然后导出为XML来查看Excel需要什么,然后在XSLT中创建_that_。 –

+0

这是很多代码,但是返回带有'xsl:value-of'的文本节点的函数绝对不会创建'font'元素。至于整个方法,你已经使用了XSLT 2.0,你使用了哪个XSLT处理器?使用Saxon 9的9.7版本(甚至是HE版本),您可以切换到XSLT 3.0,然后使用https://www.w3.org/TR/xpath-functions-31/#func-parse-xml-片段放在'​​columnDerivationDescription'的内容上,只需将内容推送到适当的模板,根据需要转换'select'元素。这比尝试解析数据要好得多。 –

+0

感谢@MartinHonnen,我目前使用的是Saxon EE 9.6。我会试着抓住9.7并尝试一下你的解决方案。再次感谢。 – Huw

回答

0

下面是使用parse-xml-fragmenthttps://www.w3.org/TR/xpath-functions-31/#func-parse-xml-fragment,提供撒克逊9.7(所有版本在XSLT使用version="3.0"时))你怎么可以处理这个问题,然后模板变换你有逃脱的标记内的元素在columnDerivationDescription元素:

<xsl:template name="getDset"> 
     <xsl:for-each select="//dataset"> 
      <ss:Worksheet ss:Name="{.//datasetName}"><!-- Add filters to the columns --> 
       <Names> 
        <NamedRange ss:Name="_FilterDatabase" ss:RefersTo="={.//datasetName}!R1C1:R1C2" ss:Hidden="1"/> 
       </Names> 
       <ss:Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="200" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="30">  
        <Column ss:Width="70"/> 
        <Column ss:Width="70"/> 


        <Row> 
         <Cell ss:StyleID="sHead"> 
          <Data ss:Type="String"> 
           Header1 
          </Data> 
          <NamedCell ss:Name="_FilterDatabase"/> 
         </Cell> 
         <Cell ss:StyleID="sHead"> 
          <Data ss:Type="String"> 
           Header2 
          </Data> 
          <NamedCell ss:Name="_FilterDatabase"/> 
         </Cell>   
        </Row> 


        <xsl:for-each select="columnList/column"> 
         <xsl:if test="columnName !=' ' "> 

          <Row> 
           <Cell ss:StyleID="sBody" > 
            <Data ss:Type="String"> 
             <xsl:value-of select="columnName"/> 
            </Data> 
           </Cell> 
<!-- SET COLOR OF TEXT WHEN TAGGED --> 
           <Cell ss:StyleID="sBody"> 
            <Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40"> 
             <xsl:apply-templates select="columnDerivationList/columnDerivation/columnDerivationDescription"/> 
            </Data> 
           </Cell> 
<!-- COLOUR TAGGED. NICELY DONE --> 
          </Row> 

         </xsl:if> 
        </xsl:for-each> 
       </ss:Table> 
       <xsl:call-template name="worksheet-options"/> 

       <AutoFilter x:Range="R1C1:R1C2" xmlns="urn:schemas-microsoft-com:office:excel"/> 
      </ss:Worksheet> 
     </xsl:for-each> 
    </xsl:template> 

    <xsl:template match="columnDerivationDescription"> 
     <xsl:apply-templates select="parse-xml-fragment(.)/node()"/> 
    </xsl:template> 

    <xsl:template match="strong"> 
     <b> 
      <xsl:apply-templates/> 
     </b> 
    </xsl:template> 

    <xsl:template match="edit"> 
     <Font html:color="#0000FF"> 
      <xsl:apply-templates/> 
     </Font> 
    </xsl:template> 

    <!-- add further templates here for the other elements and transformations you need --> 
+0

这是创建xml我现在得到例如'设置为收集的访问名称[EG.VISIT] 设置为'POST-BASELINE MINIMUM''。电子表格不再显示格式命令,但它也没有显示格式,我只是得到普通的黑色文本,没有颜色,没有粗体文​​本。 – Huw

+0

@Huw,请参阅编辑,我并不十分熟悉Excel XML格式的细节,但可能会添加名称空间声明'xmlns =“http://www.w3.org/TR/REC-html40”'修正问题。如果不是,请检查在使用Excel直接以所需输出格式保存工作表时获得的格式,并尝试调整XSLT代码以创建格式。 –

+0

命名空间'xmlns =“urn:schemas-microsoft-com:office:spreadsheet”'出现在标签中,这是单元格格式不适合我的原因。 [链接](http://stackoverflow.com/questions/22911085/xslt-2-0-remove-xmlns-attribute)为此提供了一个工作解决方案。 – Huw