Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 28th, 2009, 11:11 AM
Registered User
 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Store Javascript return values in XSL variable

I have a XSL file which has Javascript function which does XSL Transformation of data retrieved from DB, and pass it to a variable in XSL.

When I try to call the javascript function BuildXMLFieldAttributes from inside a xsl:variable, LabelStyleAttributes it works in IE but not in Firefox or Safari.

How to call a javascript function from inside a XSL:Variable and store the return value? Is it possible or not?

Any help on this will be very much appriciated. Desperately in need of help. Sample code is given below for your reference.

Sample XSL file:

<?xml version='1.0' encoding='utf-8' ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:js="urn:the-xml-files:xslt">

<ms:script language="Javascript" implements-prefix="js" >

<![CDATA[

function BuildXMLFieldAttributes(xmlField,strXMLAttributes)

{

//var XMLAttributes = new ActiveXObject("Microsoft.XMLDOM"); //For IE

//XMLAttributes.loadXML(strXMLAttributes); //For IE

var XMLAttributes = new XMLHttpRequest(); //For Firefox or Safari

var oParser = new DOMParser();

XMLAttributes = oParser.parseFromString("strXMLAttributes", "text/xml");

return XMLAttributes;

}

]]>

</ms:script>

<xsl:output indent="yes"/>
<xsl:variable name="Components" select="Components/Component/row"></xsl:variable>

<xsl:template match="/">

<xsl:for-each select="$Components">

<xsl:variable name="LabelStyleAttributes" select="js:BuildXMLFieldAttributes(current(),strin g(@LabelStyles))"/>

<div class="tabletxt" style="height:150px;border:solid 1px black;">

<xsl:call-template name="BuildComponent"/>

</div>

</xsl:for-each>

</xsl:template>

<xsl:template name="BuildComponent">

<div id="lbl_{@FieldID}_{$lblCounterIdx}" nowrap='' class="tabletxt" style="position:absolute;width:{@iLabelWidth}px;he ight:{@iLabelHeight}px;top:{@iLabelTop}px;left:{@i LabelLeft}px;">

<xsl:element name="LABEL">

<xsl:attribute name="id">

<xsl:value-of select="concat('lbl_',@FieldID)"/>

</xsl:attribute>

<xsl:call-template name="ApplyAttributes">

<xsl:with-param name="Attributes" select="$LabelStyleAttributes/Attribute"/>

</xsl:call-template>

<xsl:value-of select="@iLabelDesc"/>

</xsl:element>

</div>

</xsl:template>

<xsl:template name="ApplyAttributes">

<xsl:param name="Attributes"/>

<xsl:for-each select="$Attributes">

<xsl:attribute name="{@Name}">

<xsl:value-of select="@Value"/>

</xsl:attribute>

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

Sample XML file:

<?xml version="1.0" encoding="UTF-16"?>

<Components>
<Component>
<row TableName="LNAPPRSEARCH" FieldID="3608" FieldName="Loan Amount" FieldValue="" DescName="Loan Amount" iLeft="556" iTop="271" iHeight="50" iWidth="155" TypeTableUse="0" iControlType="0" iGroupId="0" GroupName="Loan Amount" iLabelDisplay="1" iLabelDesc="Loan Amount" iLabelLeft="362" iLabelTop="196" iLabelWidth="155" iLabelHeight="50" GrpIdentity="0" iCompTypeId="0" iReadonly="0" DataType="Float" iValidateType="0" blVerify="0" iRights="2" RecId="0" LabelStyles="&lt;Attribute Name=&quot;Style&quot; Value=&quot;FONT-FAMILY:Garamond; FONT-SIZE:15;&quot; /&gt;" LoanId="164857" />
</Component>
</Components>
 
Old July 28th, 2009, 11:30 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Note at the very start of your XSLT stylesheet:

xmlns:ms="urn:schemas-microsoft-com:xslt"
Does this not give you a hint as to why it works in Microsoft Internet Explorer, but not in Firefox or Safari? It is because you are using a proprietary Microsoft technology to run the Javascript, which does not exist in Firefox or Safari.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 28th, 2009, 11:31 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The use of Javascript to implement extension functions in an XSLT stylesheet is to my knowledge only supported by IE respectively MSXML, not by the XSLT processor Firefox uses.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old July 29th, 2009, 02:14 AM
Registered User
 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your time... Samjudson and Martin.

Samjudson :
Can you please let me know which namespace should I use to get this working in Firefox and Safari.

Martin:
If javascript implementation of extension functions in a XSLT is not supported then what will be the best approach I can take...

Is it possible to call external JS function from inside XSLT to achieve the same...If so can you give me an example...

Please let me know the best approach I can take...Iam pretty new to Firefox and Safari browsers...

Thanks again for your time.
 
Old July 29th, 2009, 06:12 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I am afraid there is not really a solution with pure XSLT 1.0 and Mozilla browsers. You can't call Javascript functions from XSLT stylesheets with Mozilla/Firefox. Sometimes people use Javascript to achieve things that can be achieved (with some efforts) in pure XSLT 1.0 but what you seem to want to do, namely parsing some markup into a tree of nodes, is not something I would suggest to use pure XSLT 1.0 for. I would consider to process the XML you have outside of the browser to use more powerful tools to bring it into a form that can be processed with browsers or XSLT 1.0.

As for Safari, I have never heard that it allows you to call Javascript extension functions from XSLT but I have never investigated that. I think Safari uses libxslt as its XSLT processor and I am not very familiar with that either.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to store XML text in a javascript variable? napstrpsx XSLT 3 March 31st, 2009 03:54 AM
How to reteive values from the table and store them in a variable bex ASP.NET 2.0 Basics 2 February 27th, 2009 06:03 AM
Set xsl variable using Javascript variable Kunn XSLT 5 December 31st, 2008 02:43 AM
Javascript Function return save in XSL Variable Vrokad XSLT 2 June 2nd, 2007 02:54 AM
how to return nodeset in template in xsl:variable alexshiell XSLT 3 March 18th, 2005 01:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.