如何使用bash脚本编辑XML? [英] how to edit XML using bash script?

查看:200
本文介绍了如何使用bash脚本编辑XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<root>
<tag>1</tag>
<tag1>2</tag1>
</root>

需要从bash的更改值1和2

Need to change values 1 and 2 from bash

推荐答案

您可以使用 xsltproc的命令(从包装 xsltproc的在基于Debian的发行版),与下面的XSLT表:

You can use the xsltproc command (from package xsltproc on Debian-based distros) with the following XSLT sheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:param name="tagReplacement"/>
  <xsl:param name="tag1Replacement"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>

  </xsl:template>
  <xsl:template match="tag">
    <xsl:copy>
      <xsl:value-of select="$tagReplacement"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="tag1">
    <xsl:copy>
      <xsl:value-of select="$tag1Replacement"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

然后使用命令:

xsltproc --stringparam tagReplacement polop \
         --stringparam tag1Replacement palap \
         transform.xsl input.xml

或者你也可以使用正则表达式,但通过正则表达式修改XML是纯粹的邪恶:)

Or you could also use regexes, but modifying XML through regexes is pure evil :)

这篇关于如何使用bash脚本编辑XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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