2010-08-11 61 views
0

尝试创建一个表,其中行被转换为列,以便我可以在逻辑上将这些列分组为XML。 的XML件事情是这样的:在XSL中将行转换为列:FO

<root> 
    <field name="field1"> 
    <string>field1.row1</string> 
    <string>field1.row2</string> 
    <string>field1.row3</string> 
    <string>field1.row4</string> 
    </field> 
    <field name="field2"> 
    <string>field2.row1</string> 
    <string>field2.row2</string> 
    <string>field2.row3</string> 
    <string>field2.row4</string> 
    </field> 
    <field name="field3"> 
    <string>field3.row1</string> 
    <string>field3.row2</string> 
    <string>field3.row3</string> 
    <string>field3.row4</string> 
    </field> 
</root> 

这意味着每场有许多细胞都显示在列,取而代之的是场跨越列,它显示了几行。

我试图创建一些xsl(无效)来显示此信息作为水平分组表。

任何想法如何做到这一点?

回答

0

此样式表生成一个简单的XSL-FO表。它把field元件成列,且将string元件成行:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:variable name="fieldCount" select="count(/root/field)" /> 
    <xsl:variable name="stringCount" select="count(/root/field[1]/string)" /> 

    <xsl:template match="root"> 
     <fo:table-and-caption> 
      <fo:table> 
       <fo:table-header> 
        <fo:table-row> 
         <xsl:call-template name="header"> 
          <xsl:with-param name="currentCol" select="'1'"/> 
          <xsl:with-param name="maxCols" select="$fieldCount"/> 
         </xsl:call-template> 
        </fo:table-row> 
       </fo:table-header> 
       <fo:table-body> 
        <xsl:call-template name="row"> 
         <xsl:with-param name="currentRow" select="'1'"/> 
         <xsl:with-param name="maxRows" select="$stringCount" /> 
        </xsl:call-template> 
       </fo:table-body> 
      </fo:table> 
     </fo:table-and-caption> 
    </xsl:template> 

    <xsl:template name="header"> 
     <xsl:param name="currentCol"/> 
     <xsl:param name="maxCols" /> 

     <fo:table-cell> 
      <fo:block font-weight="bold"> 
       <xsl:text>Column </xsl:text> 
       <xsl:value-of select="field[position()=$currentCol]/@name"/> 
      </fo:block> 
     </fo:table-cell> 

     <xsl:if test="$currentCol &lt; $maxCols"> 
      <xsl:call-template name="header"> 
       <xsl:with-param name="currentCol" select="$currentCol+1"/> 
       <xsl:with-param name="maxCols" select="$maxCols"/> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 

    <xsl:template name="row"> 
     <xsl:param name="currentRow"/> 
     <xsl:param name="maxRows" /> 

     <fo:table-row> 
      <xsl:call-template name="cell"> 
       <xsl:with-param name="currentRow" select="$currentRow" /> 
       <xsl:with-param name="currentCol" select="'1'" /> 
       <xsl:with-param name="maxCols" select="$fieldCount" /> 
      </xsl:call-template> 
     </fo:table-row> 

     <xsl:if test="$currentRow &lt; $maxRows"> 
      <xsl:call-template name="row"> 
       <xsl:with-param name="currentRow" select="$currentRow+1"/> 
       <xsl:with-param name="maxRows" select="$maxRows" /> 
      </xsl:call-template> 
     </xsl:if> 

    </xsl:template> 

    <xsl:template name="cell"> 
     <xsl:param name="currentRow"/> 
     <xsl:param name="currentCol"/> 
     <xsl:param name="maxCols" /> 

     <xsl:apply-templates select="field[position()=$currentCol]/string[position()=$currentRow]"/> 

     <xsl:if test="$currentCol &lt; $maxCols"> 
      <xsl:call-template name="cell"> 
       <xsl:with-param name="currentRow" select="$currentRow"/> 
       <xsl:with-param name="currentCol" select="$currentCol+1" /> 
       <xsl:with-param name="maxCols" select="$maxCols" /> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 

    <xsl:template match="string"> 
     <fo:table-cell> 
      <fo:block> 
       <xsl:value-of select="."/> 
      </fo:block> 
     </fo:table-cell> 
    </xsl:template> 

</xsl:stylesheet> 

输出从样本XML生成:

<?xml version="1.0" encoding="UTF-16"?> 
<fo:table-and-caption xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <fo:table> 
     <fo:table-header> 
      <fo:table-row> 
       <fo:table-cell> 
        <fo:block font-weight="bold">Column field1</fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block font-weight="bold">Column field2</fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block font-weight="bold">Column field3</fo:block> 
       </fo:table-cell> 
      </fo:table-row> 
     </fo:table-header> 
     <fo:table-body> 
      <fo:table-row> 
       <fo:table-cell> 
        <fo:block>field1.row1</fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block>field2.row1</fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block>field3.row1</fo:block> 
       </fo:table-cell> 
      </fo:table-row> 
      <fo:table-row> 
       <fo:table-cell> 
        <fo:block>field1.row2</fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block>field2.row2</fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block>field3.row2</fo:block> 
       </fo:table-cell> 
      </fo:table-row> 
      <fo:table-row> 
       <fo:table-cell> 
        <fo:block>field1.row3</fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block>field2.row3</fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block>field3.row3</fo:block> 
       </fo:table-cell> 
      </fo:table-row> 
      <fo:table-row> 
       <fo:table-cell> 
        <fo:block>field1.row4</fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block>field2.row4</fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block>field3.row4</fo:block> 
       </fo:table-cell> 
      </fo:table-row> 
     </fo:table-body> 
    </fo:table> 
</fo:table-and-caption>