Reputation: 199
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>
...<br/>
<i>Do a comparation</i><br/>
<b>if</b> (a == b)<br/><br/>
{<br/>
do what you want<br/>
}<br/>
</p>
</block>
</language_a>
<language_b>
<block>
<p>
...<br/>
<i>Do a comparation</i><br/>
<b>if</b> (a == b)<br/><br/>
{<br/>
do what you want<br/>
}<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>
...<br/>
<i>Do a comparation</i><br/>
<b>while</b> (a == b)<br/><br/>
{<br/>
do what you want<br/>
}<br/>
</p>
</language_a>
<language_b>
<p>
...<br/>
<i>Do a comparation</i><br/>
<b>while</b> (a == b)<br/><br/>
{<br/>
do what you want<br/>
}<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