开发者

Automation of find replace in multiple xml files

I need to test a software enhancement I made recently. To do this, I need to make 1000 changes in 56 xml loader files (so 56,000 total). Specifically, I need to change the following:

   </users> 
</service>

into this

   </users>
   <rules>
       <ruleid="13e77ade-f15c-433f-aac8-2fdaf2d867a5" /> 
   </rules>
   <temprestriction /> 
</service>

I could do a find/replace on each of the 56 files, but that would be tedious. Is there a good way to automate this process? Than开发者_JAVA百科ks in advance.


Like @ConradFrix mentioned in a comment: you can use different tools/methods to solve this problem.

Here is a solution based on XSLT. The code is not tested extensively.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>

    <xsl:template match="node() | @*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="service/*[last()][self::users]">
        <xsl:call-template name="identity"/>
        <rules>
            <rule id="13e77ade-f15c-433f-aac8-2fdaf2d867a5" />
        </rules>
        <temprestriction />
    </xsl:template>

</xsl:stylesheet>

Key points: 1) using an identity template to recursively copy nodes of the document 2) a separate template to add new elements after <users> element if it is a last child of a <service> element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜