方法(XSLT)为每个父元素分组节点

时间:2012-05-25 17:53:53

标签: xml xslt xslt-1.0 xslt-2.0 nodes

我想按父元素分组节点。

此处父元素为: 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>

非常感谢您的支持

1 个答案:

答案 0 :(得分: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处理器运行它。