2
如何在xslt中通過xpath選擇值調用javascript/c#函數。 這是我如何調用函數manualy類型化的參數:使用xpath選擇使用xslt調用函數
<xsl:value-of select="cs:my('some text')"/>
如何在xslt中通過xpath選擇值調用javascript/c#函數。 這是我如何調用函數manualy類型化的參數:使用xpath選擇使用xslt調用函數
<xsl:value-of select="cs:my('some text')"/>
下面是從MSXML 4 SDK(這應該是MSXML 6相同,是.NET的XslCompiledTransform頗爲相似的例子 - 爲後者search MSDN for <msxsl:script>
)
例這個例子定義了與用戶的一個命名空間前綴 的腳本塊包含一個調用的函數xml以 節點列表作爲參數。稍後,該函數xml(nodelist)在 用戶名空間中,從select屬性中調用。
XML文件(customers.xml)
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="script.xsl" ?> <customers> <customer> <name>John Smith</name> <address>123 Elm St.</address> <phone>(123) 456-7890</phone> </customer> <customer> <name>Mary Jones</name> <address>456 Oak Ave.</address> <phone>(156) 789-0123</phone> </customer> </customers>
XSLT文件(script.xsl)
<?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://mycompany.com/mynamespace"> <msxsl:script language="JScript" implements-prefix="user"> function xml(nodelist) { return nodelist.nextNode().xml; } </msxsl:script> <xsl:template match="/"> <xsl:value-of select="user:xml(.)"/> </xsl:template> </xsl:stylesheet>
格式化輸出
<?xml version="1.0" encoding="UTF-16"?><?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="script.xsl" ?>
<customers>
<customer>
<name>John Smith</name>
<address>123 Elm St.</address>
<phone>(123) 456-7890</phone>
</customer>
<customer>
<name>Mary Jones</name>
<address>456 Oak Ave.</address>
<phone>(156) 789-0123</phone>
</customer>
</customers>
您使用哪種XSLT處理器?用C#可以是System.Xml.Xsl.XslCompiledTransform,它在這裏有記錄:http://msdn.microsoft.com/en-us/library/wxaw5z5e.aspx。基本上,XSLT/XPath節點集由C#函數作爲XPathNodeIterator接收。 –