📜 ⬆️ ⬇️

Add Javascript to XSLT

Hello dear habrazhiteli!
I would like to present you my small project which I think may be useful to many of you. XSR is an extension for the Saxon XSLT processor that allows you to use JavaScript directly in the XSLT program code. So what we have:
So let's proceed directly to the installation and use, in order to show all the living examples. For a start, you can get acquainted with the main features of my crafts on this page: Description (eng.) .

Briefly about installation and configuration

To use the extension, you will need to add the following namespace definition to the root element of the XSLT document:
xmlns:xsr="java:/xsr.XSRElementFactory"

Then add extension-element-prefixes="xsr" to the same - i.e. to the list of root element attributes. This instruction will tell Saxon that the xsr prefix defines an external extension. In other words, after the above manipulations, the root element will look like this:
< xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
< xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
< xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .

Using the xsr: script statement


There are two ways to define and use xsr: script.
Firstly, you can use the href attribute to connect (and immediately execute) an external javascript file. Like this:
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- script defined in external file --> <br> < xsr:script href ="test.js" /> <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- script defined in external file --> <br> < xsr:script href ="test.js" /> <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- script defined in external file --> <br> < xsr:script href ="test.js" /> <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
This code will connect the external test.js file (which should be located relative to the location of the document) to your xsl document and execute it.

Secondly, you can write your JavaScript code directly in an XSLT document, in this simple way:
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- simple script --> <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> message('The Obligatory Hello World Example:');<br> message('HELLO WORLD!');<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- simple script --> <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> message('The Obligatory Hello World Example:');<br> message('HELLO WORLD!');<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- simple script --> <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> message('The Obligatory Hello World Example:');<br> message('HELLO WORLD!');<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .

Passing parameters to the script using xsr: with-param


In some cases, you may need to pass parameters declared from the outside into your JavaScript code. For these purposes, you can use a special instruction: xsr: with-param. Example:
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- simple script --> <br> < xsr:script > <br> < xsr:with-param name ="foo" select ="string('bar')" /> <br> < xsr:with-param name ="bar" select ="local-name(.)" /> <br> < xsr:body ><! [CDATA[<br> message('foo is:', getParam('foo'));<br> message('bar is:', getParam('bar'));<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- simple script --> <br> < xsr:script > <br> < xsr:with-param name ="foo" select ="string('bar')" /> <br> < xsr:with-param name ="bar" select ="local-name(.)" /> <br> < xsr:body ><! [CDATA[<br> message('foo is:', getParam('foo'));<br> message('bar is:', getParam('bar'));<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- simple script --> <br> < xsr:script > <br> < xsr:with-param name ="foo" select ="string('bar')" /> <br> < xsr:with-param name ="bar" select ="local-name(.)" /> <br> < xsr:body ><! [CDATA[<br> message('foo is:', getParam('foo'));<br> message('bar is:', getParam('bar'));<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
Attention! At the moment, everything that was transferred to the script in such a described manner will be automatically converted into a string, so transferring the fragments of the XML document in this way will not work.
')

Using xsr: body with XSL elements


You can mix JavaScript and XSLT by creating a situation when your JavaScript code is generated on the fly in this way:
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- script generate itself --> <br> < xsr:script > <br> < xsr:body > <br> message('\r');<br> message('Script generate itself:');<br> < xsl:for-each select ="1 to 5" > <br> message('position() = ' + < xsl:value-of select ="position()" /> );<br> </ xsl:for-each > <br> </ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- script generate itself --> <br> < xsr:script > <br> < xsr:body > <br> message('\r');<br> message('Script generate itself:');<br> < xsl:for-each select ="1 to 5" > <br> message('position() = ' + < xsl:value-of select ="position()" /> );<br> </ xsl:for-each > <br> </ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- script generate itself --> <br> < xsr:script > <br> < xsr:body > <br> message('\r');<br> message('Script generate itself:');<br> < xsl:for-each select ="1 to 5" > <br> message('position() = ' + < xsl:value-of select ="position()" /> );<br> </ xsl:for-each > <br> </ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .

Using JavaScript to generate the resulting XML document


Use the following example to generate the resulting XML document using JavaScript:
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- script that produces simple XML document as a result --> <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> message();<br> startElement('test');<br> for (var c = 0; c < 10; c++) {<br> startElement('item_' + c);<br> characters('value_' + c);<br> endElement();<br> }<br> endElement();<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- script that produces simple XML document as a result --> <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> message();<br> startElement('test');<br> for (var c = 0; c < 10; c++) {<br> startElement('item_' + c);<br> characters('value_' + c);<br> endElement();<br> }<br> endElement();<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> <!-- script that produces simple XML document as a result --> <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> message();<br> startElement('test');<br> for (var c = 0; c < 10; c++) {<br> startElement('item_' + c);<br> characters('value_' + c);<br> endElement();<br> }<br> endElement();<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
Generated XML document:
< test > <br> < item_0 > value_0 </ item_0 > <br> < item_1 > value_1 </ item_1 > <br> < item_2 > value_2 </ item_2 > <br> < item_3 > value_3 </ item_3 > <br> < item_4 > value_4 </ item_4 > <br> < item_5 > value_5 </ item_5 > <br> < item_6 > value_6 </ item_6 > <br> < item_7 > value_7 </ item_7 > <br> < item_8 > value_8 </ item_8 > <br> < item_9 > value_9 </ item_9 > <br> </ test > <br><br> * This source code was highlighted with Source Code Highlighter .
< test > <br> < item_0 > value_0 </ item_0 > <br> < item_1 > value_1 </ item_1 > <br> < item_2 > value_2 </ item_2 > <br> < item_3 > value_3 </ item_3 > <br> < item_4 > value_4 </ item_4 > <br> < item_5 > value_5 </ item_5 > <br> < item_6 > value_6 </ item_6 > <br> < item_7 > value_7 </ item_7 > <br> < item_8 > value_8 </ item_8 > <br> < item_9 > value_9 </ item_9 > <br> </ test > <br><br> * This source code was highlighted with Source Code Highlighter .
< test > <br> < item_0 > value_0 </ item_0 > <br> < item_1 > value_1 </ item_1 > <br> < item_2 > value_2 </ item_2 > <br> < item_3 > value_3 </ item_3 > <br> < item_4 > value_4 </ item_4 > <br> < item_5 > value_5 </ item_5 > <br> < item_6 > value_6 </ item_6 > <br> < item_7 > value_7 </ item_7 > <br> < item_8 > value_8 </ item_8 > <br> < item_9 > value_9 </ item_9 > <br> </ test > <br><br> * This source code was highlighted with Source Code Highlighter .

Using Java classes in JavaScript code


You can use any Java classes in your JavaScript code, as follows:
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> function buildDirectoryTree(sRootDirectory, sTrimDirectory) {<br> var aResultTree = [];<br> var aFiles = (new Packages.java.io.File(sRootDirectory)).listFiles();<br> for (var iFile = 0; iFile < aFiles.length; iFile++) {<br> var sFileName = String(aFiles[iFile]);<br> var bIsDirectory = aFiles[iFile].isDirectory();<br> if (!bIsDirectory) {<br> aResultTree.push(sFileName.substr((<br> sTrimDirectory ?<br> sTrimDirectory :<br> sRootDirectory<br> ).length));<br> } else {<br> aResultTree = aResultTree.concat(<br> buildDirectoryTree(<br> sFileName,<br> sRootDirectory<br> )<br> );<br> }<br> }<br> return aResultTree;<br> }<br> // Build directory tree<br> var aDirectoryTree = buildDirectoryTree('.');<br> for (var c = 0; c < aDirectoryTree.length; c++) {<br> message('Found directory:', aDirectoryTree[c]);<br> }<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> function buildDirectoryTree(sRootDirectory, sTrimDirectory) {<br> var aResultTree = [];<br> var aFiles = (new Packages.java.io.File(sRootDirectory)).listFiles();<br> for (var iFile = 0; iFile < aFiles.length; iFile++) {<br> var sFileName = String(aFiles[iFile]);<br> var bIsDirectory = aFiles[iFile].isDirectory();<br> if (!bIsDirectory) {<br> aResultTree.push(sFileName.substr((<br> sTrimDirectory ?<br> sTrimDirectory :<br> sRootDirectory<br> ).length));<br> } else {<br> aResultTree = aResultTree.concat(<br> buildDirectoryTree(<br> sFileName,<br> sRootDirectory<br> )<br> );<br> }<br> }<br> return aResultTree;<br> }<br> // Build directory tree<br> var aDirectoryTree = buildDirectoryTree('.');<br> for (var c = 0; c < aDirectoryTree.length; c++) {<br> message('Found directory:', aDirectoryTree[c]);<br> }<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> function buildDirectoryTree(sRootDirectory, sTrimDirectory) {<br> var aResultTree = [];<br> var aFiles = (new Packages.java.io.File(sRootDirectory)).listFiles();<br> for (var iFile = 0; iFile < aFiles.length; iFile++) {<br> var sFileName = String(aFiles[iFile]);<br> var bIsDirectory = aFiles[iFile].isDirectory();<br> if (!bIsDirectory) {<br> aResultTree.push(sFileName.substr((<br> sTrimDirectory ?<br> sTrimDirectory :<br> sRootDirectory<br> ).length));<br> } else {<br> aResultTree = aResultTree.concat(<br> buildDirectoryTree(<br> sFileName,<br> sRootDirectory<br> )<br> );<br> }<br> }<br> return aResultTree;<br> }<br> // Build directory tree<br> var aDirectoryTree = buildDirectoryTree('.');<br> for (var c = 0; c < aDirectoryTree.length; c++) {<br> message('Found directory:', aDirectoryTree[c]);<br> }<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
You can also use classes that are not a standard part of Java. The following example shows how you can use SQLiteJDBC (http://www.zentus.com/sqlitejdbc/) to connect and work with a SQLite database:
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> function SQLite() {<br> Packages.java.lang.Class.forName('org.sqlite.JDBC');<br> this.connection = null;<br> this.connect = function(sDBFileName) {<br> this.connection = new Packages.java.sql.DriverManager.getConnection(<br> 'jdbc:sqlite:' + sDBFileName<br> );<br> this.statement = this.connection.createStatement();<br> };<br> this.close = function() {<br> if (!this.connection) return;<br> this.connection.close();<br> this.statement.close();<br> this.connection = null;<br> this.statement = null;<br> };<br> this.query = function(sQuery) {<br> if (!this.statement) return;<br> var aResultSet = [];<br> try {<br> var oResultSet = this.statement.executeQuery(sQuery);<br> } catch (e) {}<br> if (oResultSet) {<br> while (oResultSet.next()) {<br> var aRowData = {};<br> for (var c = 1; c < = oResultSet.columnCount; c++) {<br> var sColumnName = oResultSet.getColumnName(c);<br> aRowData[sColumnName] = oResultSet.getString(<br> sColumnName<br> );<br> }<br> aResultSet.push(aRowData);<br> }<br> oResultSet.close();<br> }<br> return aResultSet;<br> };<br> };<br> ]] ></ xsr:body > <br> </ xsr:script > <br><br> <!-- let's do something interesting with our database now --> <br><br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> message('SQLite example:\r\n');<br> var oSQLite = new SQLite();<br> oSQLite.connect('test.db');<br> oSQLite.query('\<br> CREATE TABLE if not exists test_table (\<br> id INTEGER PRIMARY KEY AUTOINCREMENT,\<br> name TEXT\<br> );\<br> ');<br> for (var c = 0; c < 100; c++) {<br> oSQLite.query('INSERT INTO test_table (name) VALUES ("item_'+c+'")');<br> }<br> var aResult = oSQLite.query('SELECT * FROM test_table;');<br> for (var c = 0; c < aResult.length; c++) {<br> startElement('row');<br> for (var sCellName in aResult[c]) {<br> startElement(sCellName);<br> characters(aResult[c][sCellName]);<br> endElement();<br> }<br> endElement();<br> }<br> oSQLite.query('DROP TABLE test_table');<br> oSQLite.close();<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> function SQLite() {<br> Packages.java.lang.Class.forName('org.sqlite.JDBC');<br> this.connection = null;<br> this.connect = function(sDBFileName) {<br> this.connection = new Packages.java.sql.DriverManager.getConnection(<br> 'jdbc:sqlite:' + sDBFileName<br> );<br> this.statement = this.connection.createStatement();<br> };<br> this.close = function() {<br> if (!this.connection) return;<br> this.connection.close();<br> this.statement.close();<br> this.connection = null;<br> this.statement = null;<br> };<br> this.query = function(sQuery) {<br> if (!this.statement) return;<br> var aResultSet = [];<br> try {<br> var oResultSet = this.statement.executeQuery(sQuery);<br> } catch (e) {}<br> if (oResultSet) {<br> while (oResultSet.next()) {<br> var aRowData = {};<br> for (var c = 1; c < = oResultSet.columnCount; c++) {<br> var sColumnName = oResultSet.getColumnName(c);<br> aRowData[sColumnName] = oResultSet.getString(<br> sColumnName<br> );<br> }<br> aResultSet.push(aRowData);<br> }<br> oResultSet.close();<br> }<br> return aResultSet;<br> };<br> };<br> ]] ></ xsr:body > <br> </ xsr:script > <br><br> <!-- let's do something interesting with our database now --> <br><br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> message('SQLite example:\r\n');<br> var oSQLite = new SQLite();<br> oSQLite.connect('test.db');<br> oSQLite.query('\<br> CREATE TABLE if not exists test_table (\<br> id INTEGER PRIMARY KEY AUTOINCREMENT,\<br> name TEXT\<br> );\<br> ');<br> for (var c = 0; c < 100; c++) {<br> oSQLite.query('INSERT INTO test_table (name) VALUES ("item_'+c+'")');<br> }<br> var aResult = oSQLite.query('SELECT * FROM test_table;');<br> for (var c = 0; c < aResult.length; c++) {<br> startElement('row');<br> for (var sCellName in aResult[c]) {<br> startElement(sCellName);<br> characters(aResult[c][sCellName]);<br> endElement();<br> }<br> endElement();<br> }<br> oSQLite.query('DROP TABLE test_table');<br> oSQLite.close();<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > <br> < xsl:stylesheet version ="2.0" <br> xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" <br> xmlns:xsr ="java:/xsr.XSRElementFactory" <br> extension-element-prefixes ="xsr" > <br><br> < xsl:output method ="xml" indent ="yes" omit-xml-declaration ="yes" /> <br><br> < xsl:template match ="//*" > <br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> function SQLite() {<br> Packages.java.lang.Class.forName('org.sqlite.JDBC');<br> this.connection = null;<br> this.connect = function(sDBFileName) {<br> this.connection = new Packages.java.sql.DriverManager.getConnection(<br> 'jdbc:sqlite:' + sDBFileName<br> );<br> this.statement = this.connection.createStatement();<br> };<br> this.close = function() {<br> if (!this.connection) return;<br> this.connection.close();<br> this.statement.close();<br> this.connection = null;<br> this.statement = null;<br> };<br> this.query = function(sQuery) {<br> if (!this.statement) return;<br> var aResultSet = [];<br> try {<br> var oResultSet = this.statement.executeQuery(sQuery);<br> } catch (e) {}<br> if (oResultSet) {<br> while (oResultSet.next()) {<br> var aRowData = {};<br> for (var c = 1; c < = oResultSet.columnCount; c++) {<br> var sColumnName = oResultSet.getColumnName(c);<br> aRowData[sColumnName] = oResultSet.getString(<br> sColumnName<br> );<br> }<br> aResultSet.push(aRowData);<br> }<br> oResultSet.close();<br> }<br> return aResultSet;<br> };<br> };<br> ]] ></ xsr:body > <br> </ xsr:script > <br><br> <!-- let's do something interesting with our database now --> <br><br> < xsr:script > <br> < xsr:body ><! [CDATA[<br> message('SQLite example:\r\n');<br> var oSQLite = new SQLite();<br> oSQLite.connect('test.db');<br> oSQLite.query('\<br> CREATE TABLE if not exists test_table (\<br> id INTEGER PRIMARY KEY AUTOINCREMENT,\<br> name TEXT\<br> );\<br> ');<br> for (var c = 0; c < 100; c++) {<br> oSQLite.query('INSERT INTO test_table (name) VALUES ("item_'+c+'")');<br> }<br> var aResult = oSQLite.query('SELECT * FROM test_table;');<br> for (var c = 0; c < aResult.length; c++) {<br> startElement('row');<br> for (var sCellName in aResult[c]) {<br> startElement(sCellName);<br> characters(aResult[c][sCellName]);<br> endElement();<br> }<br> endElement();<br> }<br> oSQLite.query('DROP TABLE test_table');<br> oSQLite.close();<br> ]] ></ xsr:body > <br> </ xsr:script > <br> </ xsl:template > <br> </ xsl:stylesheet > <br><br> * This source code was highlighted with Source Code Highlighter .

Conclusion


You may wonder why all this is necessary? The answer is very simple, XSLT together with XML is a very powerful bundle that can perform very many tasks, adding JavaScript and the ability to easily run from the command line using Apache Ant, we get a powerful automation tool with an already built-in mechanism for generating the final result . Given the fact that for JavaScript, tons of code are now written that perform various application tasks, this project (in any case, to me) no longer seems so crazy.
A more realistic example: we need to write something so that we can tear off external resources using the HTTP protocol, do something with this text - and then drop the XML report into the database or to another server.

This is my first topic on Habré and therefore, any constructive criticism is welcome. I would also like to know from the community if you are interested in this topic and should I publish something like this in the near future?

Links

Source: https://habr.com/ru/post/82380/


All Articles