Project | WXT | Basics | Download | Documentation | Samples

PI: importdb

The purpose of an import PI is to produce an XML-fragment from a database request.

<?_wxt importdb connection="" 
         sql="" 
         driver="" 
         xpath="" targetlocation="" 
         transformation=""?>

The parameters are:

connection (mandatory) A connectionstring. In templates this parameter may be skipped. In this case the connection in contentelement (dbcontent) in the script is used.
sql (optional) A sql string. If sql is set, sqlfile is ignored.
sqlfile (optional) The location of a file containing a sql string. If sql is set, sqlfile is ignored.
targetlocation (optional) The result of the sqlquery, or queries, are saved as an XML-file. This XML-structure is base for transformation and following xpath extraction. targetlocation is optional, but it is very usefull to inspect this file when writing and debugging a transformation.
encoding (optional) The encoding for the resulting xml. Default is UTF-8 if not changed in scrip (option:default-encoding)
xpath (optional) Any xpath expression that identifies a nodeset that will be treated like a XML-fragment.
parse (optional) Possible values are yes and no, width default no. If parse is set to yes the imported material is supposed to be a parsable XML-fragment (XHTML-fragment) and WXT will include this fragment as is in the module. If xpath is used the fragment imported is considered "current node".
transformation (optional) A transformation identifier (see: Transformation ). This transformation may have parameters. Transformation _table is predefined and produces a simple table from the extracted data. _table can take no parameters.

Styleclass describing a possible table-output from this PI is: dbresult, see distributed std styles.

Examples:

Importing a fragment of XHTMLcode

<?_wxt importdb 
    connection="jdbc:mysql://frigg.hiof.no/fragments?user=a&password=b"
    sql="use fragments;
        select content from material where fragid = 3;"
    encoding="UTF-8"
    parse="yes"
    targetlocation="dbdump4.xml"?>

Importing and showing the result as a table

<?_wxt importdb 
    connection="jdbc:mysql://frigg.hiof.no/vin?user=a&password=b"
    sql="use vin;
        select type,name,dice,price from wines 
		where country='Spania' and type='white';
        select type,name,dice from wines 
		where country = 'Frankrike' and type='sparkling' and dice=6;"
    encoding="UTF-8"
    transformation="_table"
    xpath="//div[@class='dbresult' and position()='1']"
    targetlocation="dbdump.xml"?>

Note the xpath stating that we will show the first of, in this case, two dbresults.

Importing and showing with a written transformation

<?_wxt importdb 
    connection="jdbc:mysql://frigg.hiof.no/vin?user=a&password=b"
    sql="use vin;
        select type,description,dice,name from wines 
		where country = 'Frankrike';"
    encoding="UTF-8"
    transformation="vindb1(type='red',dice='1')"
    xpath="//div[@id='root']/*"
    targetlocation="dbdump1.xml"?>