 |
| 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
|
|
|
|

July 31st, 2007, 10:56 AM
|
|
Registered User
|
|
Join Date: Jan 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 31st, 2007, 11:21 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

July 31st, 2007, 12:10 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
|
|
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>
|
|

July 31st, 2007, 12:24 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
> 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
|
|

July 31st, 2007, 02:22 PM
|
|
Registered User
|
|
Join Date: Jan 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 31st, 2007, 03:06 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

July 31st, 2007, 04:22 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
|
|
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
|
|

July 31st, 2007, 04:35 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

July 31st, 2007, 06:10 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
|
|
As always, thank you.
|
|
 |