Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 31st, 2007, 10:56 AM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default JavaScript in XSLT

Hi,

I was trying to embed JavaScript in XSLT but could not succeed. It shows "Function not in the namespace Error in XPath expression. Function not in namespace". But the function is in the namespace. Please help me.. Here is the code.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:lxslt="http://xml.apache.org/xslt"
                xmlns:result="http://www.example.com/results"
                extension-element-prefixes="result"
                version="1.0">
  <lxslt:component prefix="result" elements="rules" functions="getResult">
    <lxslt:script lang="javascript">
        function getResult (thisGuess) {
          var thisResult = parseInt(Math.random()*100);
          if (thisResult == parseInt(thisGuess)) {
             return "Correct!";
          } else {
             return "Wrong! The actual answer was "+thisResult ", not "+thisGuess+".";
          }
        }
    </lxslt:script>
  </lxslt:component>
  <xsl:template match="/">
      <xsl:apply-templates/>
  </xsl:template>
<xsl:template match="entry">
      Guesser: <xsl:value-of select="player"/>
      <xsl:apply-templates select="guess"/>
  </xsl:template>
  <xsl:template match="guess">
        Guess: <xsl:value-of select="."/>
          Actual: <xsl:value-of select="result:getResult(string(.))"/>
  </xsl:template>
</xsl:stylesheet>

Thanks in Advance..

Regards,
Jagadeesh



 
Old July 31st, 2007, 11:21 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You are using facilities that are specific to the Apache Xalan product, and I think you would be best off looking for help on a list specific to that product.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 31st, 2007, 12:10 PM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

You can use <script>

e.g.

<script type="text/javascript">
var temp, temp2, cookieArray, cookieArray2, cookieCount;
function initiate(){
  cookieCount=0;
  if(document.cookie){
    cookieArray=document.cookie.split(";");
    cookieArray2=new Array();
    for(i in cookieArray){
      cookieArray2[cookieArray[i].split("=")[0].replace(/ /g,"")]=cookieArray[i].split("=")[1].replace(/ /g,"");
    }
  }
</script>



 
Old July 31st, 2007, 12:24 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

> You can use <script>

Oh no you can't.

He's trying to call this function from within an XPath expression in the stylesheet: <xsl:value-of select="result:getResult(string(.))"/>

<script> just generates a script element in the output file. It might be executable when the output HTML is rendered by the browser, but it certainly can't be invoked during the XSLT transformation.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 31st, 2007, 02:22 PM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Could you please suggest me some tutorial for javascript embedment in XSLT. I am actually using Altova XMLSPY to test it and run the project using xalan. Please give me atleast a sample code that will run in Altova XMLSPY. I could not find a proper tutorial.

Thank you very much,

Regards,
Jagadeesh

 
Old July 31st, 2007, 03:06 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Extension functions are done differently in different processors. I'm not familiar in depth with either Xalan or Altova and neither has experts answering questions on this forum. It's very unlikely that you can use Javascript extensions in a way that is portable between these two processors. Perhaps you should be looking at whether you really need Javascript or whether there is a better solution to your requirement.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 31st, 2007, 04:22 PM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

I may have misunderstood the question.

I understand that it can't be invoked during the XSLT transformation however, can you can dynamic write the javascript with xslt

Could this be possible for <xsl:value-of select="result:getResult(string(.))"/>

<xsl:text>result:getResult(</xsl:text>
     <xsl:value-of select="string(.)"/>
<xsl:text>)</xsl:text>

I ask because I do this often:

e.g.

Code:
<script language="JavaScript" type="text/JavaScript">
function CreateProjectExplorer()
{
    Initialise();

    // Build a test project.

    d2 = CreateTreeItem( d, "img/text.gif", "img/text.gif", "List of Effective revisions", "./Lep.html", "main" );
    d =CreateTreeItem( rootCell, "img/folder_closed.gif", "img/folder_open.gif", "LER", "./Lep.html", "main" );

<xsl:for-each select="$XML1/manual/chapter">
                        <xsl:text>d = CreateTreeItem( rootCell, "img/project.gif", "img/folder_open.gif", "</xsl:text>
                        <xsl:value-of select="@chaptitle"/>
                        <xsl:text>", null, null )</xsl:text>;
                        <xsl:for-each select="title">
                            <xsl:text>d2 = CreateTreeItem(d, "img/page.gif", "img/page.gif", "</xsl:text>
                            <xsl:value-of select="document(.)//title"/>
                            <xsl:text>", "</xsl:text>
                            <xsl:value-of select="substring-before(., '-')"/>/<xsl:value-of select="substring-before(., '.')"/>
                            <xsl:text>.html</xsl:text>
                            <xsl:text>" , "main" )</xsl:text>;
                            </xsl:for-each>
</xsl:for-each>


}
</script>
Its possible I am still misunderstanding the question. I often reply and respond to a post to learn more.

Bones


 
Old July 31st, 2007, 04:35 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There is a world of difference between calling script at the time you do a transformation and generating script in the output HTML page. The difference is most obvious if the transformation is done on the server, perhaps at the time the document is published rather than at the time it is requested by the end user. When the transformation is done in the browser there may be cases where the same functionality can be achieved, but it's still essential to understand the difference - for example, transformation-time script can never interact with the user.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 31st, 2007, 06:10 PM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

As always, thank you.







Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT and JavaScript Variable kwilliams XSLT 5 April 21st, 2008 12:05 PM
using javascript with xslt chocolate31 XSLT 3 February 27th, 2007 02:02 PM
javascript 'for' loop in xslt mummra1 XSLT 22 February 6th, 2007 04:26 PM
javascript function in xslt swapnilp1983 XSLT 16 November 7th, 2006 12:39 AM
XSLT + Javascript errors braindog_43 XSLT 0 February 3rd, 2005 09:04 AM





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