I am creating a script to translate xml documents. This is actually pretty cool, the idea (and it works) is to take an xml file (or a folder of xml files) and open it, parse the xml, get everything in between some tags, and use the Google translation api translate it and replace the contents of the files xml.
As I said, it works for me, but only in fairly strict documents formatted in XML, now I have to make it compatible with documents formatted in different ways. Therefore my idea:
Parse xml, find node, for example:
<template>lorem lipsum dolor mit amet<think><set name="she">Ada</set></think></template>
Save this as a string, search for and replace regular expressions in that string. But I, unfortunately, do not know how to proceed. I want the string search (xml node) to find the text that is between the tags, in this case "lorem lipsum dolor mit amet" and "Ada", call a function with this text as a parameter, and then insert the result of the function in the same the place in which it was created.
The reason I can't just get the text and rebuild the xml formatting is because there will be XML nodes formatted differently, so I need them to be identical ...
source
share