使用xslt 2.0合并具有相同父属性值的元素的xml内容

问题描述

我有两个xml文件

file1.xml

<?xml version="1.0" encoding="UTF-8"?>
<tv>
...
  <programme start="20200814040000 +0000" stop="20200814050000 +0000" channel="A">
    <title>A</title>
    <sub-title>C</sub-title>
    <desc>F</desc>
  </programme>
...
  <programme start="20200814090000 +0000" stop="20200814093000 +0000" channel="A">
    <title>B</title>
    <sub-title>D</sub-title>
    <desc>E</desc>
  </programme>
...
</tv>

file2.xml

<?xml version="1.0" encoding="UTF-8"?>
<tv>
...
  <programme start="20200814040000 +0000" stop="20200814050000 +0000" channel="A">
    <title>G</title>
    <sub-title>C</sub-title>
    <desc>H</desc>
    <episode-num system="onscreen">S9 E13</episode-num>
  </programme>
...
  <programme start="20200814090000 +0000" stop="20200814093000 +0000" channel="A">
    <title>K</title>
    <sub-title>L</sub-title>
    <desc>M</desc>
    <episode-num system="onscreen">S3 E2</episode-num>
  </programme>  
...
</tv>

我想要一个xslt 2模板来获取一个文件

file3.xml

<?xml version="1.0" encoding="UTF-8"?>
<tv>
...
  <programme start="20200814040000 +0000" stop="20200814050000 +0000" channel="A">
    <title>A (G)</title>
    <sub-title>C</sub-title>
    <desc>F (H)</desc>
    <episode-num system="onscreen">S9 E13</episode-num>
  </programme>
...
<programme start="20200814090000 +0000" stop="20200814093000 +0000" channel="A">
    <title>B (K)</title>
    <sub-title>D (L)</sub-title>
    <desc>E (M)</desc>
    <episode-num system="onscreen">S3 E2</episode-num>
  </programme>
...
</tv>

我做了一些实验,但无法获得预期的输出。任何帮助将不胜感激。

为精确而编辑

每个文件的程序属性相同时:

  1. 将两个文件中都存在的子元素合并到新文件中的一个元素上,并且 如果节点的文本内容不同,则将第二个文件内容放在括号中
  2. 如果两个文件中都不存在子元素,则将其包含在新文件

解决方法

在XSLT 3中,功能for-each-pair可以提供帮助:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
  <xsl:param name="doc2">
<tv>
  <channel id="Discovery">
    <display-name lang="el">Discovery</display-name>
  </channel>
  <programme start="20200814040000 +0000" stop="20200814050000 +0000" channel="Discovery">
    <title lang="el">Wheeler Dealers</title>
    <sub-title lang="el">BMW Isetta</sub-title>
    <desc lang="el">Mike tracks down an Isetta Bubble. </desc>
    <episode-num system="onscreen">S9 E13</episode-num>
  </programme>
</tv>
  </xsl:param>
  
  <xsl:output indent="yes"/>
  
  <xsl:function name="mf:merge-pair">
    <xsl:param name="programme1"/>
    <xsl:param name="programme2"/>
    <xsl:if test="deep-equal($programme1/@*,$programme2/@*)">
      <xsl:copy select="$programme1">
        <xsl:apply-templates select="@*"/>
        <xsl:for-each-group select="$programme1/*,$programme2/*" composite="yes" group-by="node-name(),@*">
          <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:value-of select="head(current-group()),tail(current-group()) ! ('(' || . || ')')"/>
          </xsl:copy>
        </xsl:for-each-group>
      </xsl:copy>
    </xsl:if>
  </xsl:function>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="tv">
    <xsl:copy>
      <xsl:apply-templates select="@*,channel"/>
      <xsl:sequence
         select="for-each-pair(programme,$doc2/tv/programme,mf:merge-pair#2)"/>      
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>

在上面的示例中,我内联了第二个文档,以确保完整性和自包含性,但是当然,在现实生活中,您可以使用例如<xsl:param name="doc2" select="doc('input2.xml')"/>

具有针对每个对的XSLT 3适用于Saxon 10所有版本或Saxon的商业9.8或9.9版本,或者适用于Node.js或浏览器中的Saxon-JS 2。

关于您的评论,似乎您已经编辑了示例,现在看来应该消除BMW Isetta (BMW Isetta)之类的重复内容,以便您可以更改

 <xsl:value-of select="head(current-group()),tail(current-group()) ! ('(' || . || ')')"/>

<xsl:value-of select="let $values := distinct-values(current-group()) return (head(
        $values),tail($values)! ('(' || . || ')'))"/>

将您编辑的样本和Saxon HE 10.1的输出输出给我

<tv>
   <programme start="20200814040000 +0000"
              stop="20200814050000 +0000"
              channel="A">
      <title>A (G)</title>
      <sub-title>C</sub-title>
      <desc>F (H)</desc>
      <episode-num system="onscreen">S9 E13</episode-num>
   </programme>
   <programme start="20200814090000 +0000"
              stop="20200814093000 +0000"
              channel="A">
      <title>B (K)</title>
      <sub-title>D (L)</sub-title>
      <desc>E (M)</desc>
      <episode-num system="onscreen">S3 E2</episode-num>
   </programme>
</tv>

完整的样式表是

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="#all"
    expand-text="yes">
    
    <xsl:param name="doc2" select="doc('file2.xml')"/>
    
    <xsl:output indent="yes"/>
    
    <xsl:function name="mf:merge-pair">
        <xsl:param name="programme1"/>
        <xsl:param name="programme2"/>
        <xsl:if test="deep-equal($programme1/@*,$programme2/@*)">
            <xsl:copy select="$programme1">
                <xsl:apply-templates select="@*"/>
                <xsl:for-each-group select="$programme1/*,@*">
                    <xsl:copy>
                        <xsl:apply-templates select="@*"/>
                        <xsl:value-of select="let $values := distinct-values(current-group()) return (head(
                            $values),tail($values)! ('(' || . || ')'))"/>
                    </xsl:copy>
                </xsl:for-each-group>
            </xsl:copy>
        </xsl:if>
    </xsl:function>
    
    <xsl:mode on-no-match="shallow-copy"/>
    
    <xsl:template match="tv">
        <xsl:copy>
            <xsl:apply-templates select="@*,channel"/>
            <xsl:sequence
                select="for-each-pair(programme,mf:merge-pair#2)"/>      
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>
,

我会做类似的事情:

<xsl:variable name="file1" select="doc('file1.xml')"/>
<xsl:variable name="file2" select="doc('file2.xml')"/>

<xsl:template name="xsl:initial-template">
  <tv>
    <xsl:copy-of select="$file1/tv/channel"/>
    <xsl:for-each-group select="($file1|file2)/tv/programme"
        group-by="@stop,@start,@channel" composite="yes">
      <xsl:for-each-group select="*" group-by="node-name()">
        <xsl:element name="{name()}">
          <xsl:copy-of select="current-group()/@*"/> 
          <xsl:value-of select="current-group()[1]"/>
          <xsl:for-each select="current-group()[2]">
            <xsl:value-of select="'(',.,')'"/>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each-group>
    </xsl:for-each-group>
  </tv>     
</xsl:template>

未经测试。