Solved

XSLT 1.0 -  <msxsl:script>

Posted on 2002-03-06
10
735 Views
Last Modified: 2013-11-19
In my XSLT 1.0 file, I'm trying to extract a value of a child element for placing it into the HTML <script> tag for initializing a javascript array(see code below). In WD-XSL,the "." or "this" value seems to be a node, and it's working with <xsl:eval>. In XSLT 1.0, can someone tells me what is the value of the "what" parameter in the <msxml:script> getNbOfRules(what) function? I always got errors on this function... What is different here between the old XSL call and the XSLT new one?

======================================

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:extra="urn:extra-functions"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>

<xsl:template match="/">

<html>
<head><title>Rules</title></head>
     
<script language="javascript">
     var status = new Array(<xsl:value-of select="extra:getNbOfRules(.)"/>);
</script>

...

</xsl:template>

<msxsl:script implements-prefix="extra" language="javascript">

<![CDATA[
function getNbOfRules(what)
{
var i = 50;
var modules = what.selectSingleNode("MODULES/NBOFRULES");
i = modules.nodeTypedValue;
         
return i;
}
]]>    
</msxsl:script>
</xsl:stylesheet>
0
Comment
Question by:mhebert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 6845601
Firstly, there has been considerable changes in coding technique between XSL and XSLT. In the old XSL, we had to use the xsl:eval and xsl:script many times to do complicated stuff. In those days,we had to make direct calls to the XSLRuntime Engine such as <xsl:eval>this.nodeName</xsl:eval> which has since been replaced with the standard XSLT function name() as in
<xsl:value-of select="name()" />

Secondly, in the process of porting over code from the old style to the new style, you must abandon the method of simple line by line porting! You have to re-engineer the code for XSLT if you have been using less than efficient calls to xsl:script to perform complicate actions.

Thirdly, what is the root (not the same as the documentElement) and because the script is in the CDATA, you cannot do anything.

Fourthly, WHAT (pun intended!) are you intending to do?

Regards,

Brandon Driesen
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 100 total points
ID: 6846011
The following XML/XSLT shows you have to populate the script tag using the XSLT techniques and avoiding the msxsl:script extension whatsoever.

XML (books.xml)
===============
<?xml version='1.0' encoding='utf-8'?>
<collection>
  <book>
    <title>Cosmos</title>
    <author>Carl Sagan</author>
    <publisher>Ballantine Books</publisher>
  </book>
  <book>
    <title>Catwings</title>
    <author>Ursula K. Le Guin</author>
    <publisher>Scholastic</publisher>
  </book>
  <book>
    <title>Home Town</title>
    <author>Tracy Kidder</author>
    <publisher>Random House</publisher>
  </book>
</collection>

XSLT (books.xsl)
===============
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" encoding="iso-8859-1" />
<xsl:template match="/">
<html>
<head>
<title>Porting From XSL To XSLT</title>
<script language="javascript">
<xsl:comment>
var aBooks = new Array(<xsl:apply-templates select="//book" mode="script" />);
window.onload = init;
function init() {
     if (aBooks.length != 0) {
          aBooks.sort();
          //CDATA WITHIN XSLT IS REQUIRED BECOS OF RESERVED CHARS HERE
          <![CDATA[document.body.innerHTML += "<h1>Authors</h1><div>" + aBooks.join("</div><div>") + "</div>";]]>
     }
}
// </xsl:comment>
</script>
</head>
<body style="background:#ffffff;">
</body>
</html>
</xsl:template>
<!--THE FOLLOWING TEMPLATE IS ON ONE SINGLE LINE. ADJUST ACCORDINGLY-->
<xsl:template match="book" mode="script">"<xsl:value-of select="author" />"<xsl:if test="position() != last()">,</xsl:if></xsl:template>
</xsl:stylesheet>

HTML Output (books.asp)
======================
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Porting From XSL To XSLT</title>
<script language="javascript">
<!--
var aBooks = new Array("Carl Sagan","Ursula K. Le Guin","Tracy Kidder");
window.onload = init;
function init() {
 if (aBooks.length != 0) {
  aBooks.sort();
  //CDATA WITHIN XSLT IS REQUIRED BECOS OF RESERVED CHARS HERE
  document.body.innerHTML += "<h1>Authors</h1><div>" + aBooks.join("</div><div>") + "</div>";
 }
}
// -->
</script>
</head>
<body style="background:#ffffff;"></body>
</html>

Regards,

Brandon Driesen
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6846030
You only ever use msxsl:script if there is no other way.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:mhebert
ID: 6847170
Thanks..I will work on that... ;-)
0
 
LVL 1

Author Comment

by:mhebert
ID: 6847240
Thanks..I will work on that... ;-)
0
 
LVL 1

Author Comment

by:mhebert
ID: 6847252
In XSLT, you cannot pass a node in parameter to a <msxsl:script> function? "." isn't a node?
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6847380
it depends on the context
<xsl:template match="/">

. refers to root and not documentElement
child::node()[1] refers to the documentElement

</xsl:template>
0
 
LVL 1

Author Comment

by:mhebert
ID: 6847435
In this case, where I'm not in the root, and I want to keep a value in a javascript variable(an example, it can be an integer..):
============================================
<xsl:for-each select="MODULE">
  <xsl:value-of select="extra:setModulePath(.)"/>
...
</xsl:for-each>
...
<msxsl:script implements-prefix="extra" language="javascript">
<![CDATA[
var modulePath = "";
               
function setModulePath(node)
{
var filePathNode = node.selectSingleNode("FILEPATH");
modulePath = filePathNode.nodeTypedValue + "html\\";
}
...
===========================================
By what I need to replace the "." in the call to let work properly the javascript function setModulePath()? The "." will be the current <MODULE> node.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6849648
please note what I have said earlier.

>>
Thirdly, what is the root (not the same as the documentElement) and because the script is in the CDATA,
you cannot do anything.


WRONG!
>>
<msxsl:script implements-prefix="extra" language="javascript">
<![CDATA[
var modulePath = "";
             
function setModulePath(node)
{
var filePathNode = node.selectSingleNode("FILEPATH");
modulePath = filePathNode.nodeTypedValue + "html\\";
}
<<

RIGHT!
<msxsl:script implements-prefix="extra" language="javascript">
var modulePath = "";
             
function setModulePath(node)
{
var filePathNode = node.selectSingleNode("FILEPATH");
modulePath = filePathNode.nodeTypedValue + "html\\";
}

</msxsl:script>
you place the CDATA around any code which has the reserved XML characters of >,< &

and secondly, this is really not the way you code with XSLT *sigh*, you have to forget about the OLD way.

Next, you MUST return something in your function or it WON'T work, even if you do not intend to use the return value!!!

If you want to hold on to old tricks, you will bang yourself against the wall all the time.



0
 

Expert Comment

by:jedelman
ID: 7478185
I've been reading this post and I'm not sure if I completely follow - so can I call a javascript function in my xsl document or not?  

<msxsl:script implements-prefix="extra" language="javascript">

I understand that this line shouldn't be used.  But if I have a javascript written in my document, how can I call that function from elsewhere in my document?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JQuery to parse xml string and get element by id 4 71
Randomize in Owl Carousel v1.3.2 6 127
Powershell import xml to array (non-cli xml) 4 50
Append data to an XML file 3 31
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question