2016-07-23 79 views
-1

我有一个保存列值的XML文件,我需要将它作为参数传递给我的xsl foreach语句。下面是我的实现,但我没有得到期望的结果:在XSL FO foreach语句中分配一个变量

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中,GroupingColumn1可能包含我应该任意值将它传递给我的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> 

在上表中,我已硬编码的列名作为登录ID,而不是,我想传递变量的名称和创建表。我能够打印GroupingColumn变量,但不知道为什么它不打印输出。

+0

你XSL使得对你的输入XML没有意义。例如:你的XSL有一个xpath“/ PdfPrinter/Reports/Header/GroupingColumn1”,它不存在于XML中的任何地方,所以它不会返回任何内容,也无法诊断你的竞争信息的问题。 –

+0

@KevinBrown:我正在实现基于XML中GroupingColumn1值的1级分组,我应该能够将GroupingColumn1值传递给我的XSL,如上面的代码片段所示。我认为这只是变量赋值,如果需要,我会发布完整的XSLT。请指教。谢谢。 –

+0

任何帮助,非常感谢.. –

回答

0

我给出的其他答案只能在XSLT 2.0中使用,就像在XSLT 1.0中一样“对于use属性或match属性的值来说,包含变量引用的值是错误的。 (见http://www.w3.org/TR/xslt#key

正是在XSLT 1.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> 
+0

非常感谢..略有修改的代码和期望的输出来..非常感谢帮助我.. –

+0

我诚挚的道歉,上述XSLT工作完美。测试它并根据我的要求进行修改。谢谢.. –