在XSL FO foreach语句中分配变量



我有一个XML文件,其中包含一列的值,我需要将其作为参数传递给xsl-forach语句。以下是我的实现,但我没有得到期望的结果:

XML

      <PdfPrinter>
      <Reports>
      <Report>
      <CreatedDate>2015-10-07T18:07:45</CreatedDate>
      <LogType>ChangePassword</LogType>
      <LoginID>ADMIN</LoginID>
      <Name>XYZ</Name>
      <AppVersion></AppVersion>
      <System>OS</System>
      <UserIPAddress>192.168.1.83</UserIPAddress>
      <LoginDate />
      <LogoutDate />
      <Remarks></Remarks>
      </Report>
      <Report>
      <CreatedDate>2015-10-07T18:09:54</CreatedDate>
      <LogType>ChangePassword</LogType>
      <LoginID>SUPERADMIN</LoginID>
      <Name>ABC</Name>
      <AppVersion></AppVersion>
      <System>OS</System>
      <UserIPAddress>192.168.1.83</UserIPAddress>
      <LoginDate />
      <LogoutDate />
      <Remarks></Remarks>
      </Report>
      <Header>
      <ReportID>AUD002</ReportID>
      <GroupingColumn1>LoginID</GroupingColumn1>
      <PrintedBy>SOS</PrintedBy>
      <PrintedDate>2016-07-22T11:53:59.8826074Z</PrintedDate>
      </Header>
      </Reports>
      </PdfPrinter>

XSLT

//variable declaration
<xsl:key name="Report" match="Report" use="$GroupingColumn" />
<xsl:variable name="GroupingColumn">
<xsl:value-of select="/PdfPrinter/Reports/Header/GroupingColumn1" />
</xsl:variable>
//Usage of the assigned variable 
 <xsl:for-each select="/PdfPrinter/Reports/Report[1]/*[local-name() !='$GroupingColumn']">
   <fo:table-column column-width="proportional-column-width(4.77)"/>
 </xsl:for-each>

在XML中,GroupingIs1可能包含任何值,我应该将其传递给XSL foreach。

    **My XSL TABLE**
    <fo:table border-bottom-width="5pt" 
    width="1200pt" border-bottom-color="rgb(0,51,102)">
    <!--table header-->
      <xsl:for-each select="/PdfPrinter/Reports/Report[1]/*[local-name() != 'LoginID']">
    <fo:table-column column-width="proportional-column-width(4.77)"/>
   </xsl:for-each>
     <fo:table-header>
     <fo:table-row height="20.81pt" display-align="center" overflow="hidden">
    <xsl:for-each select="/PdfPrinter/Reports/Report[1]/*[local-name() != 'LoginID']">
    <fo:table-cell text-align="center" border="rgb(0, 0, 0) solid 1pt" padding="2pt">
      <fo:block color="rgb(0,0,0)" text-align="center" font-weight="normal">
        <xsl:value-of select="name()"/>
      </fo:block>
    </fo:table-cell>
    </xsl:for-each>
     </fo:table-row>
   </fo:table-header>
   <!--table body-->
   <fo:table-body>
     <xsl:for-each 
   select="PdfPrinter/Reports/Report[generate-id() = generate-id(key('Report', LoginID)[1])]">
     <fo:table-row>
      <fo:table-cell number-columns-spanned="{count(*) - 1}">
      <fo:block><xsl:apply-templates select="LoginID" /></fo:block>
    </fo:table-cell>
  </fo:table-row>
  <xsl:for-each select="key('Report', LoginID)">
    <fo:table-row display-align="before">
      <xsl:for-each select="*[local-name() != 'LoginID']">
        <fo:table-cell text-align="center" 
        border-top-color="rgb(0, 0, 0)" 
         border-top-style="solid" border-width="1pt" padding="2pt">
          <fo:block>
            <xsl:value-of select="."/>
          </fo:block>
         </fo:table-cell>
         </xsl:for-each>
         </fo:table-row>
        </xsl:for-each>
         </xsl:for-each>
        </fo:table-body>
        </fo:table>

在上表中,我将列名硬编码为LoginID,而不是传递变量名并创建表。我可以打印GroupingColumn变量,但不知道它为什么不打印输出。

我给出的另一个答案仅适用于XSLT2.0,如XSLT1.0"use属性或match属性的值包含变量引用是错误的"。(请参见http://www.w3.org/TR/xslt#key)

这在XSLT1.0中是可能的,但效率不是很高,但请尝试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="GroupingColumn" select="/PdfPrinter/Reports/Header/GroupingColumn1" />
<xsl:template match="/PdfPrinter">
    <table>
        <header>
            <row>
                <cell>
                    <xsl:value-of select="$GroupingColumn" />
                </cell>
                <xsl:for-each select="Reports/Report[1]/*[local-name() != $GroupingColumn]">
                    <cell>
                      <xsl:value-of select="local-name()" />
                    </cell>
                </xsl:for-each>
            </row>
        </header>
        <body>
            <xsl:variable name="distinct" select="Reports/Report[not(*[local-name() = $GroupingColumn] = preceding-sibling::Report/*[local-name() = $GroupingColumn])]" />
            <xsl:for-each select="$distinct">
                <xsl:variable name="current-grouping-key" select="*[local-name() = $GroupingColumn]" />
                <xsl:variable name="current-group" select="../Report[*[local-name() = $GroupingColumn] = $current-grouping-key]" />
                <xsl:for-each select="$current-group">
                <row>
                    <xsl:if test="position() = 1">
                        <cell rowspan="{count($current-group)}"><xsl:value-of select="$current-grouping-key" /></cell>
                    </xsl:if>
                    <xsl:for-each select="*[local-name() != $GroupingColumn]">
                    <cell>
                      <xsl:value-of select="." />
                    </cell>
                    </xsl:for-each>
                </row>
                </xsl:for-each>
            </xsl:for-each>
        </body>
    </table>
</xsl:template>
</xsl:stylesheet>

相关内容

  • 没有找到相关文章

最新更新