如何使 unparsed-text-lines() 函数在一个样式表中同时有效地用于 XSLT 2.0 和 XSLT 3.0 处理器?
我以为我可以像这样使用 function-available() 函数,但这会为 XSLT 2.0 处理器返回一个语法错误。
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:local="local"
version="2.0" exclude-result-prefixes="xs fn local">
<xsl:function name="local:unparsed-text-lines" as="xs:string+">
<xsl:param name="href" as="xs:string" />
<xsl:choose>
<xsl:when test="function-available('fn:unparsed-text-lines')">
<!-- XSLT 3.0 -->
<xsl:sequence select="fn:unparsed-text-lines($href)" />
</xsl:when>
<xsl:otherwise>
<!-- XSLT 2.0 -->
<xsl:sequence select="fn:tokenize(fn:unparsed-text($href), '\r\n|\r|\n')[not(position()=last() and .='')]" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
etc.
最佳答案
问题是 :<xsl:when>
是一个运行时运算符,编译器在编译时不知道它的结果是 true()
或 false()
.
解决方案 : 使用 use-when
属性。
转换变成这样:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:local="local"
version="2.0" exclude-result-prefixes="xs local">
<xsl:function name="local:unparsed-text-lines" as="xs:string+">
<xsl:param name="href" as="xs:string" />
<xsl:sequence select="fn:unparsed-text-lines($href)"
use-when="function-available('unparsed-text-lines')" />
<xsl:sequence use-when="not(function-available('unparsed-text-lines'))"
select="tokenize(unparsed-text($href), '\r\n|\r|\n')
[not(position()=last()
and
.=''
)
]" />
</xsl:function>
</xsl:stylesheet>
现在没有错误引发 .
关于xslt - 如何为 XSLT 尚不可用的函数编写回退代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9355212/