XSL Muenchian分组工作,但我如何计算密钥的所有子节点的内容?

时间:2016-07-18 15:47:44

标签: xml xslt muenchian-grouping marc

情景是这样的: 图书馆有五本书。 作者A写了一个标题,图书馆拥有两个副本,已被删除30 + 14 = 44次 作者B写了两本书,图书馆拥有两本已经签出18 + 9 = 27次的标题B和一份已经签出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>

1 个答案:

答案 0 :(得分: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>