Fill Freeman
Fill Freeman

Reputation: 199

Is there a simple way to replace a text in a particular node of xml

For example, I have a lot of files with a similar template:

<book book_id="foo">
  <title>Examples in different languages</title>
    <book_section>Tutorials</book_section>
      <body>
        <p>Some text</p>
        <section>
            <sectionheader>if</sectionheader>
            <sectionbody>
                <p>Depending on language you use, the sytax will be different:</p>
                <scriptsample>
                    <language_a>
                        <block>
                            <p>
                                &nbsp;&nbsp;...<br/>
                                &nbsp;&nbsp;<i>Do a comparation</i><br/>
                                &nbsp;&nbsp;<b>if</b> (a == b)<br/><br/>
                                &nbsp;&nbsp;{<br/>
                                &nbsp;&nbsp;&nbsp;&nbsp;do what you want<br/>
                                &nbsp;&nbsp;}<br/>
                            </p>
                        </block>
                    </language_a>
                    <language_b>
                        <block>
                            <p>
                                &nbsp;&nbsp;...<br/>
                                &nbsp;&nbsp;<i>Do a comparation</i><br/>
                                &nbsp;&nbsp;<b>if</b> (a == b)<br/><br/>
                                &nbsp;&nbsp;{<br/>
                                &nbsp;&nbsp;&nbsp;&nbsp;do what you want<br/>
                                &nbsp;&nbsp;}<br/>
                            </p>
                        </block>
                    </language_b>
                </scriptsample>
            </sectionbody>
        </section>
        <p>Some other text</p>
        <section>
            <sectionheader>while</sectionheader>
            <sectionbody>
                <p>Depending on language you use, the sytax will be different:</p>
                <scriptsample>
                    <language_a>
                            <p>
                                &nbsp;&nbsp;...<br/>
                                &nbsp;&nbsp;<i>Do a comparation</i><br/>
                                &nbsp;&nbsp;<b>while</b> (a == b)<br/><br/>
                                &nbsp;&nbsp;{<br/>
                                &nbsp;&nbsp;&nbsp;&nbsp;do what you want<br/>
                                &nbsp;&nbsp;}<br/>
                            </p>
                    </language_a>
                    <language_b>
                            <p>
                                &nbsp;&nbsp;...<br/>
                                &nbsp;&nbsp;<i>Do a comparation</i><br/>
                                &nbsp;&nbsp;<b>while</b> (a == b)<br/><br/>
                                &nbsp;&nbsp;{<br/>
                                &nbsp;&nbsp;&nbsp;&nbsp;do what you want<br/>
                                &nbsp;&nbsp;}<br/>
                            </p>
                    </language_b>
                </scriptsample>
            </sectionbody>
        </section>
    <body>
</book>

Now, I want to replace the text (a == b) with compare(a, b) ONLY in the language_b element.

I managed to find all the files by using XPath in Altova XMLSpy, but I cannot change files.

I tried to create a regular expression for that, but I failed.

By the way, Another obstacle is that a and b can be method calls (obj.method(args)). So its really hard to use regular expression here, but it is another task)

I also tried to do an XQuery Apply Update element feature (also in Altova XMLSpy):

for $x in //language_b/*[contains(., 'a == b')] return
replace value of node $x with (
replace($x, 'a == b', 'compare(a, b)')
)

But it looses child elements within the "language_b". (like i, nbsp and so on). Also, Altova adds default attributes all around the entire file, that makes me sad because of useless elements.

I don't believe that there were not such tasks before mine.

Is there any suggestions?

PS. Now I am trying to create a something by using DOM in C# (but due to lack of knowledge it will take a long time)

Upvotes: -1

Views: 388

Answers (1)

Ashutosh Jha
Ashutosh Jha

Reputation: 1513

Try using dom or stax for this.

Upvotes: 0

Related Questions