XSLT with database querying
I've been doing XML transformation for a while in my Java application and I'm still not clear what the options are and what the best option is to read records from a database and insert it in my transformed output file.
What I've been doing so far is querying an Oracle database with xQuery that gave me a nodeset as a result. I then passed this result as a parameter to the transformer and开发者_如何学Python queried that parameter during the transformation to insert data into the appropriate nodes.
Is this the best way of doing it though? My base language again is Java. What would be the other options to get the same result?
Also I think it's worth mentioning that most of the times the db query is based on the content of the source XML file.
Thanks
Have you looked at the Saxon SQL extensions, in particular at <sql:query>
extension element?
One possibility is to instantiate a Java object in your XSLT script;
<!-- Connection to the data provider. -->
<xsl:variable name="provider" xmlns:java="http://xml.apache.org/xalan/java"
select="java:my.sample.DataProvider.getInstance()" />
Using it to provide the data later on in the script:
<xsl:template match="node">
<xsl:variable name="mydata" xmlns:java="http://xml.apache.org/xalan/java"
select="java:getdata($provider, string(@attr))" />
This would call the method getData(String)
on the object created by the static getInstance()
method on your my.sample.DataProvider
class.
You can use a setup like this to get values from a cache (for instance the query results you try to pass as parameter in your current setup), or to execute queries while transform is executed (preventing queries to data that is not visited by the transform.)
精彩评论