xpath選択値を渡してxsltでjavascript/c#関数を呼び出すにはどうすればよいですか?手動で入力したパラメーターを使用して関数を呼び出す方法は次のとおりです。
<xsl:value-of select="cs:my('some text')"/>
これはMSXML4SDKの例です(これはMSXML 6でも同じであり、.NEtのXslCompiledTransformでも非常によく似ています。後者のMSDNで検索してください<msxsl:script>
)
例この例では、ノードリストを引数として取るxmlという関数を含むuserの名前空間プレフィックスを持つスクリプトブロックを定義します。後で、この関数、ユーザー名前空間の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>