简体   繁体   中英

Editing XML files

I have some XML files and I need to edit them automatically.

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.

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:

<?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" />
  • using sed to duplicate line which contains <en
  • using awk to change the odd <en to <fr

WARNING: <en ... /> must be one line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM