我有大量文件,其结构类似于以下a.html:
<html>
<body>
<div class="a">aaa
<div class="b">bbb</div>
<div class="c">ccc1
<div class="d">ddd11
<div class="e">eee11</div>
<div class="f">fff11
<div class="g">ggg111</div>
<div class="g">ggg112</div>
<div class="g">ggg113</div>
<div class="g">ggg114</div>
<div class="g">ggg115</div>
<div class="g">ggg116</div>
</div>
</div>
<div class="d">ddd12
<div class="e">eee12</div>
<div class="f">fff12
<div class="g">ggg121</div>
<div class="g">ggg122</div>
<div class="g">ggg123</div>
<div class="g">ggg124</div>
</div>
</div>
</div>
<div class="c">ccc2
<div class="d">ddd21
<div class="e">eee21</div>
<div class="f">fff21
<div class="g">ggg211</div>
<div class="g">ggg212</div>
<div class="g">ggg213</div>
<div class="g">ggg214</div>
<div class="g">ggg215</div>
</div>
</div>
</div>
...
</div>
</body>
</html>
我想要一些div class =&#34; c&#34;等于所有文件中的max_c参数。
我使用以下shell脚本传递max_c参数,其值等于3:
#!/bin/bash
xsltproc --param max_c 3 a.xslt a.html
我使用以下a.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:param name="max_c"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[@class='a']">
<xsl:copy>
<xsl:apply-templates select="div[@class='a']" />
<xsl:apply-templates select="div[@class='b']" />
<xsl:apply-templates select="div[@class='c']" />
<xsl:call-template name="AddC">
<xsl:with-param name="count" select="$max_c - count(div[@class='c'])" />
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="AddC">
<xsl:param name="count" />
<xsl:if test="$count > 0">
<div class="c">ccc
</div>
<xsl:call-template name="AddC">
<xsl:with-param name="count" select="$count - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
输出如下:
<html>
<body>
<div>
<div class="b">bbb</div>
<div class="c">ccc1
<div class="d">ddd11
<div class="e">eee11</div>
<div class="f">fff11
<div class="g">ggg111</div>
<div class="g">ggg112</div>
<div class="g">ggg113</div>
<div class="g">ggg114</div>
<div class="g">ggg115</div>
<div class="g">ggg116</div>
</div>
</div>
<div class="d">ddd12
<div class="e">eee12</div>
<div class="f">fff12
<div class="g">ggg121</div>
<div class="g">ggg122</div>
<div class="g">ggg123</div>
<div class="g">ggg124</div>
</div>
</div>
</div>
<div class="c">ccc2
<div class="d">ddd21
<div class="e">eee21</div>
<div class="f">fff21
<div class="g">ggg211</div>
<div class="g">ggg212</div>
<div class="g">ggg213</div>
<div class="g">ggg214</div>
<div class="g">ggg215</div>
</div>
</div>
</div>
<div class="c">ccc$count</div>
</div>
</body>
</html>
这几乎是我想要的,有两个例外:
我做错了什么?
像往常一样,我事先感谢你的帮助。
答案 0 :(得分:2)
第一个div失去了它的class =“a”及其值aaa
要添加缺少的属性和文本内容,请更改xslt:copy
,如下所示:
<xsl:copy>
<xsl:apply-templates select="@* | div[@class='b'] | text()" />
<xsl:apply-templates select="div[@class='c']" />
@*
添加所有属性和text()
所有文字内容。
添加的div class =“c”的值应为ccc3,而不是ccc $ count 将div c的输出更改为:
<div class="c">
ccc<xsl:value-of select="$count"/>
</div>