XSLT+根据表头行的数据类型向单元格添加excel数据类型标记



我正在尝试使用XSLT将ss:Type="Number"分配给表头有数字的列中的所有数据单元。到目前为止,我只能将它正确地分配给标题行。

XSLT:

<!-- match the Columns element and create header row -->
<xsl:template match="Columns/*">
    <xsl:choose>
        <xsl:when test="@Description[boolean(number()) or . = 0]"> 
             <ss:Cell ss:StyleID="1">
               <ss:Data ss:Type="Number"><xsl:apply-templates select="@Description"/></ss:Data>
             </ss:Cell>
             <xsl:call-template name="numberFormat"/>
        </xsl:when>
        <xsl:otherwise>
             <ss:Cell ss:StyleID="1">
               <ss:Data ss:Type="String"><xsl:apply-templates select="@Description"/></ss:Data>
             </ss:Cell>
             <xsl:call-template name="stringFormat"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template name="stringFormat" match="Row/*">
  <ss:Cell>
    <ss:Data ss:Type="String"><xsl:apply-templates/></ss:Data>
  </ss:Cell>
</xsl:template>
<xsl:template name="numberFormat" match="Row/*">
  <ss:Cell>
    <ss:Data ss:Type="Number"><xsl:apply-templates/></ss:Data>
  </ss:Cell>
</xsl:template>

结果:

        <ss:Row>
            <ss:Cell ss:StyleID="1">
<ss:Data ss:Type="Number">2012001 (Number Header)</ss:Data>
            </ss:Cell>
            <ss:Cell ss:StyleID="1">
<ss:Data ss:Type="String">String Header</ss:Data>
</ss:Cell>
        </ss:Row>
        <ss:Row>
            <ss:Cell>
<ss:Data ss:Type="Number">Row2Column1Data</ss:Data>
            </ss:Cell>
            <ss:Cell>
<ss:Data ss:Type="Number">Row2Column2Data</ss:Data>
            </ss:Cell>
        </ss:Row>
        <ss:Row>
            <ss:Cell>
<ss:Data ss:Type="Number">Row3Column1Data</ss:Data>
            </ss:Cell>
            <ss:Cell>
<ss:Data ss:Type="Number">Row3Column2Data</ss:Data>
            </ss:Cell>
        </ss:Row>

XML:

<Rowsets>
 <Rowset>
    <Columns>
        <Column Description="_2012001"/>
        <Column Description="StringHeader"/>
    </Columns>
    <Row>
        <_2012001>Row2Column1Data</_2012001>
        <StringHeader>Row2Column2Data</StringHeader>
    </Row>
    <Row>
        <_2012001>Row3Column1Data</_2012001>
        <StringHeader>Row3Column2Data</StringHeader>
    </Row>
 </Rowset>
</Rowsets>

目前,最后一个模板优先,所以在这种情况下,我的"numberFormat"模板是最后一个,即使我调用模板explicity,ss:type也设置为Number。。。

更新:所需输出

//First header cell contains a numeric value -- the cell should have ss:Type="Number"
            <ss:Row>
                <ss:Cell ss:StyleID="1">
    <ss:Data ss:Type="Number">_2012001 (Number Header)</ss:Data>
                </ss:Cell>
//Second header cell contains a string value -- the cell should have ss:Type="String"
                <ss:Cell ss:StyleID="1">
    <ss:Data ss:Type="String">String Header</ss:Data>
                </ss:Cell>
            </ss:Row>
//ss:Type="Number" for the first column because the header row of that column contains a number
            <ss:Row>
                <ss:Cell>
    <ss:Data ss:Type="Number">Row2Column1Data</ss:Data>
                </ss:Cell>
//ss:Type="String" for the second column because the header row of that column is a String
                <ss:Cell>
    <ss:Data ss:Type="String">Row2Column2Data</ss:Data>
                </ss:Cell>
            </ss:Row>
            <ss:Row>
//ss:Type="Number" for the first column because the header row of that column contains a number
                <ss:Cell>
    <ss:Data ss:Type="Number">Row3Column1Data</ss:Data> 
                </ss:Cell>
//ss:Type="String" for the second column because the header row of that column is a String
                <ss:Cell>
    <ss:Data ss:Type="String">Row3Column2Data</ss:Data>
                </ss:Cell>
            </ss:Row>
    ...etc.

此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ss="some:ss" exclude-result-prefixes="ss">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="/*">
  <t xmlns:ss="some:ss">
    <xsl:apply-templates/>
  </t>
 </xsl:template>
 <xsl:template match="Columns | Row">
  <ss:Row>
   <xsl:apply-templates/>
  </ss:Row>
 </xsl:template>
 <xsl:template priority="2" match=
  "Column[floor(substring(@Description, 2))
         =
          floor(substring(@Description, 2))
         ]
   ">
   <ss:Cell ss:StyleID="1">
     <ss:Data ss:Type="Number">
      <xsl:value-of select="@Description"/>
     </ss:Data>
   </ss:Cell>
  </xsl:template>
   <xsl:template match="Column">
   <ss:Cell ss:StyleID="1">
     <ss:Data ss:Type="String">
      <xsl:value-of select="@Description"/>
     </ss:Data>
   </ss:Cell>
 </xsl:template>
 <xsl:template priority="2" match=
  "Row/*[floor(substring(name(), 2))
        =
         floor(substring(name(), 2))
         ]
  ">
   <ss:Cell>
     <ss:Data ss:Type="Number">
      <xsl:value-of select="."/>
     </ss:Data>
   </ss:Cell>
 </xsl:template>
 <xsl:template match="Row/*">
   <ss:Cell>
     <ss:Data ss:Type="String">
      <xsl:value-of select="."/>
     </ss:Data>
   </ss:Cell>
 </xsl:template>
</xsl:stylesheet>

应用于所提供的XML文档时

<Rowsets>
    <Rowset>
        <Columns>
            <Column Description="_2012001"/>
            <Column Description="StringHeader"/>
        </Columns>
        <Row>
            <_2012001>Row2Column1Data</_2012001>
            <StringHeader>Row2Column2Data</StringHeader>
        </Row>
        <Row>
            <_2012001>Row3Column1Data</_2012001>
            <StringHeader>Row3Column2Data</StringHeader>
        </Row>
    </Rowset>
</Rowsets>

生成所需的正确结果

<t>
   <ss:Row xmlns:ss="some:ss">
      <ss:Cell ss:StyleID="1">
         <ss:Data ss:Type="Number">_2012001</ss:Data>
      </ss:Cell>
      <ss:Cell ss:StyleID="1">
         <ss:Data ss:Type="String">StringHeader</ss:Data>
      </ss:Cell>
   </ss:Row>
   <ss:Row xmlns:ss="some:ss">
      <ss:Cell>
         <ss:Data ss:Type="Number">Row2Column1Data</ss:Data>
      </ss:Cell>
      <ss:Cell>
         <ss:Data ss:Type="String">Row2Column2Data</ss:Data>
      </ss:Cell>
   </ss:Row>
   <ss:Row xmlns:ss="some:ss">
      <ss:Cell>
         <ss:Data ss:Type="Number">Row3Column1Data</ss:Data>
      </ss:Cell>
      <ss:Cell>
         <ss:Data ss:Type="String">Row3Column2Data</ss:Data>
      </ss:Cell>
   </ss:Row>
</t>

解释

  1. 正确的模板和模式匹配。

  2. 利用CCD_ 3恰好是CCD_ 4的事实,当CCD_。

相关内容

  • 没有找到相关文章

最新更新