2016-08-08 2 views
0

apply-templateを使用して要素webのすべての子要素に属性webを追加しようとしています。apply-templatesを使用してすべての子要素に属性を追加する

ソース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> 

ターゲット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> 

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

は、テンプレートを適用し、Webのすべての子要素に属性ウェブを追加するためのいずれかの可能性があります? 誰もが、どのようにこれを行うアイデアを持っていますか? 整形入力などを考える

+0

あなたが私たちに示したソース文書は整形式のXMLではありません。 –

答えて

0

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> 

次のスタイルシート:

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> 

が返されます:

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

tabelが有効なHTML要素ではないこと。

+0

あなたの提案をありがとうございます、それは絶対に完璧な作品です。 – Olli

関連する問題