不要尝试使用来编辑XML
sed,它不是针对这种结构化数据而设计的。
sed当有人在您原本不希望的空白处插入良性空格时,编辑XML的脚本总是会崩溃,而没有编辑XML的人则希望由于布局更改而导致损坏。
相反,我将使用XSLT:
<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Identity template: just copy everything --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- special rule for version tags that include -SNAPSHOT and whose parent tag has an artifactId subtag that contains scheduler-service --> <xsl:template match="//version[contains(., '-SNAPSHOT') and not(contains(../artifactId, 'scheduler-service'))]"> <xsl:copy> <!-- copy attributes --> <xsl:apply-templates select="@*"/> <!-- and only use the part of the node content before -SNAPSHOT --> <xsl:value-of select="substring-before(., '-SNAPSHOT')"/> </xsl:copy> </xsl:template></xsl:stylesheet>
现在您可以使用例如
xsltproc foobar.xsl pom.xml
要么
xalan -in pom.xml -xsl foobar.xsl
根据您喜欢的XSLT处理器,foobar.xsl包含上面的样式表。



