[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

find and replace using xpath


xpath replace
Title: 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


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.