基于 XSLT 1.0 中的公共属性合并元素集 [英] Merging set of elements based on a common attribute in XSLT 1.0

查看:27
本文介绍了基于 XSLT 1.0 中的公共属性合并元素集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XSLT1.0 .我的要求是基于公共属性合并一组元素.我有一个看起来像这样的 xml:

I m working with XSLT1.0 . My requirement is to merge set of elements based on a common attribute. I ve an xml which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Catalog>
    <product>
        <productId>S100</productId>
        <name>RNKC</name>
        <category>books</category>
    </product>
    <product>
        <productId>S100</productId>
        <name>RNKC</name>
        <category>CD</category>
    </product>
    <product>
        <productId>S200</productId>
        <name>ISDR</name>
        <category>eBook</category>
    </product>
    <product>
        <productId>S200</productId>
        <name>ISDR</name>
        <category>books</category>
    </product>
</Catalog>

我想要输出的 XML 如下

I want the output XML as below

<?xml version="1.0" encoding="utf-8"?>
<Catalog>
    <product>
        <productId>S100</productId>
        <name>RNKC</name>
        <category>books,CD</category>
    </product>
    <product>
        <productId>S200</productId>
        <name>RNKC</name>
        <category>eBook,books</category>
    </product>
</Catalog>

已尝试使用但无法获得正确的输出.请让我知道这种转换是否可能以及如何完成.感谢您的帮助!

Have tried using but couldnt achieve the correct output. Pls let me know if this kind of transformation is possible and how it can be done. Appreciate your help!

推荐答案

如链接到的另一个主题半位所示,您需要使用一个键来 (1) 选择不同的产品(又名 Muenchian 方法)和(2) 从相关组中收集值.

As shown in the other topic halfbit linked to, you need to use a key in order to (1) select distinct products (aka the Muenchian method) and (2) collect the values from the related group.

这是一个更具可读性的(恕我直言)版本:

Here's a little more readable (IMHO) version:

<?xml version="1.0" encoding="UTF-8"?>
<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="sameProduct" match="product" use="productId" />

<xsl:template match="/">

<!-- SELECT ONLY THE FIRST PRODUCT IN EACH GROUP -->
<xsl:for-each select="Catalog/product[generate-id() = generate-id(key('sameProduct', productId)[1])]">
<product>
    <productId><xsl:value-of select="productId"/></productId>
    <name><xsl:value-of select="name"/></name>
    <category>
        <!-- GET THE VALUES FROM ALL MEMBERS OF THE GROUP -->
        <xsl:for-each select="key('sameProduct', productId)">
            <xsl:value-of select="category"/>
            <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </category>
</product>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

如果您的处理器支持 EXSLT,您可以使用 set:distinct() 函数代替 Muenchian 分组.

If your processor is EXSLT capable, you can use the set:distinct() function instead of the Muenchian grouping.

这篇关于基于 XSLT 1.0 中的公共属性合并元素集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆