XSLT:如何声明用户定义的函数
我有一个用于转换的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题中定义的函数在语法上是正确的并且似乎有意义。
因此,错误出在您忘记向我们展示的代码中 - 更具体地说,该函数如何与哪些参数一起使用。
除此之外,还有明显的重构可能性——例如用
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()
withconcat()
.正如@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)"/>
).