XSLT and JavaScript

Here should go questions about transforming XML with XSLT and FOP.
mightymax
Posts: 1

XSLT and JavaScript

Tue May 02, 2017 6:31 pm

I'm having trouble using JavaScript with XSLT. I have a JavaScript function to parse an XML string into a certain format. This works http://js.do/MHammett/applic-xmlparser Now I would like to incorporate this function into a style sheet to run on XML files. This unfortunetly doesn't work. I'm having trouble outputting the functions results. My script uses document.write, this works in my test case, but not in the style sheet.

Any help you can provide would be greatly appreciated.

XML

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<IETM>
<datamodule><file>DMC-GAASIB0-00-00-10-00-00AAA-942B-A.xml</file><applic id="SERIAL_645_PRE"><displayText><simplePara>Test 1 Motorcycle without LED Light Assemblies</simplePara></displayText><evaluate andOr="and"><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-50"></assert><evaluate andOr="or"><assert applicPropertyIdent="SERIAL_Z432-645" applicPropertyType="condition" applicPropertyValues="PRE"></assert><assert applicPropertyIdent="SERIAL_Z432-645" applicPropertyType="condition" applicPropertyValues="SP"></assert></evaluate></evaluate></applic></datamodule>
<datamodule><file>DMC-GAASIB0-00-00-10-00-00AAA-942B-A.xml</file><applic id="Test1"><displayText><simplePara>Test 1 Motorcycle</simplePara></displayText><evaluate andOr="or"><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-50"></assert><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-52"></assert></evaluate></applic></datamodule>
<datamodule><file>DMC-GAASIB0-00-00-10-00-00AAA-942B-A.xml</file><applic id="SERIAL_720_PRE"><displayText><simplePara>Test 1 Motorcycle without Control Cable</simplePara></displayText><evaluate andOr="and"><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-50"></assert><evaluate andOr="or"><assert applicPropertyIdent="SERIAL_Z432-720" applicPropertyType="condition" applicPropertyValues="PRE"></assert><assert applicPropertyIdent="SERIAL_Z432-720" applicPropertyType="condition" applicPropertyValues="SP"></assert></evaluate></evaluate></applic></datamodule>
<datamodule><file>DMC-GAASIB0-00-00-10-00-00AAA-942B-A.xml</file><applic id="SERIAL_639_PRE"><displayText><simplePara>Test 1 Motorcycle without Fuel </simplePara></displayText><evaluate andOr="and"><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-50"></assert><evaluate andOr="or"><assert applicPropertyIdent="SERIAL_Z432-639" applicPropertyType="condition" applicPropertyValues="PRE"></assert><assert applicPropertyIdent="SERIAL_Z432-639" applicPropertyType="condition" applicPropertyValues="SP"></assert></evaluate></evaluate></applic></datamodule>
<datamodule><file>DMC-GAASIB0-00-00-10-00-00AAA-942B-A.xml</file><applic id="SERIAL_709_PRE_ALL"><displayText><simplePara>All Motorcycle</simplePara></displayText><evaluate andOr="or"><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-55"></assert><evaluate andOr="and"><evaluate andOr="or"><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-50"></assert><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-52"></assert></evaluate><evaluate andOr="or"><assert applicPropertyIdent="SERIAL_Z432-709" applicPropertyType="condition" applicPropertyValues="PRE"></assert><assert applicPropertyIdent="SERIAL_Z432-709" applicPropertyType="condition" applicPropertyValues="SP"></assert></evaluate></evaluate></evaluate></applic></datamodule>
<datamodule><file>DMC-GAASIB0-00-00-10-00-00AAA-942B-A.xml</file><applic id="SERIAL_709_PRE_Test_1"><displayText><simplePara>Test 1 Motorcycle without Extended right blinker</simplePara></displayText><evaluate andOr="and"><evaluate andOr="or"><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-50"></assert><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-52"></assert></evaluate><evaluate andOr="or"><assert applicPropertyIdent="SERIAL_Z432-709" applicPropertyType="condition" applicPropertyValues="PRE"></assert><assert applicPropertyIdent="SERIAL_Z432-709" applicPropertyType="condition" applicPropertyValues="SP"></assert></evaluate></evaluate></applic></datamodule>
<datamodule><file>DMC-GAASIB0-00-00-10-00-00AAA-942B-A.xml</file><applic id="SERIAL_731_PRE_Test_1"><displayText><simplePara>Test 1 Motorcycle without fuel injection</simplePara></displayText><evaluate andOr="and"><evaluate andOr="or"><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-50"></assert><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="NOVA-52"></assert></evaluate><evaluate andOr="or"><assert applicPropertyIdent="SERIAL_Z432-731" applicPropertyType="condition" applicPropertyValues="PRE"></assert><assert applicPropertyIdent="SERIAL_Z432-731" applicPropertyType="condition" applicPropertyValues="SP"></assert></evaluate></evaluate></applic></datamodule>
</IETM>


XSLT

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:strip-space elements="*"/>
   
 <xsl:template name="applic-script">
     <script type="javascript">
         function parse(node) {
         return Array.from(node.children, child =>
         child.tagName === 'assert'
         ? child.getAttribute('applicPropertyIdent')
         + '="' + child.getAttribute('applicPropertyValues') + '"'
         : child.tagName === 'evaluate'
         ? '(' + parse(child) + ')'
         : null
         ).filter(Boolean).join(' ' + node.getAttribute('andOr') + ' ');
         }
         
         var sApplic = "<xsl:value-of select="normalize-space(/IETM/datamodule/applic)"/>";
         document.write(sApplic);
         
         const xml = ( new window.DOMParser() ).parseFromString(sApplic, "text/xml");
         const result = parse(xml.documentElement);
         
         document.write(result);
       
     </script>
     
 </xsl:template>

 
   
    <xsl:template match="applic">
        <div><xsl:apply-templates></xsl:apply-templates></div>
       
        <xsl:call-template name="applic-script"/>
    </xsl:template>
</xsl:stylesheet>
Radu
Posts: 5090

Re: XSLT and JavaScript

Wed May 03, 2017 8:09 am

Hi,

I'm not sure I understand. Did you assume that from XSLT you can call Javascript code? As far as I know this is not possible.
Usually the XSLT script is applied on the XML document and this results in an HTML document which may contain embedded Javascript content.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “XSLT and FOP”

Who is online

Users browsing this forum: Bing [Bot] and 0 guests