使用Xslt从XML删除重复项

问题描述 投票:0回答:1

[我正在尝试帮助有问题的朋友,我已经动脑了几个小时,无法弄清楚,我们正在尝试从下面删除重复的数据值(transaction_date不是giftShop)

XML输入

<?xml version="1.0"?>
<transactions>
    <transaction>
        <transaction_date>01/11/2019</transaction_date>
        <giftShop>1</giftShop>
    </transaction>
    <transaction>
        <transaction_date>01/11/2019</transaction_date>
        <giftShop>2</giftShop>
    </transaction>
    <transaction>
        <transaction_date>01/11/2019</transaction_date>
        <giftShop>3</giftShop>
    </transaction>
    <transaction>
        <transaction_date>01/11/2019</transaction_date>
        <giftShop>2</giftShop>
    </transaction>
    <transaction>
        <transaction_date>01/11/2019</transaction_date>
        <giftShop>3</giftShop>
    </transaction>
    <transaction>
        <transaction_date>02/11/2019</transaction_date>
        <giftShop>2</giftShop>
    </transaction>
    <transaction>
        <transaction_date>03/11/2019</transaction_date>
        <giftShop>3</giftShop>
    </transaction>
    <transaction>
        <transaction_date>01/11/2019</transaction_date>
        <giftShop>1</giftShop>
    </transaction>
    <transaction>
        <transaction_date>02/11/2019</transaction_date>
        <giftShop>3</giftShop>
    </transaction>
</transactions>

Xslt输入

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="transactions">
  <h2>Transactions</h2>
      <xsl:for-each select="//transaction/giftShop[not(.=preceding::*)]">
      <tr>
        <xsl:element name="shop">
                    <xsl:attribute name="ID">
                        <xsl:value-of select="."/>
                    </xsl:attribute>
                    <xsl:variable name="currentShop">
                        <xsl:value-of select="."/>
                    </xsl:variable>
          <xsl:for-each select="//transaction[giftShop=$currentShop]/transaction_date[not(..=preceding::transaction_date)]">
                        <xsl:element name="Day">
                            <xsl:attribute name="Date">
                                <xsl:value-of select="."/>                      
                            </xsl:attribute>
                        </xsl:element>
                    </xsl:for-each>
                </xsl:element>
      </tr>
      </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

期望的输出

<h2>Transactions</h2>
<tr>
   <shop ID="1">
      <Day Date="01/11/2019"/>
   </shop>
</tr>
<tr>
   <shop ID="2">
      <Day Date="01/11/2019"/>
      <Day Date="02/11/2019"/>
   </shop>
</tr>
<tr>
   <shop ID="3">
      <Day Date="01/11/2019"/>
      <Day Date="03/11/2019"/>
      <Day Date="02/11/2019"/>
   </shop>
</tr>

自动输出

<h2>Transactions</h2>
<tr>
   <shop ID="1">
      <Day Date="01/11/2019"/>
      <Day Date="01/11/2019"/>
   </shop>
</tr>
<tr>
   <shop ID="2">
      <Day Date="01/11/2019"/>
      <Day Date="01/11/2019"/>
      <Day Date="02/11/2019"/>
      <Day Date="02/11/2019"/>
   </shop>
</tr>
<tr>
   <shop ID="3">
      <Day Date="01/11/2019"/>
      <Day Date="01/11/2019"/>
      <Day Date="03/11/2019"/>
      <Day Date="02/11/2019"/>
   </shop>
</tr>

任何帮助都会令我感激,因为它使我发疯。

xml xslt
1个回答
0
投票

我建议您按照以下方法尝试:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/transactions">
    <root>
        <xsl:for-each-group select="transaction" group-by="giftShop"> 
            <shop ID="{current-grouping-key()}">
                <xsl:for-each select="distinct-values(current-group()/transaction_date)">
                    <Day Date="{.}"/>                    
                </xsl:for-each>
            </shop>
        </xsl:for-each-group>
    </root>
</xsl:template>

</xsl:stylesheet>

演示https://xsltfiddle.liberty-development.net/pNmC4HN

© www.soinside.com 2019 - 2024. All rights reserved.