2016-08-08 57 views
0

Ich versuche, allen untergeordneten Elementen des Elementwebs mithilfe von apply-template ein Attributweb hinzuzufügen.Hinzufügen von Attributen zu allen untergeordneten Elementen mithilfe von Anwendungsvorlagen

Quelle xml

<p></p> 
<tabel> 
<tr> 
    <td> 
    <web> 
    <p></p> 
    <p></p> 
    </web> 
    </td> 
</tr> 
<tr> 
    <td> 
    <web> 
    <p></p> 
    <ul> 
    <li></li> 
    <li></li> 
    </ul> 
    </web> 
    </td> 
</tr> 
</tabel> 

Ziel xml

<p></p> 
<tabel> 
<tr> 
    <td> 
    <p class="web"></p> 
    <p class="web"></p> 
    </td> 
</tr> 
<tr> 
    <td> 
    <p class="web"></p> 
    <ul class="web"> 
    <li></li> 
    <li></li> 
    </ul> 
    </td> 
</tr> 
</tabel> 

mein xsl

<xsl:template match="p"> 
<p> 
    <xsl:apply-templates/> 
</p> 
</xsl:template> 
<xsl:template match="web"> 
<xsl:apply-templates/> 
</xsl:template> 
<xsl:template match="ul"> 
<ul> 
    <xsl:apply-templates/> 
</ul> 
</xsl:template> 

Gibt es eine Möglichkeit zu-Vorlagen anzuwenden und das Attribut Web auf alle untergeordneten Elemente des Web hinzufügen ? Hat jemand eine Idee, wie man das macht?

+0

Das Quelldokument, das Sie uns zeigen, ist kein wohlgeformtes XML. –

Antwort

0

Bei einer wohlgeformten Eingangs wie:

XML

<html> 
    <p></p> 
    <tabel> 
     <tr> 
     <td> 
     <web> 
      <p></p> 
      <p></p> 
     </web> 
     </td> 
     </tr> 
     <tr> 
     <td> 
     <web> 
      <p></p> 
      <ul> 
       <li></li> 
       <li></li> 
      </ul> 
     </web> 
     </td> 
     </tr> 
    </tabel> 
</html> 

folgendes Sheet:

XSLT 1,0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" encoding="UTF-8"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="web"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="web/*"> 
    <xsl:copy> 
     <xsl:attribute name="class">web</xsl:attribute> 
     <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

zurückkehren wird:

<html> 
    <p></p> 
    <tabel> 
     <tr> 
     <td> 
      <p class="web"></p> 
      <p class="web"></p> 
     </td> 
     </tr> 
     <tr> 
     <td> 
      <p class="web"></p> 
      <ul class="web"> 
       <li></li> 
       <li></li> 
      </ul> 
     </td> 
     </tr> 
    </tabel> 
</html> 

Beachten Sie, dass tabel kein gültiges HTML-Element ist.

+0

Vielen Dank für Ihren Vorschlag, es ist absolut perfekt. – Olli