我有大量具有以下结构的html文件:
for file in log_1.log log_2.log; do
echo "$file:"
sort -t' ' -k1,1M -k2,3n $file | sed -n '/Apr 10 02:07:20/,/Jul 11 11:07:30/p'
done
我想进行以下转换:
我想要获得的输出如下:
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>t</title>
</head>
<body>
<div class="a">
<div class="b">
<div class="b1" type="t23">b11</div>
<div class="b2" type="t45">b21</div>
</div>
<div class="a">
<div class="c">
<div class="b1" type="t67">b12</div>
<div class="b2" type="t89">b22</div>
</div>
</div>
</div>
</body>
</html>
我使用以下shell脚本:
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>t</title>
</head>
<body>
<div class="a">
<div class="b">
<div class="b1" type="t23">b11</div>
<div class="b2" type="t45">b21</div>
</div>
<div class="a">
<div class="c">
<div class="c1" type="t67">b12</div>
<div class="c2" type="t89">b22</div>
</div>
</div>
</div>
</div>
</body>
</html>
其中a.xslt如下:
xsltproc a.xslt a.html > b.html
然而,这个xslt将所有class =“b1”更改为class =“c1”,将所有class =“b2”更改为class =“c2”,忽略父属性。
您对如何解决此问题有任何建议吗?
答案 0 :(得分:1)
为什么不按照自己的意愿行事:
<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="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- if node div class="b1" has parent div class="c" then rename child attribute value to c1 (div class="c1") -->
<xsl:template match="div[@class='c']/div[@class='b1']/@class">
<xsl:attribute name="class">c1</xsl:attribute>
</xsl:template>
<!-- if node div class="b2" has parent div class="c" then rename child attribute value to c2 (div class="c2") -->
<xsl:template match="div[@class='c']/div[@class='b2']/@class">
<xsl:attribute name="class">c2</xsl:attribute>
</xsl:template>
</xsl:stylesheet>