I have some XML that needs to be manipulated into a string to render some instructions. The text looks like this
<?xml version="1.0" encoding="UTF-8"?>
<instructions id="detection" version="1.0">
<instruction task="detection">
<phrase type="header">HAS THE CARD TURNED OVER?<nl/><nl/><nl/></phrase>
<phrase type="practice">you are now going to do a practice.<nl/><nl/></phrase>
<phrase type="real">You are now going to do a test.<nl/><nl/></phrase>
<phrase>As soon as the card turns face up:<nl/><nl/></phrase>
<phrase><ts/><ts/>Press YES.<nl/><nl/></phrase>
<phrase>Go as fast as you can and try not to make any mistakes.<nl/><nl/></phrase>
<phrase>If you press YES before a card turns face up, you will hear an error sound.</phrase>
</instruction>
</instructions>
Now, all I need to do is the following
- Replace all
<nl/>with \n - Replace all
<ts/>with \t - Conditionally select practice or real, probably by removing the other
- Remove all XML bits remaining to result in a string.
so lets say I want the practice version of this, I should end up with
HAS THE CARD TURNED OVER?\n\n\n
you are now going to do a practice.\n\n
As soon as the card turns face up:\n\n
\t\tPress YES.\n\n
Go as fast as you can and try not to make any mistakes.\n\n
If you press YES before a card turns face up, you will hear an error sound.
Now, I have the opportunity to change the structure of the XML if the current form isn’t ideal for this, but what I’m not sure is if I can do all of the above with e4X or I need to also use regex’s? Some examples would be great.
It can be done with E4X, probably not as elegantly as regex.
Here’s an example of replacing
<nl>with “\n” using E4x:A list of E4X methods is posted at http://wso2.org/project/mashup/0.2/docs/e4xquickstart.html
You can check for practice or real using xml.@type