six*_*ude 21 xml bash scripting
我有一个xml文件,我想使用bash脚本配置.例如,如果我有这个xml:
<a>
<b>
<bb>
<yyy>
Bla
</yyy>
</bb>
</b>
<c>
<cc>
Something
</cc>
</c>
<d>
bla
</d>
</a>
Run Code Online (Sandbox Code Playgroud)
(保密信息已删除)
我想编写一个bash脚本来删除部分<b>(或注释),但保持xml的其余部分不变.我是整个脚本的新东西.我想知道是否有人能给我一个关于我应该注意什么的提示.
我认为可以使用sed,除了 sed是一个行编辑器.我认为删除<b>标签很容易,但我不确定sed是否能够删除标签之间的所有文本<b>.
我还需要编写一个脚本来添加已删除的部分.
ame*_*une 24
这在sed中并不难做到,因为sed也适用于范围.
试试这个(假设xml在一个名为foo.xml的文件中):
sed -i '/<b>/,/<\/b>/d' foo.xml
Run Code Online (Sandbox Code Playgroud)
-i会将更改写入原始文件(使用-i.bak保留原始文件的备份副本)
此sed命令将对范围指定的所有行执行操作d(删除)
# all of the lines between a line that matches <b>
# and the next line that matches <\/b>, inclusive
/<b>/,/<\/b>/
Run Code Online (Sandbox Code Playgroud)
因此,用简单的英语,这个命令将删除<b>行和</ b>行之间的所有行.
如果您更愿意注释掉这些行,请尝试以下方法之一:
# block comment
sed -i 's/<b>/<!-- <b>/; s/<\/b>/<\/b> -->/' foo.xml
# comment out every line in the range
sed -i '/<b>/,/<\/b>/s/.*/<!-- & -->/' foo.xml
Run Code Online (Sandbox Code Playgroud)
小智 14
使用xmlstarlet:
#xmlstarlet ed -d "/a/b" file.xml > tmp.xml
xmlstarlet ed -d "//b" file.xml > tmp.xml
mv tmp.xml file.xml
Run Code Online (Sandbox Code Playgroud)
您可以使用这样的XSLT,这是一个修改过的身份转换.它默认复制所有内容,并且有一个空模板b,它什么都不做(从输出中有效删除):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--Identity transform copies all items by default -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--Empty template to match on b elements and prevent it from being copied to output -->
<xsl:template match="b"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
创建一个使用Java和Xalan命令行实用程序执行转换的bash脚本,如下所示:
java org.apache.xalan.xslt.Process -IN foo.xml -XSL foo.xsl -OUT foo.out
结果是这样的:
<?xml version="1.0" encoding="UTF-16"?><a><c><cc>
Something
</cc></c><d>
bla
</d></a>
Run Code Online (Sandbox Code Playgroud)
编辑:如果您希望将b注释掉,以便更容易放回,那么使用此样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--Identity transform copies all items by default -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--Match on b element, wrap in a comment and construct text representing XML structure by applying templates in "comment" mode -->
<xsl:template match="b">
<xsl:comment>
<xsl:apply-templates select="self::*" mode="comment" />
</xsl:comment>
</xsl:template>
<xsl:template match="*" mode="comment">
<xsl:value-of select="'<'"/>
<xsl:value-of select="name()"/>
<xsl:value-of select="'>'"/>
<xsl:apply-templates select="@*|node()" mode="comment" />
<xsl:value-of select="'</'"/>
<xsl:value-of select="name()"/>
<xsl:value-of select="'>'"/>
</xsl:template>
<xsl:template match="text()" mode="comment">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="@*" mode="comment">
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" </xsl:text>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
它产生这个输出:
<?xml version="1.0" encoding="UTF-16"?><a><!--<b><bb><yyy>
Bla
</yyy></bb></b>--><c><cc>
Something
</cc></c><d>
bla
</d></a>
Run Code Online (Sandbox Code Playgroud)
如果您想要最合适sed的XML数据替换,它将是一个XSLT处理器.就像sed它是一种复杂的语言,但专门用于XML到任何变换的任务.
在另一方面,这也似乎是在我会认真考虑切换到一个真正的编程语言,像Python点.
| 归档时间: |
|
| 查看次数: |
36135 次 |
| 最近记录: |