2016-09-06 2 views
0

Brauchen Sie Hilfe. Bitte beachten Sie meinen Kommentar in der 'Abteilung' Vorlage. Hier möchte ich die gleiche Verarbeitung mehrmals wiederholen (basierend auf der Variablen $ time). Wenn ich eine Call-Vorlage verwende und den aktuellen Knoten übergebe, kann ich die bereits geschriebenen Child-Template-Regeln nicht erneut verwenden. Was ist ein guter Weg, dies zu erreichen?Schleife ohne Kontext Knotenwechsel

XML

<?xml version="1.0" encoding="UTF-8"?> 
    <emp> 
    <department name="science"> 
     <empname>Rob</empname> 
     <empno>01</empno> 
     <emptype>regular</emptype> 
    </department> 
    </emp> 

XSLT

<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:param name="times" select="'a1,b1,c1'"></xsl:param> 
     <xsl:template match="/">   
      <xsl:apply-templates/> 
     </xsl:template> 
     <xsl:template match="department"> 
      <xsl:copy> 
       <xsl:copy-of select="@*"></xsl:copy-of> 
       <xsl:apply-templates select="empname"/> 
       <someelements></someelements> 
       <xsl:apply-templates select="empno"/> 
       <instances> 
       <xsl:for-each select="tokenize($times, ',\s*')"> 
        <xsl:variable name="time" select="."/> 
        <iemployee time="{$time}"> 
         <!-- I want to repat the the "department" template here --> 
        </iemployee> 
       </xsl:for-each> 
       </instances> 
      </xsl:copy> 
     </xsl:template> 
     <xsl:template match="empname"> 
      <employeename> 
       <xsl:apply-templates/> 
      </employeename> 
     </xsl:template> 
     <xsl:template match="empno"> 
      <employeenumber> 
       <xsl:apply-templates/> 
      </employeenumber> 
     </xsl:template> 
    </xsl:stylesheet> 

Ausgang

<department name="science"> 
<employeename>Rob</employeename> 
<someelements/> 
<employeenumber>01</employeenumber> 
<instances> 
    <iemployee time="a1"> 
     <employeename>Rob</employeename> 
     <someelements/> 
     <employeenumber>01</employeenumber> 
    </iemployee> 
    <iemployee time="b1"> 
     <employeename>Rob</employeename> 
     <someelements/> 
     <employeenumber>01</employeenumber> 
    </iemployee> 
    <iemployee time="c1"> 
     <employeename>Rob</employeename> 
     <someelements/> 
     <employeenumber>01</employeenumber> 
    </iemployee> 
</instances> 

Antwort

2

Ich glaube, Sie brauchen eine zweite Vorlage mit einem anderen Modus:

<xsl:template match="department"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"></xsl:copy-of> 
      <xsl:apply-templates select="." mode="content"/> 
      <instances> 
      <xsl:variable name="dep" select="."/> 
      <xsl:for-each select="tokenize($times, ',\s*')"> 
       <xsl:variable name="time" select="."/> 
       <iemployee time="{$time}"> 
        <xsl:apply-templates select="$dep" mode="content"/> 
       </iemployee> 
      </xsl:for-each> 
      </instances> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="department" mode="content"> 
      <xsl:apply-templates select="empname"/> 
      <someelements></someelements> 
      <xsl:apply-templates select="empno"/> 
    </xsl:template> 
1

Wenn ich Ihre Frage richtig verstanden, dann denke ich, Sie fast da sind:

<xsl:template match="department"> 
     <!-- Retain a reference to the current department element --> 
     <xsl:variable name="dept" select="." as="node()"/> 
     <xsl:copy> 
      <xsl:copy-of select="@*"></xsl:copy-of> 
      <xsl:apply-templates select="empname"/> 
      <someelements></someelements> 
      <xsl:apply-templates select="empno"/> 
      <instances> 
      <xsl:for-each select="tokenize($times, ',\s*')"> 
       <xsl:variable name="time" select="."/> 
       <iemployee time="{$time}"> 
        <!-- Apply templates to all children of the department node --> 
        <xsl:apply-templates select="$dept/*"/> 
       </iemployee> 
      </xsl:for-each> 
      </instances> 
     </xsl:copy> 
    </xsl:template> 
+0

, dass die '' auch nicht in der Ausgabe angezeigt wird erstellen. –

+0

Richtig, guter Fang. Eine sekundäre benannte Vorlage oder eine Vorlage in einem anderen Modus wäre dann am besten. – OneTrickyPony