The goal is to change position of specific xml tag (and it's content) within the given string - ideally using regex.
The string (representing my xml data) has a structure that <MoveMe>
elements appear before <Target>
elements.
How to move all <MoveMe>.*</MoveMe>
and <xsi:MoveMe>.*</xsi:MoveMe>
occurences after after equivalent </Target>
or </xsi:Target>
??
input:
<?xml version="1.0"?>
<stylesheet version="1.0" xmlns:xsi="http://some.namespace.org">
<template>
<root>
<body>
<h2>sample</h2>
<table>
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<MoveMe>Hans Müller fist
content 1 </MoveMe>
<Target>
<td>a1</td>
<td>b1</td>
</Target>
</table>
<table>
<tr><th>Title</th></tr>
<xsi:MoveMe>again</xsi:MoveMe>
<xsi:Target>
<td>x2</td>
</xsi:Target>
</table>
</body>
</root>
</template>
</stylesheet>
output:
<?xml version="1.0"?>
<stylesheet version="1.0" xmlns:xsi="http://some.namespace.org">
<template>
<root>
<body>
<h2>sample</h2>
<table>
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<Target>
<td>a1</td>
<td>b1</td>
</Target>
<MoveMe>Hans Müller fist
content 1 </MoveMe>
</table>
<table>
<tr><th>Title</th></tr>
<xsi:Target>
<td>x2</td>
</xsi:Target>
<xsi:MoveMe>again</xsi:MoveMe>
</table>
</body>
</root>
</template>
</stylesheet>
So far i managed to capture all grupus of MoveMe
nodes using this pattern:
s_pat = "(<(xsi:)?MoveMe>(.*?)<\/(xsi:)?MoveMe>)"
Note that <table>
element can occur multiple times, but both MoveMe
and Target
elements are single.