473,416 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

xslt queries in xml to SQL queries


Hello,

I want to embed SQL type queries within an XML data record. The XML
looks something like this:
<DISPLAYPAGE>
<FIELD NAME="SERVER" TYPE="DROPDOWN">
<OPTION>1<OPTION>
<OPTION>2<OPTION>
<OPTION>3<OPTION>
</FIELD>
</DISPLAYPAGE>

I want to populate the drop down options from a database. The table
looks like this (example):
CREATE TABLE options_table (
id int primary key,
name varchar(255),
field varchar(255)
)
I read an article that would allow me to embed an SQL query using XSLT
like this (in place of <OPTIONS>):
<xslt:template match="options_table[field='SERVER']">
<OPTION><xslt:value-of select="name/text()"/></OPTION>
</xslt:template>

Which would be equivalent of:

SELECT name FROM options_table WHERE field='SERVER';

Does anyone else have experience of describing SQL queries in XML or
parsing XSLT in python?
--
To mail remove REMOVEME. from address.

Feb 26 '06 #1
3 3064
Skipping ahead, let me try to rephrase this.

First, this isn't really a python question, it is SQL, XSLT, and
program design, but I'll try to answer.

You have templates, they contain general layout stuff, and input
fields. You are transforming them into HTML pages, and part of what you
want to do is to expand the enumerated fields with the enumeration
values at the time of template application.

You read an article about XSLT, and decided that you could use it in
your app. For some unknown reason, perhaps involving large amounts of
alcohol, you want to use the syntax of XSLT, but have it actually
parsed by python, which silently does an SQL querry to give you your
results.

This is a really, really bad idea. There are many ways to solve your
problem using XSLT, and though all of them are easy, none of them are
so simple that I could put them in a post to someone who doesn't know
XSLT. If you know XSLT, follow the directions below, if you don't, DO
NOT USE XSLT for this project. And for the love of anything you may
believe in, don't try to implement a partial XSLT engine in python.
Pattern 1, paramaterized templates:

Pattern 1 assumes that all the data you will need for your templates is
known in advance, and is common to your templates. If this is true, you
need only generate an XML fragment with the data you need, and compose
a document which contains both the data you need, and the page template
you are rendering. Then, write an XSLT transform to build your pages,
which can now resolve data needs in the template section using the data
provided in the data section.

Pattern 2, query extraction:

When you don't know what data the page template might need, and the
full dataset is unmanageably large, you can't use pattern 1 easily. So
instead, write an XSLT transform which _only_ extracts information
about the data you need. You can then run this transform, get the query
list, perform the queries, and use the resultant data for the data tree
from pattern 1.

Feb 26 '06 #2
Crutcher wrote:
Skipping ahead, let me try to rephrase this.

First, this isn't really a python question, it is SQL, XSLT, and
program design, but I'll try to answer.
Well, first of all, it's about mapping XPath onto a relational data
model. This is clear from the original posting:

<xslt:template match="options_table[field='SERVER']">
<OPTION><xslt:value-of select="name/text()"/></OPTION>
</xslt:template>

Here, it makes sense to consider instances of options_table elements as
rows in the options_table table, just as described. Perhaps, if field
and name are columns in options_table, I would rather model them using
attributes:

<xslt:template match="options_table[@field='SERVER']">
<OPTION><xslt:value-of select="@name"/></OPTION>
</xslt:template>

Is this possible or wise to do? Well, I did myself write an
XPath-to-SQL query engine, together with a simple "document relational"
mapper in order to be able to execute XPath expressions similar to
those above. However, I don't believe that my code really delivered on
the concept because...

* Generating efficient sets of queries can be tricky: a simple
descent into what appears to be an XML document needs
to involve a lot of joins to enforce the virtual "element
hierarchy".

* One has to decide whether to enforce a schema or not: if you don't
this means that XPath axes like "child" can behave like
"descendant-or-self". If you do, generating "descendant-or-self"
queries becomes very difficult (as far as I can tell and remember).

* The XPath specification states that nodes are returned in document
order - this is difficult to enforce unless you litter your tables
with additional information (again as far as I remember).

* Generating simple DOM structures for simple SQL queries is
straightforward, but generating them for arbitrarily complicated
XPath queries mapped to SQL is quite difficult. If you permit axes
like "ancestor-or-self" and "parent", or have any query that
returns a node over and over again, it's best to instantiate that
node only once in order to keep the number of instantiated
objects low and to more easily test relationships between nodes.
It's all awkward if not particularly difficult.
You have templates, they contain general layout stuff, and input
fields. You are transforming them into HTML pages, and part of what you
want to do is to expand the enumerated fields with the enumeration
values at the time of template application.

You read an article about XSLT, and decided that you could use it in
your app. For some unknown reason, perhaps involving large amounts of
alcohol, you want to use the syntax of XSLT, but have it actually
parsed by python, which silently does an SQL querry to give you your
results.
I don't know what it is with comp.lang.python/python-list these days
and the cheap put-downs. Unless you know the person you're responding
to personally, and thus the above counts as some kind of banter, you
would do better to keep the insults to yourself.
This is a really, really bad idea. There are many ways to solve your
problem using XSLT, and though all of them are easy, none of them are
so simple that I could put them in a post to someone who doesn't know
XSLT. If you know XSLT, follow the directions below, if you don't, DO
NOT USE XSLT for this project. And for the love of anything you may
believe in, don't try to implement a partial XSLT engine in python.


Why not? 4XSLT is in Python, or at least large parts of it were.
Moreover, my XPath query experiments used a variant of PyXML's XPath
library which could quite probably have been integrated with 4XSLT,
although I didn't try it at the time.

[Patterns]

Anyway, I'm not suggesting that the XPath-to-SQL route is the right
way. The various patterns you suggest are easier to comprehend and
require a lot less magic, even if there's a bit more integration to be
done to prepare data for use by the templates. But given the existence
of XML database systems, I don't think the idea of accessing them (or
things pretending to be like them) via XML technologies is outrageous
at all.

Paul

Feb 26 '06 #3
> I don't know what it is with comp.lang.python/python-list these days
and the cheap put-downs. Unless you know the person you're responding
to personally, and thus the above counts as some kind of banter, you
would do better to keep the insults to yourself.
You are completely right, I was overly familiar.

Let me restate my central thesis.

1) It is possible to do what he is asking to do.
2) It requires expert knowledge of XSLT to do it well, and then it
would still require a bit of work.
3) There are simpler ways to do it, both using and not using xslt, than
this.

You could also do something like this:

<options ...>
<sql-hack query="select name from servers">
<option><sql-value value="name"></option>
</sql-hack>
</options>

and then do an XPath search for sql-hack nodes, and process them with
your python.

If you are happy staying on the python ranch, you could even do this:

<options ...>
<python><![CDATA[
rows = sql('select names from servers')
for row in rows:
xml('<option>%s</option>' % row[0])
]]>
</python>
</options>

With this, you find the python sections, and exec them in a context
with sql defined to do queries against your db and xml defined to parse
the xml you give it, and emit it in place of the python element.

But given the existence of XML database systems,
I don't think the idea of accessing them (or things pretending to be like them)
via XML technologies is outrageous at all.


No, not outrageous. But this wasn't about quering XML database systems.
This was about implementing something which looked like, but did not
act like, XSLT. It's just bad design.

Feb 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ted | last post by:
Was wondering if XSLT alone is appropriate for the following situation. From XML, I'm creating a small website (around 50 pages) with pages that link to each other through a nav menu and a...
1
by: Duane Morin | last post by:
I fear this might be a FAQ but I can't figure out how to google for it. Recently we had a big bug where the source that an XSL file acted on changed out from under it. Result, the XSL matched...
0
by: BaKMaN | last post by:
I am using ASP/VBScript to produce a XML file based on a AccesDB query (using adPersistXML) and include a XSL, by itself this works nicely. Is it possible to combine 2 separate queries into one...
3
by: Gordon Moore | last post by:
Hi, I'm new to using xml/xslt and although I can create an xml document using the dataset.WriteXml statement, and I have created an xslt to transform the xml into the output I want, I have to...
0
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
4
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
7
by: Elhanan | last post by:
hi.. i'm trying to figure out if i can include an extenal file of xml inside xslt, what i need is a configuration file which xslt can query duing transformation, (we are talking about a rather...
1
by: sani723 | last post by:
i am fetching some data from SQL and filling it through the Adapterto in DataSet, and then passing that DataSet to the XmlDataDocument Object, like XmlDataDocument XMLDoc = new...
1
by: kadykedar | last post by:
I have following XML now i want to search XML using XSLT for Solution word which is in Title element. I want to use same functionality which is provided by Like % operator in SQL queries ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.