0

我有一个生成交易文件的 XML,它记录了会员卡中用于赚取积分的金额和完成日期的条目。没有我可以使用的顺序,因为相同的卡号可能会出现在 XML 中。我想要的是让 XSL 找到一个卡号(我没有用作 ID 的记录),把它放在 a 中<td></td>并显示总共花了多少钱,赚了多少积分以及什么完成日期。

这是XML...

<Root>
  <Events>
    <TicketEnd Date="2012-10-21" />
  </Events>
  <Ticket>
    <TicketStart Date="2012-10-22" />
    <TicketEnd Date="2012-10-22" />
  </Ticket>
  <Events>
  </Events>
  <Ticket>
    <TicketStart Date="2012-10-22" />
    <Clubcard Opcode="96" Function="27" CardAcc="1" QualSpendInfo="0" SchemeNo="40" CardNo="1042540000026" PointsToDate="0" RedemptionValue="0" CustomerUpdateDate="000000" QualSpent="0" Date="2012-10-22" />
    <ClubcardPoints Opcode="96" Function="28" SchemeNo="40" PromNo="0" CardNo="1042540000026" QualSpend="30005" PointSpend="0" Points="6" BonusPoints="0" PromCount="0" Date="2012-10-22" />
    <Clubcard Opcode="96" Function="27" CardAcc="1" QualSpendInfo="1" SchemeNo="40" CardNo="1042540000026" PointsToDate="0" RedemptionValue="0" CustomerUpdateDate="000000" QualSpent="30005" Date="2012-10-22" />
    <TicketEnd Date="2012-10-22" />
  </Ticket>
  <Ticket>
    <TicketStart Date="2012-10-22" />
    <Clubcard Opcode="96" Function="27" CardAcc="1" QualSpendInfo="0" SchemeNo="40" CardNo="1042540000019" PointsToDate="0" RedemptionValue="0" CustomerUpdateDate="000000" QualSpent="0" Date="2012-10-22" />
    <ClubcardPoints Opcode="96" Function="28" SchemeNo="40" PromNo="0" CardNo="1042540000019" QualSpend="24330" PointSpend="0" Points="4" BonusPoints="0" PromCount="0" Date="2012-10-22" />
    <Clubcard Opcode="96" Function="27" CardAcc="1" QualSpendInfo="1" SchemeNo="40" CardNo="1042540000019" PointsToDate="0" RedemptionValue="0" CustomerUpdateDate="000000" QualSpent="24330" Date="2012-10-22" />
    <TicketEnd Date="2012-10-22" />
  </Ticket>
  <Ticket>
    <TicketStart Date="2012-10-22" />
    <Clubcard Opcode="96" Function="27" CardAcc="1" QualSpendInfo="0" SchemeNo="40" CardNo="1042540000026" PointsToDate="0" RedemptionValue="0" CustomerUpdateDate="000000" QualSpent="0" Date="2012-10-22" />
    <ClubcardPoints Opcode="96" Function="28" SchemeNo="40" PromNo="0" CardNo="1042540000026" QualSpend="30005" PointSpend="0" Points="6" BonusPoints="0" PromCount="0" Date="2012-10-22" />
    <Clubcard Opcode="96" Function="27" CardAcc="1" QualSpendInfo="1" SchemeNo="40" CardNo="1042540000026" PointsToDate="0" RedemptionValue="0" CustomerUpdateDate="000000" QualSpent="30005" Date="2012-10-22" />
    <TicketEnd Date="2012-10-22" />
  </Ticket>
</Root>

我的 XSL 只能列出条目...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <html>
      <head>
    <title>Loyalty Sales</title>
      </head>
    <body>
      <br/>
      <br/>
      <br/>
       <h1 style="color:blue;
                  margin-left:20px;
                  font-family:verdana;
                  text-align:center;">
        Customers Report</h1>
      <br/>
      <p style="color:red;
                margin-left:20px;
                font-family:arial;
                text-align:right;
                font-size:15px;">
        Store Report</p>
      <p style="color:green;
                margin-left:20px;
                font-family:arial;
                text-align:right;
                font-size:15px;">
        for Customer ABC</p>
      <br/>
    <table width="100%" border="3">
      <THEAD>
      <TR bgcolor="RGB(0, 204, 51)">
      <TD width="25%">
        <font color="white"><B>Account Number</B></font>
      </TD>
      <TD width="25%">
        <font color="white"><B>Points</B></font>
      </TD>
      <TD width="25%">
        <font color="white"><B>Date</B></font>
      </TD>
      <TD width="25%">
        <font color="white"><B>Qualified Spent</B></font>
      </TD>
      </TR>
      </THEAD>
    <TBODY>
    <xsl:for-each select="Root/Ticket/ClubcardPoints">
      <TR>
      <TD width="25%"><xsl:value-of select="@CardNo" /></TD>
      <TD width="25%"><xsl:value-of select="@Points" /></TD>
      <TD width="25%"><xsl:value-of select="@Date" /></TD>
      <TD width="25%"><xsl:value-of select="format-number(@QualSpend div 100,'&#x52;#.##')" /></TD>
    </TR>
    </xsl:for-each>
    </TBODY>
    </table>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>

我已经搜索了示例,但找不到具有所有这些条件的场景,并且当放入单个 XSL 时它会失败。请帮忙。

这是我想要的输出文件...

<THEAD>
                <TR bgcolor="RGB(0, 204, 51)">
                    <TD width="25%"><font color="white"><B>Account Number</B></font></TD>
                    <TD width="25%"><font color="white"><B>Total Points</B></font></TD>
                    <TD width="25%"><font color="white"><B>Date</B></font></TD>
                    <TD width="25%"><font color="white"><B>Total Qualified Spent</B></font></TD>
                </TR>
            </THEAD>
            <TBODY>
                <TR>
                    <TD width="25%">1042540000002</TD>
                    <TD width="25%">100</TD>
                    <TD width="25%">2012-10-22</TD>
                    <TD width="25%">R750.32</TD>
                </TR>
4

1 回答 1

0

如果您想合计每张卡的积分并消费,则需要按卡号对卡进行分组。在 XSLT1.0 中,您为此使用了 Munechian Grouping。这意味着为您的组定义一个密钥

<xsl:key name="cards" match="ClubcardPoints" use="@CardNo" />

然后,要选择每个不同的卡号,您需要查找组中第一个出现的ClubcardPoints的卡号。这是按如下方式完成的

<xsl:apply-templates 
   select="Root/Ticket/ClubcardPoints[generate-id() = generate-id(key('cards', @CardNo)[1])]" />

获得总分就很简单了

<xsl:value-of select="sum(key('cards', @CardNo)/@Points)" />

这是完整的 XSLT(请注意,我已对其进行了简化以删除所有样式)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html"/>
   <xsl:key name="cards" match="ClubcardPoints" use="@CardNo"/>

   <xsl:template match="/">
      <table>
         <THEAD>
            <TR>
               <TD>Account Number</TD>
               <TD>Points</TD>
               <TD>Date</TD>
               <TD>Qualified Spent</TD>
            </TR>
         </THEAD>
         <TBODY>
            <xsl:apply-templates select="Root/Ticket/ClubcardPoints[generate-id() = generate-id(key('cards', @CardNo)[1])]"/>
         </TBODY>
      </table>
   </xsl:template>

   <xsl:template match="ClubcardPoints">
      <TR>
         <TD>
            <xsl:value-of select="@CardNo"/>
         </TD>
         <TD>
            <xsl:value-of select="sum(key('cards', @CardNo)/@Points)"/>
         </TD>
         <TD>
            <xsl:value-of select="@Date"/>
         </TD>
         <TD>
            <xsl:value-of select="format-number(sum(key('cards', @CardNo)/@QualSpend) div 100,'R#.##')"/>
         </TD>
      </TR>
   </xsl:template>
</xsl:stylesheet>

应用于您的示例 XML 时,将输出以下内容

<table>
   <THEAD>
      <TR>
         <TD>Account Number</TD>
         <TD>Points</TD>
         <TD>Date</TD>
         <TD>Qualified Spent</TD>
      </TR>
   </THEAD>
   <TBODY>
      <TR>
         <TD>1042540000026</TD>
         <TD>12</TD>
         <TD>2012-10-22</TD>
         <TD>R600.1</TD>
      </TR>
      <TR>
         <TD>1042540000019</TD>
         <TD>4</TD>
         <TD>2012-10-22</TD>
         <TD>R243.3</TD>
      </TR>
   </TBODY>
</table>

请注意,在 XSLT2.0 中,将使用xsl:for-each-group元素进行分组。

于 2012-10-29T11:22:50.903 回答