将子节点分组为4个组

时间:2012-06-04 15:26:04

标签: xml xslt

我有以下XSLT,我正在使用身份转换,因为我需要保持xml不变,只更改XML的特定部分,即:<committee committeetype="Associate">

   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>

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

      <xsl:template match="committee[@committeetype='Associate']/affiliation">
            <Committee CommitteeType="Associate"> 
                <xsl:copy>  
                    <xsl:apply-templates select="node()|@*"/>   
                </xsl:copy>  
           </Committee >
      </xsl:template>

  <xsl:template match="committee[@committeeType='Associate']">
        <xsl:apply-templates/>
  </xsl:template>

</xsl:stylesheet>

变换

<committee committeetype="Associate">
    <affiliation dbid="11">
        <name>Eve </name>
    </affiliation>
    <affiliation dbid="12">
        <name>Dan </name>
    </affiliation>
    <affiliation dbid="13">
        <name>Harold </name>
    </affiliation>
    <affiliation dbid="14">
        <name>Chris </name>
    </affiliation>      
    <affiliation dbid="25">
        <name>Malcolm </name>
    </affiliation>
    <affiliation dbid="15">
        <name>Mike </name>
    </affiliation>
</committee>

            <committee committeetype="Associate">
                <affiliation dbid="11">
                    <name>Eve </name>
                </affiliation>
            </committee>
            <committee committeetype="Associate">
                <affiliation dbid="12">
                    <name>Dan </name>
                </affiliation>
            </committee>
            <committee committeetype="Associate">
                <affiliation dbid="13">
                    <name>Harold </name>
                </affiliation>
            </committee>
            <committee committeetype="Associate">
                <affiliation dbid="14">
                    <name>Chris </name>
                </affiliation>
            </committee>
            <committee committeetype="Associate">
                <affiliation dbid="25">
                    <name>Malcolm </name>
                </affiliation>
            </committee>
            <committee committeetype="Associate">
                <affiliation dbid="15">
                    <name>Mike </name>
                </affiliation>
            </committee>
  

我怎样才能这样做,所以它将联盟分为4 <committee committeetype="Associate">

  <committee committeetype="Associate">
                <affiliation dbid="11">
                    <name>Eve </name>
                </affiliation>
                <affiliation dbid="12">
                    <name>Dan </name>
                </affiliation>
                <affiliation dbid="13">
                    <name>Harold </name>
                </affiliation>
                <affiliation dbid="14">
                    <name>Chris </name>
                </affiliation>
   </committee>
   <committee committeetype="Associate">
                <affiliation dbid="25">
                    <name>Malcolm </name>
                </affiliation>
               <affiliation dbid="15">
                    <name>Mike </name>
                </affiliation>
   </committeemembergroup>

提前致谢。

1 个答案:

答案 0 :(得分:1)

这是一个可能足够的建议:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:param name="chunk-size" select="4"/>

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

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

  <xsl:template match="committee[@committeetype='Associate']">
    <xsl:apply-templates select="affiliation[position() mod $chunk-size = 1]" mode="group"/>
  </xsl:template>

  <xsl:template match="committee[@committeetype='Associate']/affiliation" mode="group">
    <committee committeetype="Associate">
      <xsl:apply-templates select=". | following-sibling::affiliation[position() &lt; $chunk-size]"/>
    </committee>
  </xsl:template>


</xsl:stylesheet>

它转换

<committee committeetype="Associate">
    <affiliation dbid="11">
        <name>Eve </name>
    </affiliation>
    <affiliation dbid="12">
        <name>Dan </name>
    </affiliation>
    <affiliation dbid="13">
        <name>Harold </name>
    </affiliation>
    <affiliation dbid="14">
        <name>Chris </name>
    </affiliation>      
    <affiliation dbid="25">
        <name>Malcolm </name>
    </affiliation>
    <affiliation dbid="15">
        <name>Mike </name>
    </affiliation>
</committee>

<committee committeetype="Associate">
   <affiliation dbid="11">
      <name>Eve </name>
   </affiliation>
   <affiliation dbid="12">
      <name>Dan </name>
   </affiliation>
   <affiliation dbid="13">
      <name>Harold </name>
   </affiliation>
   <affiliation dbid="14">
      <name>Chris </name>
   </affiliation>
</committee>
<committee committeetype="Associate">
   <affiliation dbid="25">
      <name>Malcolm </name>
   </affiliation>
   <affiliation dbid="15">
      <name>Mike </name>
   </affiliation>
</committee>

我不知道你的committee元素是否具有上述样式表处理的affiliation元素以外的其他子元素。如果需要更多代码。