0

I have the following XSLT, I am using identity transform because I need to keep the xml intact and only change a specific section of the XML i.e: the <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>

Transforms

<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>

Into

            <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>

How can i make it so it groups the affiliation into 4 per <committee committeetype="Associate">

Such as

  <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>

Thanks in advance.

4

1 回答 1

1

Here is a suggestion that might suffice:

<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>

It transforms

<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>

into

<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>

I don't know however whether your committee elements have other child elements than the affiliation ones the above stylesheet processes. If there are more code is needed.

于 2012-06-04T15:50:57.870 回答