OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   [xml-dev] find and replace using xpath

[ Lists Home | Date Index | Thread Index ]

Title: [xml-dev] find and replace using xpath

Hi,

I was wondering if anybody knows about a tool that will let me find and replace stuff in an XML document using XPath.  Finding things using XPath seems to be integrated in many XML IDEs, but I've never seen the ability to replace the matches then...  Ideally it should be possible to specify the replacement as a relative XPath.

An example.  Let's say I have a document like this (fragment of rng grammar produced by Trang for the Maven project grammar):

<grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">

        <define name="siteDirectory">
                <choice>
                        <notAllowed/>
                        <element name="siteDirectory">
                                <ref name="string"/>
                        </element>
                </choice>
        </define>
        <define name="subscribe">
                <choice>
                        <notAllowed/>
                        <element name="subscribe">
                                <ref name="string"/>
                        </element>
                </choice>
        </define>
</grammar>

Now I'd like to find "//define/choice[notAllowed and element and count(*) = 2]" and replace it with "element" yielding:

<grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">

        <define name="siteDirectory">
                <element name="siteDirectory">
                        <ref name="string"/>
                </element>
        </define>
        <define name="subscribe">
                <element name="subscribe">
                        <ref name="string"/>
                </element>
        </define>
</grammar>

(I'm no RNG expert, so forgive me if this is silly in terms of RNG.  But I'd still be glad for comments ;-))  So, for every element found by the find XPath, the relative replacement XPath was applied, yielding the replacement.  Does this make sense?

I guess this could be accomplished with a simple XSLT stylesheet, but I'd want something more lightweight.  Something like a regexp replace feature as found in many text editors.

Cheers,

--
knut





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS