Consider XSLT, the special-purpose language designed to transform XML files, where you can pass parameters (language type and translated text) between R to stylesheet for transformation. You can run XSLT 1.0 scripts with the xslt package (sister to xml2). Do note: XSLT is an industry language so is portable to other languages (Java, Python, PHP) and executables which do have parameter interfaces.
Specifically, run the Identity Transform template to copy XML as is and then bind parameters to new LANGENTRY node under INTLVAL node conditionally after searching for original text.
XSLT (save as .xsl file, a special .xml file)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- INITIALIZE PARAMS -->
<xsl:param name="orig_text"/>
<xsl:param name="trans_lang"/>
<xsl:param name="trans_text"/>
<!-- IDENTITY TRANSFORM -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- ADD LANGENTRY NODES WITH BINDED PARAM VALUES -->
<xsl:template match="INTLVAL">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<xsl:if test = "ancestor::EXPLANATION[@NAME = $orig_text]">
<LANGENTRY LANG="{$trans_lang}" VAL="{$trans_text}"/>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
R (demonstrating with loop)
library(xml2)
library(xslt)
# READ XML AND XSLT
doc <- read_xml("Input.xml", package = "xslt")
style <- read_xml("Script.xsl", package = "xslt")
# TRANSLATION DATA
translations <- list(
list(orig = c("de", "[b]5. Seit wann wohnen Sie in Deutschland, Leipzig bzw. Ihrem Wohngebiet? Bitte tragen Sie das Jahr ein."),
other = c("en", "[b]5. Since when do you live in Germany, Leipzig or the residential area you are living at the moment? Please enter the year. ")),
list(orig = c("de", "[b]5. Seit wann wohnen Sie in Deutschland, Leipzig bzw. Ihrem Wohngebiet? Bitte tragen Sie das Jahr ein."),
oth = c("fa", "ـ از چه زمانی تاکنون در آلمان بترتیب در لایپزیگ یا منطقه مسکونی خود زندگی می کنید؟")),
list(orig = c("de", "Die Sanftmütigen werden die Erde erben"),
other = c("en", "The meek shall inherit the earth")),
list(orig = c("de", "Die Sanftmütigen werden die Erde erben"),
other = c("fr", "Les doux hériteront de la terre")),
list(orig = c("de", "Sein oder Nichtsein; das ist hier die Frage"),
other = c("en", "To be, or not to be: that is the question")),
list(orig = c("de", "Sein oder Nichtsein; das ist hier die Frage"),
oth = c("es", "¡Ser, o no ser, es la cuestión!"))
)
# LOOP THROUGH TRANSLATIONS AND PASS IN PARAMS FOR TRANSFORMATION
for (t in translations) {
doc <- xml_xslt(doc, style,
params=list(orig_text = t$orig[2], # LIST NAME MUST MATCH XSLT PARAM
trans_lang = t$oth[1], # LIST NAME MUST MATCH XSLT PARAM
trans_text = t$oth[2]) # LIST NAME MUST MATCH XSLT PARAM
)
}
# SAVE EDITED DOC TO FILE
output <- write_xml(doc, "Output.xml")
Input XML (no LANGENTRY nodes)
<QUESTIONBLOCK PLACE="5" TARGETLAYER="0" RANDOMIZED="false" NAME="" TUTOR="false">
<META KEY="KEEP_TOGETHER" VALUE="false" />
<META KEY="METAKEY_ONLINE_QUESTION_SPACE" VALUE="DEFAULT" />
<META KEY="METAKEY_VALIDATION_RULE" VALUE="" />
<META KEY="METAKEY_TWO_COLUMN_PAPER" VALUE="false" />
<EXPLANATION PLACE="1" NAME="[b]5. Seit wann wohnen Sie in Deutschland, Leipzig bzw. Ihrem Wohngebiet? Bitte tragen Sie das Jahr ein." TUTOR="false">
<META KEY="METAKEY_ONLINE_QUESTION_SPACE" VALUE="DEFAULT" />
<INTLVAL KEY="NAME">
</INTLVAL>
</EXPLANATION>
<EXPLANATION PLACE="1" NAME="Die Sanftmütigen werden die Erde erben">
<META KEY="METAKEY_ONLINE_QUESTION_SPACE" VALUE="DEFAULT" />
<INTLVAL KEY="NAME">
</INTLVAL>
</EXPLANATION>
<EXPLANATION PLACE="1" NAME="Sein oder Nichtsein; das ist hier die Frage">
<META KEY="METAKEY_ONLINE_QUESTION_SPACE" VALUE="DEFAULT" />
<INTLVAL KEY="NAME">
</INTLVAL>
</EXPLANATION>
</QUESTIONBLOCK>
Output XML (new LANGENTRY nodes)
<?xml version="1.0" encoding="UTF-8"?>
<QUESTIONBLOCK PLACE="5" TARGETLAYER="0" RANDOMIZED="false" NAME="" TUTOR="false">
<META KEY="KEEP_TOGETHER" VALUE="false"/>
<META KEY="METAKEY_ONLINE_QUESTION_SPACE" VALUE="DEFAULT"/>
<META KEY="METAKEY_VALIDATION_RULE" VALUE=""/>
<META KEY="METAKEY_TWO_COLUMN_PAPER" VALUE="false"/>
<EXPLANATION PLACE="1" NAME="[b]5. Seit wann wohnen Sie in Deutschland, Leipzig bzw. Ihrem Wohngebiet? Bitte tragen Sie das Jahr ein." TUTOR="false">
<META KEY="METAKEY_ONLINE_QUESTION_SPACE" VALUE="DEFAULT"/>
<INTLVAL KEY="NAME">
<LANGENTRY LANG="en" VAL="[b]5. Since when do you live in Germany, Leipzig or the residential area you are living at the moment? Please enter the year. "/>
<LANGENTRY LANG="fa" VAL="ٜـ از چه زمانی تاکنون در آلمان بترتیب در لایپزیگ یا منطقه مسکونی خود زندگی می کنید؟"/>
</INTLVAL>
</EXPLANATION>
<EXPLANATION PLACE="1" NAME="Die Sanftmütigen werden die Erde erben">
<META KEY="METAKEY_ONLINE_QUESTION_SPACE" VALUE="DEFAULT"/>
<INTLVAL KEY="NAME">
<LANGENTRY LANG="en" VAL="The meek shall inherit the earth"/>
<LANGENTRY LANG="fr" VAL="Les doux hériteront de la terre"/>
</INTLVAL>
</EXPLANATION>
<EXPLANATION PLACE="1" NAME="Sein oder Nichtsein; das ist hier die Frage">
<META KEY="METAKEY_ONLINE_QUESTION_SPACE" VALUE="DEFAULT"/>
<INTLVAL KEY="NAME">
<LANGENTRY LANG="en" VAL="To be, or not to be: that is the question"/>
<LANGENTRY LANG="es" VAL="¡Ser, o no ser, es la cuestión!"/>
</INTLVAL>
</EXPLANATION>
</QUESTIONBLOCK>