代码之家  ›  专栏  ›  技术社区  ›  Lukas Hrd

使用csl从xml中获取相等的产品Id(在行csv中)并在一列中求和大小

  •  0
  • Lukas Hrd  · 技术社区  · 6 年前

    我有一个xml文件,必须将其解析为csv。在这种情况下,我希望删除所有double parent\u product\u id,并在一列中求和大小和库存数量。遗憾的是,我无法找到一个解决方案,即如何在parent\u product\u id相等时将其分组,以及如何将大小和库存数量相加。谢谢你给我的一切帮助。

    <?xml version="1.0" encoding="UTF-8" ?>
      <products> 
       <product>
     <parent_product_id>RMA021-05J</parent_product_id>
            <brand_name>Regatta</brand_name>
            <colour>blauw</colour>
            <currency>EUR</currency>
            <rrp_price>29.95 EUR</rrp_price>
            <size>XXL</size>
            <stock_quantity>2</stock_quantity>
        </product> 
      <product>
     <parent_product_id>RMA021-05J</parent_product_id>
            <brand_name>Regatta</brand_name>
            <colour>blauw</colour>
            <currency>EUR</currency>
            <rrp_price>29.95 EUR</rrp_price>
            <size>XL</size>
            <stock_quantity>4</stock_quantity>
        </product>
      </products>

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="text" omit-xml-declaration="yes" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="products">
    <xsl:for-each select="product">
    <xsl:text>"</xsl:text>
     <xsl:value-of select="normalize-space(parent_product_id)"/><xsl:text>"|"</xsl:text>
     <xsl:value-of select="normalize-space(brand_name)"/><xsl:text>"|"</xsl:text>
     <xsl:value-of select="normalize-space(colour)"/><xsl:text>"|"</xsl:text>
     <xsl:value-of select="normalize-space(currency)"/><xsl:text>"|"</xsl:text>
     <xsl:value-of select="normalize-space(rrp_price)"/><xsl:text>"|"</xsl:text>
      <xsl:value-of select="normalize-space(size)"/><xsl:text>"|"</xsl:text>
     <xsl:value-of select="normalize-space(stock_quantity)"/><xsl:text>"|"</xsl:text>
    <xsl:text>"
    </xsl:text>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
    这就是我现在的结果:
    "RMA021-05J"|"Regatta"|"blauw"|"EUR"|"29.95 EUR"|"XXL"|"2"|""
    "RMA021-05J"|"Regatta"|"blauw"|"EUR"|"29.95 EUR"|"XL"|"4"|""

    我需要的结果如下所示:

    "RMA021-05J"|"Regatta"|"blauw"|"EUR"|"29.95 EUR"|"XXL,XL"|"6"|""
    1 回复  |  直到 6 年前
        1
  •  0
  •   Rupesh_Kr    6 年前

    尝试以下操作:

    <xsl:template match="products">
        <xsl:for-each-group select="product" group-by="parent_product_id">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="parent_product_id"/><xsl:text>"|"</xsl:text>
            <xsl:value-of select="brand_name"/><xsl:text>"|"</xsl:text>
            <xsl:value-of select="colour"/><xsl:text>"|"</xsl:text>
            <xsl:value-of select="currency"/><xsl:text>"|"</xsl:text>
            <xsl:value-of select="rrp_price"/><xsl:text>"|"</xsl:text>
            <xsl:value-of select="distinct-values(current-group()/size)" separator=","/><xsl:text>"|"</xsl:text>
            <xsl:value-of select="sum(current-group()/stock_quantity)"/><xsl:text>"|"</xsl:text>
            <xsl:text>"&#xa;</xsl:text>
        </xsl:for-each-group>
    </xsl:template>
    

    您可以在以下位置看到转换: https://xsltfiddle.liberty-development.net/94hvTyS