[英]Editing XML files
I have some XML files and I need to edit them automatically. 我有一些XML文件,我需要自动对其进行编辑。
For example 例如
<Content>
<Texts>
<Text id="1">
<en value="blaabla" />
</Text>
<Text id="2">
<en value="blablablablal" />
</Text>
</Texts>
</Content>
I need to copy "en value" lines and add these lines to their under line but with one change. 我需要复制“ en value”行,并将这些行添加到其下一行,但要进行一次更改。
So when processing done, result should be that: 因此,处理完成后,结果应为:
<Content>
<Texts>
<Text id="1">
<en value="blablabla" />
<fr value="blablabla" />
</Text>
<Text id="2">
<en value="blablablablal" />
<fr value="blablablablal" />
</Text>
</Texts>
</Content>
You could use this XSLT to transform your XML files: 您可以使用此XSLT转换XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="en[@value]">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
<fr value="{@value}"/>
</xsl:template>
</xsl:stylesheet>
$ sed '/<en /p' data.txt | awk '/<en /{if(x++%2)sub(/<en /, "<fr ")}1'
<Content>
<Texts>
<Text id="1">
<en value="blaabla" />
<fr value="blaabla" />
</Text>
<Text id="2">
<en value="blablablablal" />
<fr value="blablablablal" />
sed
to duplicate line which contains <en
使用sed
复制包含<en
awk
to change the odd <en
to <fr
使用awk
将奇数<en
更改为<fr
WARNING: <en ... />
must be one line. 警告: <en ... />
必须是一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.