是否可以使用xmlstarlet或其他bash工具对xml文件中的标签进行注释/取消注释 [英] Is it possible to comment/uncomment tags inside an xml file using xmlstarlet or other bash tools

查看:57
本文介绍了是否可以使用xmlstarlet或其他bash工具对xml文件中的标签进行注释/取消注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用xmlstarlet或任何其他Shell脚本库/工具等以编程方式注释/取消注释xml文件中的标记块.

How can i comment/uncomment tag blocks inside an xml file programmatically using xmlstarlet or any other shell scripting libraries/tools etc.

评论...

输入文件:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

输出文件:

<note>
<to>Tove</to>
<!-- <from>Jani</from> -->
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


取消注释...


Uncommenting...

输入文件:

<note>
<to>Tove</to>
<!-- <from>Jani</from> -->
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

输出文件:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

推荐答案

在使用xlstproc之后,我想出了一种解决情况的解决方案.包含"功能可以解决问题……

After playing with xlstproc, i come up with a solution for uncommenting case. 'contains' function does the trick...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!--Identity template,
   provides default behavior that copies all content into the output -->
   <xsl:template match="@*|node()">
       <xsl:copy>
           <xsl:apply-templates select="@*|node()"/>
       </xsl:copy>
   </xsl:template>
   <!--More specific template for "from" that provides custom behavior -->
   <xsl:template match="comment()">

       <xsl:if test='contains(.,"&lt;from&gt;")' >
           <xsl:value-of  select="." disable-output-escaping="yes" />
       </xsl:if>

       <xsl:if test='not (contains(.,"&lt;from&gt;"))' > 
           <xsl:copy-of select="." />
       </xsl:if>

   </xsl:template>
</xsl:stylesheet>

这篇关于是否可以使用xmlstarlet或其他bash工具对xml文件中的标签进行注释/取消注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆