2011-02-07 146 views
2

只是試圖創建和使用顯示該節點的內容(如果有的話)或一個破折號如果爲空的XSL函數。錯誤聲明和使用函數xsl

下面是該文件的某些部分:

<xsl:stylesheet version="2.0" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fn="http://www.w3.org/2005/xpath-functions" 
    xmlns:qes="http://www.qwamci.com"> 

    <xsl:function name="qes:textOrDash" as="xs:string"> 
    <xsl:param name="mynode" /> 
    <xsl:sequence select="if (fn:compare(translate($mynode, ' ', ''), '')=0) then '-' else $mynode" /> 
    </xsl:function> 

    <xsl:template match="Response"> 
    <xsl:value-of select="qes:textOrDash(./SOME/OTHER/XPATH/TO/NODE)" /> 
    </xsl:template> 

</xsl:stylesheet> 

錯誤:

Erreur:java.lang.NoSuchMethodException: For extension function, could not find method org.apache.xml.utils.NodeVector.textOrDash([ExpressionContext,]). 

一些想法?

+0

問得好,+1。查看我的答案,找出問題的最可能原因。 :) –

回答

0

似乎您試圖使用XSLT 1.0處理器到執行XSLT 2.0轉換。

在XSLT 1.0沒有<xsl:function>指令,而是一個可以使用的模板:

<xsl:call-template name="textOrDash"> 
<xsl:with-param name="mynode" select="SomeXPath-Expression"/> 
</xsl:call-template> 

<xsl:template name="textOrDash"> 
<xsl:param name="mynode" select="someDefault"/> 

<!-- Processing here --> 
</xsl:template> 
+0

你對!我正在使用AXIS 1! 用於在XSLT 1.0中創建函數的任何解決方案? –

+0

@ Patrick-Ferreira:在XSLT1.0中,對任何命名模板使用''。 –

+0

@ Patrick-Ferreira:我已經更新了我的答案,以說明如何在XSLT 1.0中編寫和調用命名模板。 –

1

您需要爲您的功能定義一些參數。您已經定義了一個功能qes:textOrDash()你需要添加<xsl:param name="input"/>到您的函數定義,然後引用$input而不是.所以你必須:

<xsl:function name="qes:textOrDash" as="xs:string"> 
    <xsl:param name="input" /> 
    <xsl:sequence select="if (fn:compare(translate($input, ' ', ''), '')=0) then '-' else ." /> 
</xsl:function> 
+0

+1很好的答案。所引用函數的簽名與所定義函數的簽名不匹配。 – LarsH

+0

感謝您的快速回復。我已經嘗試過了,但是我遇到了同樣的問題:( –

+0

函數體中沒有上下文節點,所以'self :: node()'的''''縮寫會產生一個錯誤。 – 2011-02-07 16:41:59

1

首先,我不認爲你需要一個功能。作爲例子,這個樣式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="test"> 
     <xsl:value-of select="(.,'-')[normalize-space(.)][1]"/> 
    </xsl:template> 
</xsl:stylesheet> 

利用該輸入:

<test>string</test> 

輸出:

string 

利用該輸入:

<test></test> 

輸出:

- 

有了這個輸入:

<test><not-string-value/></test> 

輸出:

- 

而這種輸入:

<test>&#x20;&#xA;&#x9;&#xD;</test> 

輸出

- 

關於你的函數:你只帶區空格字符...

+0

較短:'(。[normalize-space()],' - ')[1]' – 2011-02-07 17:06:32