简体   繁体   English

请帮助使用 Muenchian 分组 XSLT 1.0 为每个目录按标题分组

[英]Please help in grouping by Title within for each catalog using Muenchian grouping XSLT 1.0

Please help with a code to group based on TITLE within its each separate CATALOG tag.请帮助在其每个单独的 CATALOG 标签中根据 TITLE 分组的代码。 I am using XSLT 1.0 version.我正在使用 XSLT 1.0 版本。

<?xml version="1.0" encoding="UTF-8"?>
<hello>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
<cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>2000</year>
  </cd>
  <cd>
    <title>Unchain my heart</title>
    <artist>Joe Cocker</artist>
    <country>USA</country>
    <company>EMI</company>
    <price>8.20</price>
    <year>1987</year>
  </cd>
  
</catalog>
<catalog>
 <cd>
    <title>Ring My Bells</title>
    <artist>Enrique</artist>
    <country>USA</country>
    <company>EMI</company>
    <price>8.20</price>
    <year>1987</year>
  </cd>
  <cd>
     <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
</catalog>
</hello>

Excepted Output异常输出

<Song>
    <Desc>From First Catalog</Desc>
    <nameofAlbum>Empire Burlesque</nameofAlbum>
    <nameofAlbum>Unchain my heart</nameofAlbum>
</Song>
<Song>
    <Desc>From Second Catalog</Desc>
    <nameofAlbum>Ring My Bells</nameofAlbum>
    <nameofAlbum>Empire Burlesque</nameofAlbum>
</Song>

Please help as the requirement is to group based on title of each CatLog only.请帮助,因为要求仅根据每个 CatLog 的标题进行分组。

I have tried using the Muenchian grouping but it was grouping with all the catalogs tags where as I require as induvial grouping between catalogs我曾尝试使用 Muenchian 分组,但它与所有目录标签分组,因为我需要在目录之间进行独立分组

Since you want to dedup within the catalog , I would use generate-id() and create a composite key with the generated ID for the catalog element and the cd/title .由于您想在catalog进行重复数据删除,我将使用generate-id()并使用为 catalog 元素和cd/title生成的 ID 创建一个复合键。

Use that composite key for the xsl:key matching on cd , then you can iterate over each /hello/catalog and then for each cd under that catalog you can use the key for Muenchian grouping:使用该复合键进行cd上的xsl:key匹配,然后您可以遍历每个/hello/catalog ,然后对于该catalog下的每个cd ,您可以使用 Muenchian 分组的键:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>
    
    <xsl:variable name="labels">
        <label>First</label>
        <label>Second</label>
    </xsl:variable>
    
    <xsl:key name="cd-by-catalog-and-title" match="cd" use="concat(generate-id(..), title)"/>
    
    <xsl:template match="/">
        <xsl:for-each select="/hello/catalog">
            <xsl:variable name="pos" select="position()"/>
            <Song>
                <Desc>From <xsl:value-of select="$labels/*[position() = $pos]"/> Catalog</Desc>
                <xsl:for-each select="cd[count(. | key('cd-by-catalog-and-title', concat(generate-id(..), title))[1]) = 1]">
                    <nameofAlbum><xsl:value-of select="title"/></nameofAlbum>
                </xsl:for-each>
            </Song>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM