我正在尝试使用sed将发布者和isbn值合并到标题标签中。但是我在这里找不到符合我要求的示例。示例如下
来自此
<book>
<title>The Big Book of Silly Jokes for Kids</title>
<publisher>Rockridge Press</publisher>
<isbn>ISBN-10</isbn>
</book>
至此
<book>
<title>[Rockridge Press ISBN-10] The Big Book of Silly Jokes for Kids</title>
<publisher>Rockridge Press</publisher>
<isbn>ISBN-10</isbn>
</book>
使用xmlstarlet:
xml ed -u /book/title -x "concat('[',/book/publisher/text(),' ',/book/isbn,'] ',/book/title)" book.xml
输出:
<?xml version="1.0"?>
<book>
<title>[Rockridge Press ISBN-10] The Big Book of Silly Jokes for Kids</title>
<publisher>Rockridge Press</publisher>
<isbn>ISBN-10</isbn>
</book>
编辑:''之后的空格''--inplace'是可选的'inplace编辑文件'。
如您所说,用于修改XML文件中节点的命令行可以是xmlstarlet。
您还可以编写一个XSL程序,如下:
book.xsl
<?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" encoding="utf-8"/>
<xsl:template match="title"><!-- the node you want to modify -->
<title>
<xsl:text>[</xsl:text>
<xsl:value-of select="../publisher/text()"/>
<xsl:text> </xsl:text>
<xsl:value-of select="../isbn/text()"/>
<xsl:text>] </xsl:text>
<xsl:value-of select="../title/text()"/>
</title>
</xsl:template>
<xsl:template match="@*|node()"><!-- identity transformation for remaining nodes -->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
假设您的原始文件名为book.xml
,则可以在命令行上运行上述XSL程序:
xsltproc book.xsl book.xml