Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 15th, 2006, 03:25 PM
Authorized User
Points: 138, Level: 2
Points: 138, Level: 2 Points: 138, Level: 2 Points: 138, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2004
Location: McHenry, IL, .
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
Reply With Quote
  #2 (permalink)  
Old August 15th, 2006, 03:33 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
  #3 (permalink)  
Old August 24th, 2006, 10:25 AM
Authorized User
Points: 138, Level: 2
Points: 138, Level: 2 Points: 138, Level: 2 Points: 138, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2004
Location: McHenry, IL, .
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.