|
 |
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|
 |

August 15th, 2006, 03:25 PM
|
Authorized User
|
|
Join Date: Dec 2004
Location: McHenry, IL, .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

August 15th, 2006, 03:33 PM
|
 |
Wrox Author
Points: 18,487, Level: 59 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You might like to look at the stylesheet that converts from XQueryX into XQuery, which is a rather similar task. See http://www.w3.org/TR/xqueryx/, especially Appendix B.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|

August 24th, 2006, 10:25 AM
|
Authorized User
|
|
Join Date: Dec 2004
Location: McHenry, IL, .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for your suggestions regarding XQueryX.
I spent some time reverse engineering the XQueryX translator to look for ideas. The organization of its XSLT translator suggested that I should re-structure my own translator. Unfortunately, XQueryX's simple syntax has been constructed to avoid limitations in XSLT V1.0.
I have posted another topic regarding one of several XSLT V1.0 issues: parsing and translating attribute strings (and their tokens).
Regards,
Philibuster
|
Thread Tools |
Search this Thread |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |