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

June 25th, 2015, 05:20 AM
|
|
Registered User
|
|
Join Date: Mar 2015
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Calling user defined function written in javascript from an XSL
I have a javascript inside the XSL style sheet. In the javascript, i have a function, getName(node). How do i call this in my xsl? I had used xsl:eval=getname(this) earlier. It worked till IE8. Hasnt worked from IE9 onwards. Can i use xsl:value-of-select? or any other tips to make it work ? Thank you in advance.
<xsl:script language='JavaScript'><![CDATA[
var sSobeysRegion = "Sobeys West";
var sReceiverID = "SOBEYSCANADA:ZZ";
var ATLANTIC = "Sobeys Atlantic";
var WEST = "Sobeys West";
var QUEBEC = "Sobeys Quebec";
var ONTARIO = "Sobeys Ontario";
var option1 = ATLANTIC ;
var option2 = WEST ;
var option3 = QUEBEC ;
var option4 = ONTARIO ;
function getRegion(sVar)
{
if (sVar == sSobeysRegion) {
return true;
}
else {
return false;
}
}
function setReceiverID(sTmp)
{
if (sTmp == "Sobeys Atlantic") {
sReceiverID = "SOBNBTATLTST:ZZ";
option1 = ATLANTIC;
option2 = WEST;
option3 = ONTARIO;
option4 = QUEBEC;
}
else if (sTmp == "Sobeys West") {
sReceiverID = "SOBNBTWSTTST:ZZ";
option2 = ATLANTIC;
option1 = WEST;
option3 = QUEBEC;
option4 = ONTARIO;
}
else if (sTmp == "Sobeys Quebec") {
sReceiverID = "SOBNBTQUETST:ZZ";
option2 = ATLANTIC;
option3 = WEST;
option4 = ONTARIO;
option1 = QUEBEC;
}
else if (sTmp == "Sobeys Ontario") {
sReceiverID = "SOBNBTONTTST:ZZ";
option2 = ATLANTIC;
option3 = WEST;
option1 = ONTARIO;
option4 = QUEBEC;
}
}
function getRegionValue(Node)
{
var sRegion = "TESTID";
var xmldoc = Node.ownerDocument;
var root = xmldoc.documentElement;
sRegion = root.getAttribute("Region");
sSobeysRegion = sRegion;
setReceiverID(sSobeysRegion)
// return sRegion;
return "";
}
var iLineItemCount = 0;
function addLineItemCount()
{
++iLineItemCount;
return iLineItemCount;
}
function getLineItemCount()
{
return iLineItemCount;
}
function getEditNodeName(Node)
{
var sTmp = "";
// var xmldoc = Node.ownerDocument;
// var root = xmldoc.documentElement;
// var attribute = null;
// sTmp = Node.nodeName;
// attribute = xmldoc .createAttribute("EditItemCount");
// Node.setAttributeNode(attribute);
// Node.setAttribute("EditItemCount", getEditItemCount());
//attribute = Node.getAttributeNode();
// sTmp += ", " + Node.getAttribute("EditItemCount");
// sTmp += ", document.form." + Node.nodeName + Node.getAttribute("EditItemCount") + ".value";
// sTmp = "this";
sTmp = "this, " + getIndex(Node);
return sTmp;
}
function getName(Node)
{
var sTmp = "";
sTmp = Node.nodeName;
return sTmp;
}
function getValue(Node)
{
var sTmp = "";
if (Node.childnodes(0)!= null) {
sTmp = Node.childnodes(0).nodeValue;
}
return sTmp;
}
function setValue(Node, sValue)
{
}
function TransformDate(Node)
{
var sTmp = 0;
var sDate = "";
sTmp = Node.childnodes(0).nodeValue;
sDate = sTmp.substring(4,6) + "/" + sTmp.substring(6,8) + "/" + sTmp.substring(0,4);
return sDate;
}
var iLineCount = 0;
function getLineCount(Node)
{
++iLineCount;
return iLineCount;
}
var iEditItemCount = 0;
function getEditItemCount(Node)
{
++iEditItemCount;
return iEditItemCount;
}
function getIndex(node)
{
var iCount = 0;
var previousNode = null;
while(node.parentNode != null){
previousNode = node.previousSibling;
while(previousNode != null){
if ((previousNode.nodeName != "#document") && (previousNode.nodeName != "xml")
&& (previousNode.nodeName != "xml-stylesheet")) {
iCount += countChildren(previousNode);
// sValue += previousNode.nodeName + "%";
++iCount;
}
previousNode = previousNode.previousSibling;
}
node = node.parentNode;
if (node.nodeName != "#document") {
++iCount;
// sValue += node.nodeName + "*"
}
}
return(iCount);
}
function countChildren(node)
{
var iCount = 0;
var iTotal = 0;
var intNode = 0;
iCount = node.childNodes.length;
if (iCount > 0) {
for (intNode = 0; intNode < iCount; intNode++) {
if (node.childNodes(intNode).nodeName != "#text") {
iTotal += countChildren(node.childNodes(intNode));
// sValue += node.childNodes(intNode).nodeName + "&";
++iTotal;
}
}
}
return(iTotal);
}
function showNext()
{
}
]]></xsl:script>
This is the javascript present in the xsl:script block. The call to this function is below.
`enter code here`
<xsl:template match="SAC_SAC_01_248">
<xsl:element name='select'>
<xsl:attribute name='class'>select</xsl:attribute>
<xsl:attribute name='name'><xsl:value-of-select = "getName(this)">/></xsl:attribute>
<xsl:attribute name='defaultvalue'><xsl:value-of-select = "getValue(this)"/></xsl:attribute>
<xsl:attribute name='style'>color: black; font-size: 10px;font-weight: normal;font-family: Verdana, Arial, Helvetica, sans-serif</xsl:attribute>
<xsl:attribute name='onchange'>onNewCodeChange(<xsl:value-of-select = "getEditNodeName(this)"/>, 2)</xsl:attribute>
<xsl:element name='Option'>
<xsl:attribute name='value'></xsl:attribute>
</xsl:element>
<xsl:element name='Option'>
<xsl:attribute name='value'>A</xsl:attribute>
A
</xsl:element>
<xsl:element name='Option'>
<xsl:attribute name='value'>C</xsl:attribute>
C
</xsl:element>
</xsl:element>
</xsl:template>
|
|

June 25th, 2015, 05:50 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
I strongly suspect you are using the old 1998 Microsoft dialect of XSL which was superseded when the W3C standard came out in 1999. Check the namespace on the xsl:stylesheet element - if it ends in WD-xsl, that means "working draft XSL", and you're on he old version. (I haven't seen this for a long while; I even threw away my Wrox book on WD-xsl a couple of years ago because I thought I would never see it again. It's almost impossible to get hold of a spec nowadays, and there are significant differences from the W3C spec.)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

June 25th, 2015, 05:55 AM
|
|
Registered User
|
|
Join Date: Mar 2015
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0">
This is what i am using.
|
|

June 25th, 2015, 06:12 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
OK, then it's not WD-xsl. In that case I don't know what the xsl:eval was about. Also the xsl:script: it should be msxsl:script in the Microsoft namespace.
Note that in W3C XSLT you can write this kind of stuff:
Code:
<xsl:element name='Option'>
<xsl:attribute name='value'>A</xsl:attribute>
A
</xsl:element>
<xsl:element name='Option'>
<xsl:attribute name='value'>C</xsl:attribute>
C
</xsl:element>
much more concisely as
Code:
<Option value="A"/>
<Option value="C"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

June 25th, 2015, 06:29 AM
|
|
Registered User
|
|
Join Date: Mar 2015
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
you are telling me to change this <SCRIPT language='JavaScript'><xsl:comment><
June 25th, 2015, 06:31 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
No, it was the xsl:script I was concerned about. But I'm afraid I'm out of my depth now - haven't used client-side XSLT on Internet Explorer for many years. I hope I've pointed you in the right direction.
There aren't many people using this forum these days - you may have better luck on StackOverflow.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

June 25th, 2015, 06:34 AM
|
|
Registered User
|
|
Join Date: Mar 2015
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
oh, thank you so much
|
|
 |