将样式添加到使用 xslt 用 xml 创建的 excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13299862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:46:28  来源:igfitidea点击:

Add style to excel file created with xml using xslt

xmlxsltspreadsheetxslt-1.0

提问by Aleks

I need to indent the text in specific cells.The specific cells are xml elements that have children.

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

My XML is:

我的 XML 是:

<rows>

<row id="FOLDER1">
    <cell image="blank.gif">Folder 1</cell>
    <cell/>
    <cell/>
    <cell/>
    <cell/>
    <cell/>
    <cell/>
    <cell sum="1"></cell>
    <row id="FOLDER2">
        <cell image="blank.gif">Folder 2</cell>
        <cell/>
        <cell/>
        <cell/>
        <cell/>
        <cell/>
        <cell/>
        <cell sum="2"></cell>
        <row id="FOLDER3">
            <cell image="blank.gif">Folder 3</cell>
            <cell/>
            <cell/>
            <cell/>
            <cell/>
            <cell/>
            <cell/>
            <cell sum="3"></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"></cell>
</row>

My XSLT is:

我的 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>

My output generated from the transformations:

我从转换生成的输出:

    <?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"></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"></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"></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"></Data>
                </Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

And here how it looks like when opened in excel: screenshot

这是在 excel 中打开时的样子: 截屏

I need to indent the cells recursively and also to check if the rows have children if they do i need to make them italic,if not just to leave them unedited. Example of how it should look like:

我需要递归地缩进单元格,并检查行是否有子项,如果有的话,我需要将它们设为斜体,如果不只是为了让它们不被编辑。它应该是什么样子的示例:

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

Here is the xml i should get:

这是我应该得到的 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"></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"></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"></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"></Data>
                </Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

Screenshot using the xml i should get: enter image description here

使用我应该得到的 xml 的屏幕截图: 在此处输入图片说明

回答by Tim C

For the indentation, I would start off by adding a new parameter to hold the size of the indentation you require for each level. In this case, I am using four spaces.

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

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

Then, within your template that matches the rowelement, you can have a parameter that stores the size of the current identation to use, which will initially default to an empty string (i.e no indentation).

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

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

When you recursively call your row template, you just add on the paramterised indentation amount to the current indentation, to increase it

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

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

Then, it is simply a case of outputing the indentation parameter for the first cell

那么,它只是输出第一个单元格的缩进参数的情况

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

For the italics, you can simply define a new style

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

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

Then, you can just set the style attribute of the cell depending on whether it has a rowelement as a child

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

<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>

As a side issue, you have the processing instruction <?mso-application progid="Excel.Sheet"?>in your XSLT. This is not the right place for it, as it needs to appear in your output XML, and this won't happen currently. To output this process instruction, you need to do this

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

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

Here is the full XSLT in all its glory then....

这是完整的 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>