Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 15th, 2006, 03:25 PM
Authorized User
 
Join Date: Dec 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old August 15th, 2006, 03:33 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old August 24th, 2006, 10:25 AM
Authorized User
 
Join Date: Dec 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting Source Xml into Target Xml Using XSL. alapati.sasi XSLT 3 May 14th, 2007 10:54 AM
any markup language to XML bcogney XSLT 10 April 16th, 2007 08:39 AM
HOW TO: xlate attribute values? Philibuster XSLT 4 August 24th, 2006 12:17 PM
How to program with XML from VB chanti VB How-To 2 October 4th, 2004 03:42 AM
a xml program csc820203 C# 1 May 27th, 2004 05:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.