Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php:2) in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pub/skins/simple/simple.php on line 39

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Division by zero in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1066

Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php:2) in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 866
Harrington Web : Brian - X Path Translator

Home > Projects > X Path Translator


This class translates simple XPath expressions into SQL queries. A very simple translation of relational tables to XML documents is assumed so that simple queries can be performed using XPath statements.

The translation works as follows:

Table A
A1A2A3A4
1234
11223344
111222333444
Table B
B1B2B3
123
112233
111222333

Would correspond to:

<Data>
    <A>
        <A1>1</A1>
        <A2>2</A2>
        <A3>3</A3>
        <A4>4</A4>
    </A>
    <A>
        <A1>11</A1>
        <A2>22</A2>
        <A3>33</A3>
        <A4>44</A4>
    </A>
    <A>
        <A1>111</A1>
        <A2>222</A2>
        <A3>333</A3>
        <A4>444</A4>
    </A>
    <B>
        <B1>1</B1>
        <B2>2</B2>
        <B3>3</B3>
    </B>
    <B>
        <B1>11</B1>
        <B2>22</B2>
        <B3>33</B3>
    </B>
    <B>
        <B1>111</B1>
        <B2>222</B2>
        <B3>333</B3>
    </B>
</Data>

So XPath statements can be written to get data from the database considering this structure. For example:

SQLXPath Expression
select * from A//A or /Data/A
select A1 from A//A/A1 or /Data/A/A1
select * from B where B2=22//B[B2=22]
select * from B where B2=22 and B3!=333//B[B2=22 and B3!=333]

This translator only supports simple XPath statements. The primary limitations are:

  • No way to perform joins.
  • XPath * operator not supported.
  • Must use absolute paths.
  • Axes and functions (except starts-with and contains) not supported.
  • // is treated as shorthand for /Data/ and so text after will be table name and will not match columns.
  • Conditions in [] are limited to expressions that are valid in both XPath and SQL where clause.
  • Attributes are not supported (but are not really needed).

Example of Use

public class Translate
{
    public static void main(String[] args)
    {
        XPathTranslator translator=new XPathTranslator();

        for(String xpath : args)
            System.out.println(translator.translate(xpath));
    }
}

Download

 
Page last modified on August 18, 2006, at 10:21 AM.