【问题标题】:calling function with xslt using xpath selection使用 xpath 选择使用 xslt 调用函数
【发布时间】:2011-09-18 16:26:45
【问题描述】:

如何在 xslt 中调用 javascript/c# 函数并传递 xpath 选择值。 以下是我如何使用手动输入的参数调用函数:

<xsl:value-of select="cs:my('some text')"/>

【问题讨论】:

  • 您使用哪种 XSLT 处理器?对于 C#,它可能是 System.Xml.Xsl.XslCompiledTransform,它记录在这里:msdn.microsoft.com/en-us/library/wxaw5z5e.aspx。基本上,您的 C# 函数将 XSLT/XPath 节点集作为 XPathNodeIterator 接收。

标签: xslt msxsl


【解决方案1】:

这是来自MSXML 4 SDK 的示例(这对于 MSXML 6 应该是相同的,并且对于 .NET 的 XslCompiledTransform 非常相似——对于后者 search MSDN for &lt;msxsl:script&gt;强>)

示例 此示例定义了一个带有命名空间的脚本块 包含名为 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"?>&lt;?xml version="1.0"?&gt;
&lt;?xml-stylesheet type="text/xsl" href="script.xsl" ?&gt;
&lt;customers&gt;
 &lt;customer&gt;
  &lt;name&gt;John Smith&lt;/name&gt;
  &lt;address&gt;123 Elm St.&lt;/address&gt;
  &lt;phone&gt;(123) 456-7890&lt;/phone&gt;
 &lt;/customer&gt;
 &lt;customer&gt;
  &lt;name&gt;Mary Jones&lt;/name&gt;
  &lt;address&gt;456 Oak Ave.&lt;/address&gt;
  &lt;phone&gt;(156) 789-0123&lt;/phone&gt;
 &lt;/customer&gt;
&lt;/customers&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2022-01-04
    • 2012-02-07
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多