Xslt 在节点和属性上使用GROUPBY将xml转换为xml

Xslt 在节点和属性上使用GROUPBY将xml转换为xml,xslt,Xslt,我需要关于使用xslt将xml转换为另一种xml格式的帮助。 尝试使用下面的.xsl文件,但不起作用,感谢帮助 <?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" indent ="yes"/> <

我需要关于使用xslt将xml转换为另一种xml格式的帮助。 尝试使用下面的.xsl文件,但不起作用,感谢帮助

 <?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" indent ="yes"/>
  <xsl:template match="Mappings/Source/MapToType/MapTo/*">
  <xsl:copy>
      <xsl:for-each-group select="Source" group-by="name">
          <xsl:apply-templates select="." />
      </xsl:for-each-group>
  </xsl:copy>
  </xsl:template>

    <xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
   </xsl:template>
   </xsl:stylesheet>

下面是源XML

  <?xml version='1.0' encoding='UTF-8'?>
   <Mappings version="1.0">
   <Source name="testA" type="testType" reg="Good">
    <MapToType type="Tiger">
        <MapTo name="Org" reg="Ver1.0"/>
    </MapToType>
   </Source>
  <Source name="testB" type="testType" reg="Good">
    <MapToType type="Tiger">
        <MapTo name="Org2" reg="Ver2.0"/>
    </MapToType>
  </Source>
  <Source name="testA" type="testType" reg="Good">
    <MapToType type="Tiger">
        <MapTo name="Org3" reg="Ver3.0"/>
    </MapToType>
   </Source>
</Mappings>

输出如下所示

 <Mappings version="1.0">
 <Source name="testA" type="testType" reg="Good">
    <MapToType type="Tiger">
        <MapTo name="Org" reg="Ver1.0"/>
        <MapTo name="Org3" reg="Ver3.0"/>
    </MapToType>
  </Source>
  <Source name="testB" type="testType" reg="Good">
    <MapToType type="Tiger">
       <MapTo name="Org2" reg="Ver2.0"/>
    </MapToType>
  </Source>
  </Mappings>


任何人都可以解决如何编写条件以显示为output.xml的问题?

首先注意,
xsl:for each group
是一个XSLT 2.0命令,因此您应该确保使用的处理器能够处理XSLT 2.0

无论如何,它不起作用的一个原因是,当您执行
操作时,您所处的模板与
映射到的模板相匹配。由于选择将与此
MapTo
元素相关,因此您将查找名为
Source
的子元素,其中没有子元素

(编辑:或者,正如Martin Honnen在评论中正确指出的那样,模板实际上匹配
MapTo
的子元素,而不是
MapTo
本身,因此不会匹配任何内容,因此永远不会使用……)

您应该真正更改模板以匹配
映射

<xsl:template match="Mappings">
  <xsl:copy>
    <xsl:apply-templates select="@*" />
     <xsl:for-each-group select="Source" group-by="@name">
或者,如果
映射类型
对于每个分组的
元素始终具有相同的
类型
,则可以将XSLT简化为此

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

  <xsl:template match="Mappings">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:for-each-group select="Source" group-by="@name">
        <xsl:copy>
          <xsl:apply-templates select="@*" />
          <MapToType>
            <xsl:apply-templates select="MapToType/@*" />
            <xsl:apply-templates select="current-group()/MapToType/MapTo" />
          </MapToType>
        </xsl:copy>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

编辑:如果您只使用XSLT1.0,那么您需要使用一种称为Muenchian分组的技术。如果您是XSLT新手,这将是一个巨大的挑战,尤其是在这种情况下,如果您有嵌套分组,需要考虑父级

请尝试以下XSLT:

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

  <xsl:key name="Sources" match="Source" use="@name" />
  <xsl:key name="MapToTypes" match="MapToType" use="concat(../@name, @type)" />

  <xsl:template match="Mappings">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:for-each select="Source[generate-id() = generate-id(key('Sources', @name)[1])]">
        <xsl:copy>
          <xsl:apply-templates select="@*" />
          <xsl:for-each select="key('Sources', @name)/MapToType[generate-id() = generate-id(key('MapToTypes', concat(../@name, @type))[1])]">
            <xsl:copy>
             <xsl:apply-templates select="@*" />
             <xsl:apply-templates select="key('MapToTypes', concat(../@name, @type))/MapTo" />
            </xsl:copy>
          </xsl:for-each>
        </xsl:copy>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

看看它在哪里起作用


请在上阅读慕尼黑分组。然后,再读一遍。再一次….

首先注意,
xsl:for each group
是一个XSLT 2.0命令,因此您应该确保您使用的处理器能够处理XSLT 2.0

无论如何,它不起作用的一个原因是,当您执行
操作时,您所处的模板与
映射到的模板相匹配。由于选择将与此
MapTo
元素相关,因此您将查找名为
Source
的子元素,其中没有子元素

(编辑:或者,正如Martin Honnen在评论中正确指出的那样,模板实际上匹配
MapTo
的子元素,而不是
MapTo
本身,因此不会匹配任何内容,因此永远不会使用……)

您应该真正更改模板以匹配
映射

<xsl:template match="Mappings">
  <xsl:copy>
    <xsl:apply-templates select="@*" />
     <xsl:for-each-group select="Source" group-by="@name">
或者,如果
映射类型
对于每个分组的
元素始终具有相同的
类型
,则可以将XSLT简化为此

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

  <xsl:template match="Mappings">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:for-each-group select="Source" group-by="@name">
        <xsl:copy>
          <xsl:apply-templates select="@*" />
          <MapToType>
            <xsl:apply-templates select="MapToType/@*" />
            <xsl:apply-templates select="current-group()/MapToType/MapTo" />
          </MapToType>
        </xsl:copy>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

编辑:如果您只使用XSLT1.0,那么您需要使用一种称为Muenchian分组的技术。如果您是XSLT新手,这将是一个巨大的挑战,尤其是在这种情况下,如果您有嵌套分组,需要考虑父级

请尝试以下XSLT:

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

  <xsl:key name="Sources" match="Source" use="@name" />
  <xsl:key name="MapToTypes" match="MapToType" use="concat(../@name, @type)" />

  <xsl:template match="Mappings">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:for-each select="Source[generate-id() = generate-id(key('Sources', @name)[1])]">
        <xsl:copy>
          <xsl:apply-templates select="@*" />
          <xsl:for-each select="key('Sources', @name)/MapToType[generate-id() = generate-id(key('MapToTypes', concat(../@name, @type))[1])]">
            <xsl:copy>
             <xsl:apply-templates select="@*" />
             <xsl:apply-templates select="key('MapToTypes', concat(../@name, @type))/MapTo" />
            </xsl:copy>
          </xsl:for-each>
        </xsl:copy>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

看看它在哪里起作用


请在上阅读慕尼黑分组。然后,再读一遍。再一次……

“不工作”。。。也许可以和一位同事分享一下您的意思?因为您没有说过任何关于XSLT版本的内容,而且您在一个名为version=“1.0”的样式表中使用了XSLT 2.0构造,所以我猜您还没有弄清楚您使用的是1.0还是2.0处理器。现在,如果你不是说你的代码“不工作”,而是告诉我们它失败的方式,那么我们可能会立即确认或排除这种怀疑。“不工作”。。。也许可以和一位同事分享一下您的意思?因为您没有说过任何关于XSLT版本的内容,而且您在一个名为version=“1.0”的样式表中使用了XSLT 2.0构造,所以我猜您还没有弄清楚您使用的是1.0还是2.0处理器。现在,如果您没有说您的代码“不工作”,而是告诉了我们它失败的方式,我们可能会立即确认或排除这种怀疑。原始模板有
match=“Mappings/Source/MapToType/MapTo/*”
因此它匹配
MapTo
的任何子元素,而这些子元素在输入中根本不存在。否则你的回答当然可以。非常感谢你的快速回复。我对xslt非常陌生。我尝试了上述两种代码,但出现错误时找不到函数:当前组。我使用的xslt版本1.0 xalan 2.7仅支持xslt 1.0版本,请您帮助使用版本1.0。我已修改了答案以显示xslt 1.0解决方案。原始模板具有
match=“Mappings/Source/MapToType/MapTo/*”
因此它匹配
MapTo
的任何子元素,而这些子元素在输入中根本不存在。否则你的回答当然可以。非常感谢你的快速回复。我对xslt非常陌生。我尝试了上述两种代码,但出现错误时找不到函数:当前组。我使用的是xslt 1.0版xalan 2.7仅支持xslt 1.0版。您可以在版本1.0中获得帮助吗?我已修改了答案,以显示xslt 1.0解决方案。