我真的在寻找一些关于在bash中解决这个问题的最佳方法的建议。
我有一个包含1000个条目的XML文件,如下所示:
<?xml version="1.0"?>
<myList>
<dataitem>
<path>./5553 Subset 43d.zip</path>
<name>5553 Subset 43d</name>
</dataitem>
<dataitem>
<path>./Another file name here with spaces.zip</path>
<name>Another file name here with spaces</name>
</dataitem>
...
我想使用带有mp4扩展名的<dataitem>
密钥数据为每个<name>
添加一个额外的密钥,所以它看起来像这样:
<?xml version="1.0"?>
<myList>
<dataitem>
<path>./5553 Subset 43d.zip</path>
<name>5553 Subset 43d</name>
<video>5553 Subset 43d.mp4</video>
</dataitem>
<dataitem>
<path>./Another file name here with spaces.zip</path>
<name>Another file name here with spaces</name>
<video>Another file name here with spaces.mp4</video>
</dataitem>
...
使用xmlstarlet
工具的正确方法:
xmlstarlet ed -s "//dataitem" -t elem -n video input.xml \
| xmlstarlet ed -u "//dataitem/video" -x "concat(./preceding-sibling::name/text(), '.mp4')"
输出应为:
<?xml version="1.0"?>
<myList>
<dataitem>
<path>./5553 Subset 43d.zip</path>
<name>5553 Subset 43d</name>
<video>5553 Subset 43d.mp4</video>
</dataitem>
<dataitem>
<path>./Another file name here with spaces.zip</path>
<name>Another file name here with spaces</name>
<video>Another file name here with spaces.mp4</video>
</dataitem>
...
或者,考虑XSLT,一种用于转换XML的专用语言,以及对xsltproc的bash调用。
XSLT(另存为.xsl文件,一个特殊的.xml文件)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="dataitem">
<xsl:copy>
<xsl:copy-of select="*"/>
<video><xsl:value-of select="concat(name, '.mp4')"/></video>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Bash(假设本地cd路径,使用已安装的xsltproc)
xsltproc -o Output.xml XSLTScript.xsl Input.xml