XSLT:如何声明用户定义的函数

发布于 2024-12-20 09:48:49 字数 668 浏览 3 评论 0原文

我有一个用于转换的 XSLT 样式表。在此,我必须使用 指定用户定义的函数。当我准备在此 中执行此操作时,它会引发错误。

这是函数:

<xsl:function name="functx:pad-string-to-length" as="xs:string" 
              xmlns:functx="http://www.functx.com" >
  <xsl:param name="stringToPad" as="xs:string?"/> 
  <xsl:param name="padChar" as="xs:string"/> 
  <xsl:param name="length" as="xs:integer"/> 

  <xsl:sequence select=" 
   substring(
     string-join (
       ($stringToPad, for $i in (1 to $length) return $padChar)
       ,'')
    ,1,$length)
 "/>

</xsl:function>

I have a XSLT style sheet for transforming. In this I have to specify user defined function using <xsl:function>. When I am about to do this within this <xsl:stylesheet></xsl:stylesheet> it throws an error.

Here is the function:

<xsl:function name="functx:pad-string-to-length" as="xs:string" 
              xmlns:functx="http://www.functx.com" >
  <xsl:param name="stringToPad" as="xs:string?"/> 
  <xsl:param name="padChar" as="xs:string"/> 
  <xsl:param name="length" as="xs:integer"/> 

  <xsl:sequence select=" 
   substring(
     string-join (
       ($stringToPad, for $i in (1 to $length) return $padChar)
       ,'')
    ,1,$length)
 "/>

</xsl:function>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗失的美好 2024-12-27 09:48:49

问题中定义的函数在语法上是正确的并且似乎有意义。

因此,错误出在您忘记向我们展示的代码中 - 更具体地说,该函数如何与哪些参数一起使用。

除此之外,还有明显的重构可能性——例如用 concat() 替换不必要的 string-join()

The function defined in the question is syntactically correct and seems meaningful.

Therefore, the error is in the code that you forgot to show to us -- more specifically how this function is being used with what arguments.

Apart from this, there are obvious refactoring possibilities -- such as replacing the unnecessary string-join() with concat().

守望孤独 2024-12-27 09:48:49

正如@John Mitchell 所说,告诉我们错误是什么可能会有所帮助。但是,我看到您在函数上声明了命名空间 - 确保 xmlns:functx="http://www.functx.com" 也在 中声明; 样式表顶部的元素。

显然,要检查的另一件事是您在函数调用中正确输入参数:前两个参数需要引号,但第三个参数不需要(例如 )。

As @John Mitchell said, telling us what the error is might be helpful. However, I see you have the namespace declared on the function - make sure xmlns:functx="http://www.functx.com" is also declared in the <xsl:stylesheet> element at the top of your stylesheet.

Another thing to check, obviously, is that you're entering your arguments correctly in the function call: your first two parameters will need quotes but the third doesn't (eg <xsl:value-of select="functx:pad-string-to-length('boo', '-', 12)"/>).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文