2012-02-20 24 views
4

如何使unparsed-text-lines()函数在一个样式表中对XSLT 2.0和XSLT 3.0处理器有效可用?代码如何退出,以获取尚未提供的函数XSLT?

我认为我可以像这样使用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. 

回答

5

的问题是

<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> 

现在不会引发错误

+0

谢谢。我最初尝试xsl:use-when在xsl:when level时仍然无法编译。谢谢。 – 2012-02-20 02:24:50

+0

@ SeanB.Durkin:不客气。 – 2012-02-20 02:35:34

+0

@DimitreNovatchev这只是救了我的一天,谢谢:) – quaylar 2013-06-04 13:11:47