0

场景是这样的:一个图书馆有五本书。作者 A 写了一本书,图书馆拥有两本已签出 30+14=44 次 作者 B 写了两本书,图书馆拥有两本已签出 B 的本 18+9=27次和一份已签出 41 次的标题 C。

我的 XML 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="popauthors.xsl"?>
<report>
  <catalog>
    <marc>
      <marcEntry tag="100" label="Personal Author" ind="1 ">Author A</marcEntry>
      <marcEntry tag="245" label="Title" ind="10">Title A</marcEntry>
    </marc>
  <call>
    <item>
      <totalCharges>30</totalCharges>
        <itemID>1234</itemID>
    </item>
    <item>
      <totalCharges>14</totalCharges>
      <itemID>2345</itemID>
    </item>
  </call>
</catalog>
<catalog>
  <marc>
    <marcEntry tag="100" label="Personal Author" ind="1 ">Author B</marcEntry>
    <marcEntry tag="245" label="Title" ind="10">Title B</marcEntry>
  </marc>
  <call>
    <item>
      <totalCharges>18</totalCharges>
      <itemID>3456</itemID>
    </item>
    <item>
      <totalCharges>9</totalCharges>
      <itemID>4567</itemID>
    </item>
  </call>
</catalog>
<catalog>
  <marc>
    <marcEntry tag="100" label="Personal Author" ind="1 ">Author B</marcEntry>
    <marcEntry tag="245" label="Title" ind="10">Title C</marcEntry>
  </marc>
  <call>
    <item>
      <totalCharges>41</totalCharges>
      <itemID>5678</itemID>
    </item>
  </call>
</catalog>
</report>

我尝试了 Muenchian 分组——它为作者 A 提供了正确的数字,但对于作者 B,它只计算了第一个标题的项目和费用,两个项目有 27 个收费,而不是正确的数字 3 个项目有 68 个收费。我应该添加什么来计算拥有多个标题的作者的所有费用?

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="popauthor" match="marc" use="marcEntry[@tag='100']"/>

<xsl:template match="/">
    <popauthors>
        <xsl:for-each select="//marc[generate-id(.)=generate-id(key('popauthor', marcEntry[@tag='100'])[1])]">
            <xsl:sort select="marcEntry"/> 

                <popauthorline>
                    <authorName><xsl:value-of select="marcEntry[@tag='100']"/></authorName>
                    <numberOfTitles><xsl:value-of select="count(key('popauthor', marcEntry[@tag='100']))"/></numberOfTitles>
                    <numberOfItems><xsl:value-of select="count(../call/item/itemID)"/></numberOfItems>
                    <TotalCharges><xsl:value-of select="sum(../call/item/totalCharges)"/></TotalCharges>
                </popauthorline>

        </xsl:for-each>
    </popauthors>
</xsl:template>

</xsl:stylesheet>
4

1 回答 1

1

在我看来,您想对catalog条目进行分组,而不是对它们的marc子项进行分组。然后一切都变得简单:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="cat-by-auth" match="catalog" use="marc/marcEntry[@tag='100']"/>

<xsl:template match="/report">
    <popauthors>
        <xsl:for-each select="catalog[generate-id(.)=generate-id(key('cat-by-auth', marc/marcEntry[@tag='100'])[1])]">
            <xsl:sort select="marc/marcEntry[@tag='100']"/> 
            <xsl:variable name="titles" select="key('cat-by-auth', marc/marcEntry[@tag='100'])" />
                <popauthorline>
                    <authorName><xsl:value-of select="marc/marcEntry[@tag='100']"/></authorName>
                    <numberOfTitles><xsl:value-of select="count($titles)"/></numberOfTitles>
                    <numberOfItems><xsl:value-of select="count($titles/call/item)"/></numberOfItems>
                    <TotalCharges><xsl:value-of select="sum($titles/call/item/totalCharges)"/></TotalCharges>
                </popauthorline>
        </xsl:for-each>
    </popauthors>
</xsl:template>

</xsl:stylesheet>
于 2016-07-18T16:11:00.100 回答