XSL Muenchian 分组有效,但我如何计算键的所有 child 节点的内容?
XSL Muenchian grouping works, but how can I count content of all child nodes of the key?
场景是这样的:
一个图书馆有五本书。
作者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>
在我看来,您想要对 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>
场景是这样的: 一个图书馆有五本书。 作者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>
在我看来,您想要对 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>