Peter: XSLT Tags gruppieren

Hi,

Wie kann ich mit XSLT apply-templates so verwenden, dass die Elemente nach Tags gruppiert werden und über bestimmte Gruppen ein übergeordneter Tag eingefügt wird (aber nur wenn Kindelemente gefunden wurden!)?

Bsp:

<Root>
  <Heading Name="Ich bin eine normale Überschrift">
    <R ID="1" />
    <R ID="2" />
    <R ID="3" />
    <Heading Type="Node" Name="Subnode">
      <Heading Type="Node" Name="SubSubNode">
        <R ID="x0"/>
        <Heading Name="Unterüberschrift">
          <R ID="x1"/>
          <R ID="x2"/>
          <R ID="x3"/>
        </Heading>
        <R ID="x4"/>
      </Heading>
    </Heading>
    <R ID="4" />
    <R ID="5" />
    <Heading Type="Node" Name="Empty Subnode">
    </Heading>
  </Heading>
</Root>

Alle Heading die nicht Type="Node" sind sollen ignoriert werden. Für den Rest soll die Struktur erhalten bleiben. Dabei sollen alle <R> Elemente einer Hierarchie zuerst zusammengefasst werden, falls in der Hierarchie Elemente existieren, mit <RS></RS> umschlossen werden und danach ggf. Unterknoten geschrieben werden. Also:

<Scope>
  <RS>
    <R ID="1" />
    <R ID="2" />
    <R ID="3" />
    <R ID="4" />
    <R ID="5" />
  </RS>

<Scope Name="Subnode">
    <Scope Name="SubSubNode">
      <RS>
        <R ID="x0"/>
        <R ID="x1"/>
        <R ID="x2"/>
        <R ID="x3"/>
        <R ID="x4"/>
      </RS>
    </Scope>
  </Scope>
  <Scope Name="Empty Subnode"/>
</Scope>

Mein XSL sieht so aus:

<xsl:template match="/Root">
  <Scope>
    <RS> <!-- RS soll nur erstellt werden, wenn es Kinder gibt! -->
       <xsl:apply-templates select="//R" />
    </RS>
    <xsl:apply-templates />
  </Scope>
</xsl:template>

<xsl:template match="//Heading[@Type != 'Node']">
  <RS>
    <xsl:apply-templates />
  </RS>
</xsl:template>

<xsl:template match="//Heading[@Type = 'Node']">
  <Scope>
    <xsl:attribute name="Name">
      <xsl:value-of select="@Name"/>
    </xsl:attribute>
    <xsl:apply-templates />
  </Scope>
</xsl:template>

<xsl:template match="//R">
  <xsl:copy-of select="."/>
</xsl:template>

Wie bringe ich das richtig hin?

LG
Peter

  1. Hi,

    warum machst du es dir so schwer. Moduliere einfach deine Templates, also zum Beispiel:

      
      
     <xsl:template match="*">  
     <!-- zuerst nur templates der gruppe 1 abarbeiten -->  
     <xsl:apply-templates mode="gruppe1"/>  
     <!-- dann nur templates der gruppe 2 -->  
     <xsl:apply-templates mode="gruppe2"/>  
     </xsl:template>  
      
     <!-- gruppe 1 template -->  
     <xsl:template mode="gruppe1" match=" ... ">  
       ...  
     </xsl:template>  
     <xsl:template mode="gruppe1" match=" ... ">  
       ...  
     </xsl:template>  
      
     <!--gruppe 2 templates -->  
     <xsl:template mode="gruppe2" match=" ... ">  
       ...  
     </xsl:template>  
     <xsl:template mode="gruppe2" match=" ... ">  
       ...  
     </xsl:template>  
      
      
    
    

    Gruss, Holge r