xml & xsl transform

G

Guest

Hi i have my XML file c:\prd.xm
<?xml-stylesheet type="text/xsl" href="prd.xsl"?><products><product><a>2</a><b>3</b></product><product><a>4</a><b>2</b></product></products

This is my XSL file
<?xml version="1.0" encoding="windows-1255" ?><xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:utils="urn:galileo-com:msxsl-jscript-utilities"
version="1.0"><xsl:template match="/"><table border="1"><tr><td>a</td><td>b</td><td>sum</td></tr><xsl:for-each select="products/product"><tr><td><xsl:value-of disable-output-escaping="yes" select="a" /></td><td><xsl:value-of disable-output-escaping="yes" select="b" /></td><td><xsl:value-of disable-output-escaping="yes" select="utils:mult(.)" /></td></tr></xsl:for-each></table></xsl:template><msxsl:script language="JScript" implements-prefix="utils"><![CDATA[
function mult(objElem

var a
var b
a = objElem.item(0).selectSingleNode("a").text
b = objElem.item(0).selectSingleNode("b").text
return a*b

]]></msxsl:script></xsl:transform

I MUST use javascript to evaluate values. when i open xml file with my browser it works fine. but when I use C# code to transform xml file i recieve exception from Javascript part
this is my C# code
tr

XslTransform xslt = new XslTransform();
xslt.Load(xslFile)
XPathDocument mydata = new XPathDocument(xmlFile)
XmlWriter writer = new XmlTextWriter(Console.Out)
xslt.Transform(mydata,null,writer, null)

catch(XsltException ex

Console.WriteLine(ex.Message )

Please can anydody help me ??
 
V

Vjekoslav Babic

This happens because of the context in which the XSL transformation is
executed. When you open the XML file in Internet Explorer, the context is
MSXML and custom functions use XML DOM, while in .NET custom functions must
use .NET classes. JScript function mult you defined knows how to use
objElem.item(0).selectSingleNode syntax when executed from Internet Explorer
using XML DOM, however .NET does not understand these classes and you get
the exception.

When XSLT processor reaches the utils:mult(.) and calls the mult function,
the function argument (objElem) is IXMLDOMNodeList when you call it from
MSXML. When your transformation is done in .NET, the objElem is of type
System.Xml.XPath.XPathQueryIterator and you script simply won't work unless
you rewrite.

The point is, you can not have the inline functions in XSLT use DOM when
called from .NET.

--
Vjekoslav Babic, MCSA, MCDBA



Danny Lesnik said:
Hi i have my XML file c:\prd.xml
<?xml-stylesheet type="text/xsl"
href="prd.xsl"?> said:
This is my XSL file:
<?xml version="1.0" encoding="windows-1255" ?><xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:utils="urn:galileo-com:msxsl-jscript-utilities"
version="1.0"><xsl:template match="/"><table
border="1"><tr><td>a</td><td>b</td><td>sum</td></tr><xsl:for-each
select="products/product"><tr><td><xsl:value-of
disable-output-escaping="yes" select="a" /></td><td><xsl:value-of
disable-output-escaping="yes" select="b" /></td><td><xsl:value-of
disable-output-escaping="yes" select="utils:mult(.)"
/> said:
function mult(objElem)
{
var a;
var b;
a = objElem.item(0).selectSingleNode("a").text;
b = objElem.item(0).selectSingleNode("b").text;
return a*b;
}
]]></msxsl:script></xsl:transform>

I MUST use javascript to evaluate values. when i open xml file with my
browser it works fine. but when I use C# code to transform xml file i
recieve exception from Javascript part
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top