删除重复的 xml 元素并按唯一元素对嵌套元素进行分组

Remove duplicate xml elements and group nested elements by unique one

我是一个非常新的 xslt 开发人员,提前感谢您的理解。我有以下 xml 文档,其中包括期刊及其订阅者(订阅者名称在属性“标识符”中):

<?xml version="1.0" encoding="UTF-8"?>
<journals xmlns:xlink="http://www.w3.org/1999/xlink"
    <journal name="00002030">
       <target identifier="aries" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
    </journal>
    <journal name="00000372">
       <target identifier="aries" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
    </journal>
    <journal name="00002030">
       <target identifier="cas" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
    </journal>
    <journal name="00000372">
       <target identifier="cas" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
                  xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
           </target>
        </journal>
    </journals>

我想创建具有独特期刊元素的文档,以及在它们下面的订阅者元素。所需文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<journals xmlns:xlink="http://www.w3.org/1999/xlink"
    <journal name="00002030">
       <target identifier="aries" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
       <target identifier="cas" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
    </journal>
    <journal name="00000372">
       <target identifier="aries" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
       <target identifier="cas" pubdate="01/01/2004">
          <targetLink xmlns:xlink="http://www.w3.org/1999/xlink"
              xlink:href="targets.xml#xpointer(/descendant::subscriber[attribute::name='$subscriberNameFromDoc'])"/>
       </target>
    </journal>  
</journals>

你能建议怎么做吗?预先感谢您的帮助。

你可以这样做。

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

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="journals"/>
  </xsl:template>

  <xsl:template match="journals">
    <xsl:copy>
     <xsl:for-each-group select="journal" group-by="@name">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:copy-of select="current-group()/target"/>
        </xsl:copy>
     </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>

看到它在这里工作:https://xsltfiddle.liberty-development.net/jxDjinb