jjzjj

xml - 使用 xslt 向使用 xml 创建的 excel 文件添加样式

coder 2024-06-29 原文

我需要缩进特定单元格中的文本。特定单元格是具有子元素的 xml 元素。

我的 XML 是:

<rows>

<row id="FOLDER1">
    <cell image="blank.gif">Folder 1</cell>
    <cell/>
    <cell/>
    <cell/>
    <cell/>
    <cell/>
    <cell/>
    <cell sum="1">$23</cell>
    <row id="FOLDER2">
        <cell image="blank.gif">Folder 2</cell>
        <cell/>
        <cell/>
        <cell/>
        <cell/>
        <cell/>
        <cell/>
        <cell sum="2">$11</cell>
        <row id="FOLDER3">
            <cell image="blank.gif">Folder 3</cell>
            <cell/>
            <cell/>
            <cell/>
            <cell/>
            <cell/>
            <cell/>
            <cell sum="3">$44</cell>
            <row id="pro1">
                <cell image="blank.gif">Product 1</cell>
                <cell>324234</cell>
                <cell>3.00</cell>
                <cell>Kilo</cell>
                <cell>1.00</cell>
                <cell>No</cell>
                <cell>euro 33.33</cell>
                <cell>euro 33.33</cell>
            </row>
            <row id="pro2">
                <cell image="blank.gif">Product 2</cell>
                <cell>4354354</cell>
                <cell>1.00</cell>
                <cell>Kilo</cell>
                <cell >0.50</cell>
                <cell>No</cell>
                <cell>euro 2.53</cell>
                <cell>euro 1.26</cell>
            </row>

        </row>
        <row id="pro3">
            <cell image="blank.gif">Product 3</cell>
            <cell>435436</cell>
            <cell>10.00</cell>
            <cell>Kilo</cell>
            <cell>Yes</cell>
            <cell>euro 0.36</cell>
            <cell>euro 3.60</cell>
        </row>

        <row id="pro4">
            <cell image="blank.gif">Product 4</cell>
            <cell>435435</cell>
            <cell>3.28</cell>
            <cell>Kilo</cell>
            <cell>1.00</cell>
            <cell>No</cell>
            <cell>euro 17.38</cell>
            <cell>euro 17.38</cell>
        </row>
    </row>
</row>
<row id="FOLDER1">
    <cell image="blank.gif">Folder 4</cell>
    <cell/>
    <cell/>
    <cell/>
    <cell/>
    <cell/>
    <cell/>
    <cell sum="1">$23</cell>
</row>

我的 XSLT 是:

<?xml version="1.0" encoding="ISO-8859-1"?>

<?mso-application progid="Excel.Sheet"?>
<xsl:stylesheet version="1.0"
    xmlns:html="http://www.w3.org/TR/REC-html40"
    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">


  <xsl:param name="header1">Header1</xsl:param>
  <xsl:param name="header2">Header2</xsl:param>
  <xsl:param name="header3">Header3</xsl:param>
  <xsl:param name="header4">Header4</xsl:param>
  <xsl:param name="header5">Header5</xsl:param>
  <xsl:param name="header6">Header6</xsl:param>
  <xsl:param name="header7">Header7</xsl:param>
  <xsl:param name="header8">Header8</xsl:param>

  <xsl:template match="rows">

    <Workbook>
      <Styles>
        <Style ss:ID="Default" ss:Name="Normal">
          <Alignment ss:Vertical="Bottom" />
          <Borders />
          <Font />
          <Interior />
          <NumberFormat />
          <Protection />
        </Style>
        <Style ss:ID="s21">
          <Font ss:Size="22" ss:Bold="1" />
        </Style>
        <Style ss:ID="columnheaders">
          <Font ss:Size="12" ss:Bold="1" />
        </Style>
        <Style ss:ID="s22">
          <Font ss:Size="14" ss:Bold="1" />
        </Style>
        <Style ss:ID="s23">
          <Font ss:Size="10"  />
        </Style>
        <Style ss:ID="s24">
          <Font ss:Size="10" ss:Bold="1" />
        </Style>
      </Styles>

      <Worksheet ss:Name="data">
        <Table>
          <Column ss:AutoFitWidth="0" ss:Width="300" />
          <Column ss:AutoFitWidth="0" ss:Width="95" />
          <Column ss:AutoFitWidth="0" ss:Width="95" />
          <Column ss:AutoFitWidth="0" ss:Width="175" />
          <Column ss:AutoFitWidth="0" ss:Width="186" />
          <Column ss:AutoFitWidth="0" ss:Width="185" />
          <Column ss:AutoFitWidth="0" ss:Width="113" />
          <Column ss:AutoFitWidth="0" ss:Width="133" />


          <Row ss:AutoFitHeight="0" ss:Height="18">
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header1"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header2"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header3"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header4"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header5"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header6"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header7"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header8"/>
              </Data>
            </Cell>
          </Row>

          <xsl:apply-templates select="row"/>

        </Table>
      </Worksheet>

    </Workbook>

  </xsl:template>


  <xsl:template match="row">
    <xsl:variable name="rowID">
      <xsl:number level="any" format="1"/>
    </xsl:variable>
    <Row ss:AutoFitHeight="0" ss:Height="18">
      <xsl:for-each select="cell">
        <xsl:variable name="colID">
          <xsl:number value="position()" format="A"/>
        </xsl:variable>
        <Cell ss:StyleID="s23">
          <Data ss:Type="String">
            <xsl:value-of select="translate(.,'?','&#8364;')"/>
          </Data>
        </Cell>
      </xsl:for-each>
    </Row>

    <xsl:apply-templates select="row"/>
  </xsl:template>


</xsl:stylesheet>

我从转换中生成的输出:

    <?xml version="1.0" encoding="UTF-8"?>
<Workbook xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    <Styles>
        <Style ss:ID="Default" ss:Name="Normal">
            <Alignment ss:Vertical="Bottom"/>
            <Borders/>
            <Font/>
            <Interior/>
            <NumberFormat/>
            <Protection/>
        </Style>
        <Style ss:ID="s21">
            <Font ss:Size="22" ss:Bold="1"/>
        </Style>
        <Style ss:ID="columnheaders">
            <Font ss:Size="12" ss:Bold="1"/>
        </Style>
        <Style ss:ID="s22">
            <Font ss:Size="14" ss:Bold="1"/>
        </Style>
        <Style ss:ID="s23">
            <Font ss:Size="10"/>
        </Style>
        <Style ss:ID="s24">
            <Font ss:Size="10" ss:Bold="1"/>
        </Style>
    </Styles>
    <Worksheet ss:Name="data">
        <Table>
            <Column ss:AutoFitWidth="0" ss:Width="300"/>
            <Column ss:AutoFitWidth="0" ss:Width="95"/>
            <Column ss:AutoFitWidth="0" ss:Width="95"/>
            <Column ss:AutoFitWidth="0" ss:Width="175"/>
            <Column ss:AutoFitWidth="0" ss:Width="186"/>
            <Column ss:AutoFitWidth="0" ss:Width="185"/>
            <Column ss:AutoFitWidth="0" ss:Width="113"/>
            <Column ss:AutoFitWidth="0" ss:Width="133"/>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header1</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header2</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header3</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header4</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header5</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header6</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header7</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header8</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Folder 1</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">$23</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Folder 2</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">$11</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Folder 3</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">$44</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Product 1</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">324234</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">3.00</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Kilo</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">1.00</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">No</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 33.33</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 33.33</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Product 2</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">4354354</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">1.00</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Kilo</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">0.50</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">No</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 2.53</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 1.26</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Product 3</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">435436</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">10.00</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Kilo</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Yes</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 0.36</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 3.60</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Product 4</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">435435</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">3.28</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Kilo</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">1.00</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">No</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 17.38</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 17.38</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Folder 4</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">$23</Data>
                </Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

下面是在 excel 中打开时的样子:

我需要递归地缩进单元格,还需要检查行是否有子行,如果有的话,我需要将它们设为斜体,如果不只是让它们保持原样的话。 它的外观示例:

*Folder1*
   *Folder2*
       *Folder3*
          Product1
          Product2
   Product3
   Product4
 Folder4

这是我应该得到的 xml:

    <?xml version="1.0" encoding="UTF-8"?>
<Workbook xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    <Styles>
        <Style ss:ID="Default" ss:Name="Normal">
            <Alignment ss:Vertical="Bottom"/>
            <Borders/>
            <Font/>
            <Interior/>
            <NumberFormat/>
            <Protection/>
        </Style>
        <Style ss:ID="s21">
            <Font ss:Size="22" ss:Bold="1"/>
        </Style>
        <Style ss:ID="columnheaders">
            <Font ss:Size="12" ss:Bold="1"/>
        </Style>
        <Style ss:ID="s22">
            <Font ss:Size="14" ss:Bold="1"/>
        </Style>
        <Style ss:ID="s23">
            <Font ss:Size="10"/>
        </Style>
        <Style ss:ID="s24">
            <Font ss:Size="10" ss:Bold="1"/>
        </Style>
        <Style ss:ID="parent1">
            <Font ss:Size="10" ss:Italic="1"/>
        </Style>
        <Style ss:ID="parent2">
            <Alignment ss:Indent="1"/>
            <Font ss:Size="10" ss:Italic="1"/>
        </Style>
        <Style ss:ID="parent3">
            <Alignment ss:Indent="2"/>
            <Font ss:Size="10" ss:Italic="1"/>
        </Style>
        <Style ss:ID="children3">
            <Alignment ss:Indent="3"/>
            <Font ss:Size="10"/>
        </Style>
        <Style ss:ID="children2">
            <Alignment ss:Indent="2"/>
            <Font ss:Size="10"/>
        </Style>
    </Styles>
    <Worksheet ss:Name="data">
        <Table>
            <Column ss:AutoFitWidth="0" ss:Width="300"/>
            <Column ss:AutoFitWidth="0" ss:Width="95"/>
            <Column ss:AutoFitWidth="0" ss:Width="95"/>
            <Column ss:AutoFitWidth="0" ss:Width="175"/>
            <Column ss:AutoFitWidth="0" ss:Width="186"/>
            <Column ss:AutoFitWidth="0" ss:Width="185"/>
            <Column ss:AutoFitWidth="0" ss:Width="113"/>
            <Column ss:AutoFitWidth="0" ss:Width="133"/>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header1</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header2</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header3</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header4</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header5</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header6</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header7</Data>
                </Cell>
                <Cell ss:StyleID="columnheaders">
                    <Data ss:Type="String">Header8</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="parent1">
                    <Data ss:Type="String">Folder 1</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">$23</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="parent2">
                    <Data ss:Type="String">Folder 2</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">$11</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="parent3">
                    <Data ss:Type="String">Folder 3</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">$44</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="children3">
                    <Data ss:Type="String">Product 1</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">324234</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">3.00</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Kilo</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">1.00</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">No</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 33.33</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 33.33</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="children3">
                    <Data ss:Type="String">Product 2</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">4354354</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">1.00</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Kilo</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">0.50</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">No</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 2.53</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 1.26</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="children2">
                    <Data ss:Type="String">Product 3</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">435436</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">10.00</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Kilo</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Yes</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 0.36</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 3.60</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="children2">
                    <Data ss:Type="String">Product 4</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">435435</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">3.28</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Kilo</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">1.00</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">No</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 17.38</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">euro 17.38</Data>
                </Cell>
            </Row>
            <Row ss:AutoFitHeight="0" ss:Height="18">
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">Folder 4</Data>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String"/>
                </Cell>
                <Cell ss:StyleID="s23">
                    <Data ss:Type="String">$23</Data>
                </Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

使用我应该得到的 xml 的屏幕截图:

最佳答案

对于缩进,我首先添加一个新参数来保存每个级别所需的缩进大小。在本例中,我使用了四个空格。

<xsl:param name="indentAmount" select="'    '" />

然后,在与 row 元素匹配的模板中,您可以有一个参数来存储要使用的当前标识的大小,该参数最初默认为空字符串(即没有缩进) .

<xsl:template match="row">
   <xsl:param name="indentation" select="''" />

当你递归调用你的行模板时,你只需将参数化的缩进量添加到当前缩进量,以增加它

<xsl:apply-templates select="row">
  <xsl:with-param name="indentation" select="concat($indentAmount, $indentation)" />
</xsl:apply-templates>

那么就简单的输出第一个cell的缩进参数

<Data ss:Type="String">
   <xsl:if test="$colID = 'A'">
      <xsl:value-of select="$indentation" />
   </xsl:if>
  <xsl:value-of select="translate(.,'?','&#8364;')"/>
</Data>

对于斜体,你可以简单地定义一个新的样式

 <Style ss:ID="s23i">
   <Font ss:Size="10" ss:Italic="1" />
 </Style>

然后,您可以根据单元格是否有 row 子元素来设置单元格的样式属性

<Cell>
   <xsl:attribute name="ss:StyleID">
      <xsl:choose>
         <xsl:when test="$colID = 'A' and ../row">s23i</xsl:when>
         <xsl:otherwise>s23</xsl:otherwise>
      </xsl:choose>
   </xsl:attribute>

作为附带问题,您有处理指令 <?mso-application progid="Excel.Sheet"?>在你的 XSLT 中。这不是放置它的正确位置,因为它需要出现在您的输出 XML 中,而目前不会发生这种情况。要输出这个过程指令,需要这样做

  <xsl:processing-instruction name="mso-application">
     <xsl:text>progid="Excel.Sheet"</xsl:text>
  </xsl:processing-instruction>

这就是完整的 XSLT 的所有荣耀....

<xsl:stylesheet version="1.0"
    xmlns:html="http://www.w3.org/TR/REC-html40"
    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">

   <xsl:output method="xml" indent="yes" />
  <xsl:param name="header1">Header1</xsl:param>
  <xsl:param name="header2">Header2</xsl:param>
  <xsl:param name="header3">Header3</xsl:param>
  <xsl:param name="header4">Header4</xsl:param>
  <xsl:param name="header5">Header5</xsl:param>
  <xsl:param name="header6">Header6</xsl:param>
  <xsl:param name="header7">Header7</xsl:param>
  <xsl:param name="header8">Header8</xsl:param>
  <xsl:param name="indentAmount" select="'    '" />

  <xsl:template match="rows">
      <xsl:processing-instruction name="mso-application">
         <xsl:text>progid="Excel.Sheet"</xsl:text>
      </xsl:processing-instruction>
    <Workbook>
      <Styles>
        <Style ss:ID="Default" ss:Name="Normal">
          <Alignment ss:Vertical="Bottom" />
          <Borders />
          <Font />
          <Interior />
          <NumberFormat />
          <Protection />
        </Style>
        <Style ss:ID="s21">
          <Font ss:Size="22" ss:Bold="1" />
        </Style>
        <Style ss:ID="columnheaders">
          <Font ss:Size="12" ss:Bold="1" />
        </Style>
        <Style ss:ID="s22">
          <Font ss:Size="14" ss:Bold="1" />
        </Style>
        <Style ss:ID="s23">
          <Font ss:Size="10"  />
        </Style>
        <Style ss:ID="s23i">
          <Font ss:Size="10" ss:Italic="1" />
        </Style>
        <Style ss:ID="s24">
          <Font ss:Size="10" ss:Bold="1" />
        </Style>
      </Styles>

      <Worksheet ss:Name="data">
        <Table>
          <Column ss:AutoFitWidth="0" ss:Width="300" />
          <Column ss:AutoFitWidth="0" ss:Width="95" />
          <Column ss:AutoFitWidth="0" ss:Width="95" />
          <Column ss:AutoFitWidth="0" ss:Width="175" />
          <Column ss:AutoFitWidth="0" ss:Width="186" />
          <Column ss:AutoFitWidth="0" ss:Width="185" />
          <Column ss:AutoFitWidth="0" ss:Width="113" />
          <Column ss:AutoFitWidth="0" ss:Width="133" />
          <Row ss:AutoFitHeight="0" ss:Height="18">
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header1"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header2"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header3"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header4"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header5"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header6"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header7"/>
              </Data>
            </Cell>
            <Cell ss:StyleID="columnheaders">
              <Data ss:Type="String">
                <xsl:value-of select="$header8"/>
              </Data>
            </Cell>
          </Row>
          <xsl:apply-templates select="row"/>
        </Table>
      </Worksheet>
    </Workbook>
  </xsl:template>


  <xsl:template match="row">
    <xsl:param name="indentation" select="''" />
    <xsl:variable name="rowID">
      <xsl:number level="any" format="1"/>
    </xsl:variable>
    <Row ss:AutoFitHeight="0" ss:Height="18">
      <xsl:for-each select="cell">
        <xsl:variable name="colID">
          <xsl:number value="position()" format="A"/>
        </xsl:variable>
        <Cell>
           <xsl:attribute name="ss:StyleID">
              <xsl:choose>
                 <xsl:when test="$colID = 'A' and ../row">s23i</xsl:when>
                 <xsl:otherwise>s23</xsl:otherwise>
              </xsl:choose>
           </xsl:attribute>
          <Data ss:Type="String">
             <xsl:if test="$colID = 'A'">
                <xsl:value-of select="$indentation" />
             </xsl:if>
            <xsl:value-of select="translate(.,'?','&#8364;')"/>
          </Data>
        </Cell>
      </xsl:for-each>
    </Row>

    <xsl:apply-templates select="row">
       <xsl:with-param name="indentation" select="concat($indentAmount, $indentation)" />
    </xsl:apply-templates>
  </xsl:template>
</xsl:stylesheet>

关于xml - 使用 xslt 向使用 xml 创建的 excel 文件添加样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13299862/

有关xml - 使用 xslt 向使用 xml 创建的 excel 文件添加样式的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  5. ruby - 在 Ruby 中使用匿名模块 - 2

    假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于

  6. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  7. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  8. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  9. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  10. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

随机推荐