0

我想按父元素对节点进行分组。

这里的父元素是: FDDCell id="AAA" method="modify"

  • 父元素重复两次。

  • 我希望“FDDCell id”只出现一次。并将所有节点分组在“FDDCell id”下

这是 XML 输入文件:

<?xml version="1.0" encoding="UTF-8"?>
<start>

<FDDCell id="AAA"  method="modify">
<UMTSFddNeighbouringCell id="FAR_AWAY" method="create">
    <attributes>
        <mbmsNeighbouringWeight>0.0</mbmsNeighbouringWeight>
    </attributes>
</UMTSFddNeighbouringCell>
</FDDCell>

<FDDCell id="AAA" method="modify">
<attributes>
    <cacConfId>RadioAccessService/0 DedicatedConf/0 CacConfClass/10</cacConfId>
    <layerPreferredForR99>true</layerPreferredForR99>
    <reserved0>1398341632</reserved0>
    <reserved1>1398352896</reserved1>
    <reserved2>1616994144</reserved2>
    <reserved3>1616994144</reserved3>
</attributes>
</FDDCell>

</start>

这是所需的输出文件:

 <?xml version="1.0" encoding="UTF-8"?>
 <start>

 <FDDCell id="AAA" method="modify">
  <UMTSFddNeighbouringCell id="FAR_AWAY" method="create">
    <attributes>
        <mbmsNeighbouringWeight>0.0</mbmsNeighbouringWeight>
    </attributes>
 </UMTSFddNeighbouringCell>

<attributes>
    <cacConfId>RadioAccessService/0 DedicatedConf/0 CacConfClass/10</cacConfId>
    <layerPreferredForR99>true</layerPreferredForR99>
    <reserved0>1398341632</reserved0>
    <reserved1>1398352896</reserved1>
    <reserved2>1616994144</reserved2>
    <reserved3>1616994144</reserved3>
</attributes>

</FDDCell>
</start>

十分感谢您的支持

4

1 回答 1

1

这是一个 XSLT 2.0 样式表:

<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="2.0">

  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="start">
    <xsl:copy>
      <xsl:for-each-group select="FDDCell" group-by="@id">
        <xsl:copy>
          <xsl:apply-templates select="@*, current-group()/node()"/>
        </xsl:copy>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

您可以使用 Saxon 9、AltovaXML 工具、XMLPrime 等 XSLT 2.0 处理器来运行它。

于 2012-05-27T09:35:45.733 回答