求助大神,xslt解析xml为html中触发事件的问题
我要在解析后的html的页面中的button能触发displaymessage()方法
XMLFile.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="XSLTFile1.xslt"?>
<root>
<item>abcdefabcdeabcdef</item>
</root>
XSLTFile1.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:myNameSpace">
<msxsl:script language="javascript" implements-prefix="user">
<![CDATA[
function trans(s)
{
var ary = s.split('b');
return ary.join('B');
}
function displaymessage()
{
alert("Hello World!")
}
]]>
</msxsl:script>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="//root/item"/>
<input type="button" value="Click me!" onclick="user:displaymessage()"/>
</body>
</html>
</xsl:template>
<xsl:template match="root/item">
<xsl:value-of select="user:trans(string(.))"/>
</xsl:template>
</xsl:stylesheet>
[解决办法]
你搞错了,你应该区分清楚xsl调用的函数和用户界面函数的区别
下面是正确的
<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:myNameSpace"> <msxsl:script language="javascript" implements-prefix="user"> <![CDATA[function trans(s){ var ary = s.split('b'); return ary.join('B'); }]]> </msxsl:script> <xsl:template match="/"> <html> <script> function displaymessage() { alert("Hello World!") } </script> <body> <xsl:apply-templates select="//root/item"/> <input type="button" value="Click me!" onclick="displaymessage()"/> </body> </html> </xsl:template> <xsl:template match="root/item"> <xsl:value-of select="user:trans(string(.))"/> </xsl:template></xsl:stylesheet>
[解决办法]
或者
<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:myNameSpace"> <msxsl:script language="javascript" implements-prefix="user"> <![CDATA[function trans(s){ var ary = s.split('b'); return ary.join('B'); }]]> </msxsl:script> <xsl:template match="/"> <html> <script> function displaymessage(xx) { alert(xx) } </script> <body> <input type="button" value="Click me!"> <xsl:attribute name="onclick"> displaymessage('<xsl:apply-templates select="//root/item"/>') </xsl:attribute> </input> </body> </html> </xsl:template> <xsl:template match="root/item"> <xsl:value-of select="user:trans(string(.))"/> </xsl:template></xsl:stylesheet>