Re: [xsl] [ XSLT ] Dynamically Transform xml pages to database queries

Subject: Re: [xsl] [ XSLT ] Dynamically Transform xml pages to database queries
From: Raffaele Sena <raff@xxxxxxxxxxxx>
Date: Sat, 20 May 2006 17:45:11 -0700
One thing that you can do is to match all elements (and attributes if you want) and decide what to do depending on their name.

You may have to scan the document twice, once for the column names and once for the column values. You can do this by passing a "mode" to your templates. Something like:

<xsl:template match="/">
  insert (
    <xsl:apply-templates mode="column-names"/>
  ) values (
    <xsl:apply-templates mode="column-values"/>

<xsl:template match="*" mode="column-names">
  <!-- match elements for column names -->

<xsl:template match="*' mode="column-values">

And you can use the "name()" function to get the name of the current element.

This doesn't seem to me to be the best use of XSLT. Given the nature of the problem I suspect that using a simple SAX parser and writing your logic in the event handler that you provide it may be easier and more efficient.

-- Raffaele

Jyotsna B wrote:
Thanks for the response .
The languagae I am using is JAVA.

Solution suggested works fine for getting values
replaced in query from XML file

But in my project the condition is NOT to hardcode
even the first part of query .Query need to be
generated "dynamically" from XML 'elements' and
'attribute' names .

Because the XML files I receive are not same pattern
They always are different from eachother with element
and attribute strcuture .

The database table name amd column names are NOT known
ahead of time to mention in template <xsl:template >.I
need look at XML file elements and attribute names

Questions :-
How to get this First part of the Query from XSLT with
unknown element & attribute info of XML files to map
to XSLT template definition ?
Please suggest if below 2 points are possible and best
way to achieve this ?
insert into server(not always same tablename "server"
- its diff name for each xml file) (
'oracleAppVersion'not always "oracleAppVersion" -
its diff attribute depending on element for each xml

1)Element Name in XML (0 - Unbounded limit becomes -> Table Name in output query (CANNOT hardcode table name a head of time in XSLT)

2)Attribute Name under above element becomes Column
Name in output query dynamically (CANNOT hardcode
column names a head of time in XSLT)

I can give me more explaination if needed.
Appreciate your time

--- Raffaele Sena <raff@xxxxxxxxxxxx> wrote:


you don't mention which language you want to use to
write write your application. I am assuming is Java but the answer
should apply to any language for which you have an XSLT engine.

1) how to read each file from a folder ?
Use the filesystem API. XSLT cannot help you with
Once you have the file name you can read your
document into a DOM or even better use SAX to stream it to the XSLT

2) how to write a template that generate text:
<xsl:output method="text"/>
<xsl:template match="/config/server">
insert into server (


 ) values (
  <xsl:value-of select="company/server-version"/>,
  <xsl:value-of select="java/version"/>,
  <xsl:value-of select="java/vendor"/>,
  <xsl:value-of select="java/vm/version"/>,
  <xsl:value-of select="java/vm/vendor"/>,
  <xsl:value-of select="java/vm/name"/>,
  <xsl:value-of select="os/arch"/>,
  <xsl:value-of select="os/name"/>,
  <xsl:value-of select="os/version"/>

Note that I left out the namespace (I am lazy) so if
you try this as-is your output will be empty.

I suggest you start with a command line XSLT
processor (or use XMLSpy) and a static xml file. When you get that to work you
can think about writing a program that runs the conversion.

Hope this helps,

-- Raffaele

Jyotsna B wrote:

Hi All,

I am NEWbie to XSLT and in process of learning.Any
code suggestions or direction is greatly


I have a list of XML files at certain location


D:\xmlfilesloc )
I need to write a program which checks the folder
automatically and if files exists -reads each xml


at a time and transforms the xml file (using XSLT

) to

bunch of customized oracle database queries .

Questions :
1)How to read each xml file from a specified


dynamically ?
2)How to use XSLT to transform a xml file
dynamically/automatically to bunch of customized
database queries (elements to table name mapping ;
attributes to columns names ;...etc )?

Ex Output I am trying to acheive is queries :- -------------------------------------------
insert into server





I tried using XMLSpy feature "Create DB structure


XML Schema" but its not helping to customize the output


want to code myself the XSLT transformation to get
desired output queries

Example of Input XML file :-
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2006 sp1


<config xmlns="";




<domain-name>oracle appserver</domain-name>
<server is-admin-server="false" name="String">

--------------------------- configdetail.xsd - XSD Schema that it macthes ------------ <?xml version="1.0" encoding="UTF-8"?> <xs:schema


xmlns=""; xmlns:inv="";
<xs:import namespace="";
<xs:complexType name="configdetailtype">
<xs:extension base="cfg:ConfigmainType">
<xs:element name="server" minOccurs="0"
<xs:element name="oracleAppVersion"
<xs:element name="java">
   <xs:element name="version" type="xs:string"/>
   <xs:element name="vendor" type="xs:string"/>
   <xs:element name="vm">
      <xs:element name="version"


<xs:element name="vendor"


      <xs:element name="name" type="xs:string"/>
<xs:element name="os">
   <xs:element name="arch" type="xs:string"/>
   <xs:element name="name" type="xs:string"/>
   <xs:element name="version" type="xs:string"/>
<xs:attribute name="name" type="xs:string"
<xs:attribute name="adminServer" type="xs:boolean"

I can give me more explaination if needed.
Thanks a lot for your response

=== message truncated ===

Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around


Current Thread