我有大量的html文件,如下所示:
<html>
<body>
<div class="a">aaa
<div class="a1">a1a1a1</div>
<div class="a2">a2a2a2</div>
<div class="a2">a3a3a3</div>
</div>
<div class="v u">bbb
<div class="x">xxx</div>
<div class="y">yyy</div>
<div class="z">z1z1z1
<div class="w">www1</div>
<div class="w">www2</div>
<div class="w">www3</div>
</div>
<div class="z">z2z2z2
<div class="w">www4</div>
<div class="w">www5</div>
<div class="w">www6</div>
</div>
</div>
<div class="i">
<div class="j">jjj</div>
<div class="x">
<div class="k">
<div class="w">www7</div>
<div class="w">www8</div>
</div>
</div>
</div>
</body>
</html>
类x,y,z和w可以出现任意次,并且在整个html中都有任何div。
只有一个div class =“v u”
我想:
结果将是:
<html>
<body>
<div class="a">aaa
<div class="a1">a1a1a1</div>
<div class="a2">a2a2a2</div>
<div class="a2">a3a3a3</div>
</div>
<div class="b">bbb
<div class="b1">xxx</div>
<div class="b2">yyy</div>
<div class="b3">z1z1z1
<div class="b4">www1</div>
<div class="b4">www2</div>
<div class="b4">www3</div>
</div>
<div class="b3">z2z2z2
<div class="b4">www4</div>
<div class="b4">www5</div>
<div class="b4">www6</div>
</div>
</div>
<div class="i">
<div class="j">jjj</div>
<div class="x">
<div class="k">
<div class="w">www7</div>
<div class="w">www8</div>
</div>
</div>
</div>
</body>
</html>
我尝试了以下xslt,但没有给出预期的结果:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()" >
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="div[@class='v u']">
<div class="b">
<xsl:copy-of select="node()" />
</div>
</xsl:template>
<xsl:template match="div[@class='v u']/div[@class='x']">
<div class="b1">
<xsl:copy-of select="node()" />
</div>
</xsl:template>
<xsl:template match="div[@class='v u']/div[@class='y']">
<div class="b2">
<xsl:copy-of select="node()" />
</div>
</xsl:template>
<xsl:template match="div[@class='v u']/div[@class='z']">
<div class="b3">
<xsl:copy-of select="node()" />
</div>
</xsl:template>
<xsl:template match="div[@class='v u']/div[@class='z']/div[@class='w']">
<div class="b4">
<xsl:copy-of select="node()" />
</div>
</xsl:template>
</xsl:stylesheet>
我想我理解为什么它没有给我正确的结果,但我似乎找不到正确的解决方案。
答案 0 :(得分:2)
而不是所有<xsl:copy-of select="node()" />
,您需要使用<xsl:apply-templates/>
或<xsl:apply-templates select="node()"/>
来保持基于模板的处理。