我尝试使用apply-template将属性web添加到元素web的所有子元素。
source 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添加到Web的所有子元素? 有没有人有想法,怎么做?
答案 0 :(得分: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元素。