Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 在XSL FO foreach语句中分配变量_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

Xml 在XSL FO foreach语句中分配变量

Xml 在XSL FO foreach语句中分配变量,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我有一个保存列值的XML文件,我需要将其作为参数传递给xsl foreach语句。下面是我的实现,但我没有得到预期的结果: XML <PdfPrinter> <Reports> <Report> <CreatedDate>2015-10-07T18:07:45</CreatedDate> <LogType>ChangePassword</LogType>

我有一个保存列值的XML文件,我需要将其作为参数传递给xsl 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>

2015-10-07T18:07:45
控件
管理
XYZ
操作系统
192.168.1.83
2015-10-07T18:09:54
控件
超级管理员
基础知识
操作系统
192.168.1.83
AUD002
罗吉尼
紧急求救信号
2016-07-22T11:53:59.8826074Z
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>
**我的XSL表**

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

我给出的另一个答案仅适用于XSLT 2.0,如XSLT 1.0“use属性或match属性的值包含变量引用是错误的”。(见附件)

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


XSL对输入XML毫无意义。不要试图打断它,并认为我们可以理解,例如:您的XSL有一个xpath“/PdfPrinter/Reports/Header/GroupingColumn1”,它在XML中的任何地方都不存在,因此它不会返回任何内容,无法用竞争信息诊断您的问题。@KevinBrown:我正在基于XML中的GroupingColumn1值实现级别1分组,我应该能够将GroupingColumn1值传递给XSL,如上面的代码段所示。我认为这只是变量赋值,如果需要,我将发布完整的XSLT。请告知。谢谢。非常感谢您的帮助。非常感谢。。稍微修改了代码,得到了所需的输出。。非常感谢您帮助我……我真诚的道歉,上面的XSLT工作得非常好。根据我的要求进行测试和修改。非常感谢。