How to: Xlate program in XML to target language?
How would I translate an XML representation of a program into a target language using XSLT? (In particular, I would like to translate o:XML into PHP.)
For example, suppose we have the o:XML representation of a program (below) and would like to translate it into a PHP program (see below - may syntax and semantical errors but this will give a rough idea). Is there an example to illustrate the recursive XSLT translator that would do this. Any help would be greatly appreciated.
============ o:XML program A =================
<?xml version="1.0"?>
<program xmlns:o="http://www.o-xml.org/lang/">
<o:type name="Translator">
<o:parent name="Node"/>
<o:variable name="map"/>
<o:function name="Translator">
<o:param name="map"/>
<o:do/>
</o:function>
<o:function name="translate">
<o:param name="nodes"/>
<o:do>
<o:for-each name="text" select="$nodes/*/text()">
<o:set translated="$this.translate($text)"/>
<o:log msg="{$text} = {$translated}"/>
<o:do select="$text.replace($translated)"/>
</o:for-each>
<o:return select="$nodes"/>
</o:do>
</o:function>
<o:function name="translate">
<o:param name="text" type="String"/>
<o:do>
<o:return select="$map/item[from = $text]/to/text()"/>
</o:do>
</o:function>
</o:type>
<o:variable name="words">
<item>
<from>here</from>
<to>aqui</to>
</item>
<item>
<from>where</from>
<to>donde</to>
</item>
<item>
<from>never</from>
<to>nunca</to>
</item>
</o:variable>
<o:set translator="Translator($words)"/>
<o:variable name="speak">
<line>where</line>
<word>never</word>
<stop>here</stop>
</o:variable>
<o:eval select="$translator.translate($speak)"/>
<o:for-each name="text" select="$speak/*/text()">
<o:set translated="$translator.translate($text.string())"/>
<o:log msg="{$text} = {$translated}"/>
<o:do select="$text.replace($translated)"/>
</o:for-each>
<o:eval select="$speak"/>
<o:log msg="here: {$translator.translate('here')}"/>
<o:do select="$speak//text().replace('yoyo')"/>
<o:eval select="$speak"/>
</program>
=========== XSLT Desired Result: PHP Program A ====================
<?PHP
import "support_functions.php";
class Translator extends Node
{
var $map;
function Translator($map)
{
return NULL;
}
function translate_nodes($nodes)
{
foreach (selectNodes("$node/*/text()) as $key => $text)
{
$translated = "$this.translate($text)";
log("{$text} = {$translated}");
selectNodes("$text->replace($translated)");
}
return selectNodes("$nodes");
}
function translate_text($text, $type="String")
{
return selectNodes("$map/item[from = $text]/to/text()");
}
}
$words = {{"from" => "here", "to" => "aqui"}, {"from" => "where", "to" => "donde"}, {"from" => "never", "to" => "nunca"}};
$translator = "Translator($words)";
$speak = {"line" => "where", "word" => "never", "stop" => "here"};
foreach (selectNodes("$speak/*/text()") as $key => $text)
{
$translated = "$translator.translate($text.string())";
log("{$text} = {$translated}");
selectNodes("$text.replace($translated)");
}
eval(selectNodes("$speak"));
log("here: {$translator.translate('here')}");
selectNodes("$speak//text().replace('yoyo')");
eval(selectNodes("$speak"));
?>
Philibuster
__________________
Philibuster
|