使用包含xsl:key的函数

时间:2017-02-08 02:56:36

标签: xml xslt

我是XSLT 1.0的新手,所以请原谅我糟糕的编码技巧。我正在尝试计算包含像xsl:key中的'fee'这样的字符串的记录。虽然这有效xsl:value-of select="count(//Comments[contains(., 'Fee')])",但它会将计数应用于所有不同的记录。如何计算包含特定于ReferralDesc元素的字符串的记录。

输出(当前)

Referral    Count   Days    Days    CntInfo
Architect   9   794     88.2        1
Building    26  1784    68.6        1
Trees   6   548     91.3        1

XML

<Referrals>
    <Referral>
        <ApplicationId>51240</ApplicationId>
        <Proposal>Demolition work</Proposal>
        <ReferralDesc>Architect</ReferralDesc>
        <Lapsed>49</Lapsed>
        <Officer>Mark What</Officer>
        <Comments>Information provided</Comments>
    </Referral>
    <Referral>
        <ApplicationId>51304</ApplicationId>
        <Proposal>Alterations and Additions</Proposal>
        <ReferralDesc>Tree</ReferralDesc>
        <Lapsed>28</Lapsed>
        <Officer>Mark Some</Officer>
        <Comments>Fee quoted</Comments>
    </Referral>
</Referrals>

XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="groups" match="Referral" use="ReferralDesc" />
<xsl:key name="info" match="Referral" use="normalize-space(Comments)" />
<xsl:key name="days" match="Referral" use="Lapsed" />

<xsl:template match="NewDataSet">

   <table style="width:55%; font-size:10px;">
     <thead>
      <th style="width:25%" class="borders">Referral</th>
      <th style="width:10%" class="borders">Count</th>
      <th style="width:10%" class="borders">Days total</th>
      <th style="width:10%" class="borders">Days avg</th>
      <th style="width:10%" class="borders">CntInfo</th>
     </thead>

    <xsl:for-each select="Referrals/Referral[count(. | key('groups', ReferralDesc)[1]) = 1]">
    <xsl:sort select="ReferralDesc" />
      <tr>
        <td class="borders">
          <xsl:value-of select="ReferralDesc" />
        </td>
        <td class="borders">
          <xsl:value-of select="count(. | key('groups', ReferralDesc))"/>
        </td>
        <td class="borders">
          <!-- <xsl:value-of select="sum(Lapsed)"/> -->
          <xsl:value-of select="sum(key('groups',ReferralDesc)/Lapsed)"/>
        </td>
        <td class="borders">
          <xsl:variable name="nRef">
            <xsl:value-of select="count(. | key('groups', ReferralDesc))"/>
          </xsl:variable>
          <xsl:variable name="sDay">
            <xsl:value-of select="sum(key('groups',ReferralDesc)/Lapsed)"/>
          </xsl:variable>
          <xsl:value-of select="format-number($sDay div $nRef, '###,###.0')"/>
        </td>
        <td class="borders">
          <xsl:value-of select="count(//Comments[contains(., 'fee')])" /> 
        </td>
      </tr> 
    </xsl:for-each>

    </table>
</xsl:template>
</xsl:stlyesheet>

ANSWER

看起来坚持不懈是值得的。更新密钥()。 used xsl:key name =“info”match =“Referral [contains(./ Comments,'fee')]”use =“ReferralDesc”

1 个答案:

答案 0 :(得分:0)

你想要的表达是......

<xsl:value-of select="count(key('groups', ReferralDesc)/Comments[contains(., 'fee')])" /> 

顺便说一句,这个表达式在你的XSLT ....

<xsl:value-of select="count(. | key('groups', ReferralDesc))"/>

可以略微简化为此(因为您已经知道当前节点是当前键的一部分)

<xsl:value-of select="count(key('groups', ReferralDesc))"/>