2015-04-08 23 views
0

我得到以下例外:爲什麼我會得到以下異常'非靜態Java函數的第一個參數'?

致命錯誤:'非靜態Java函數***的第一個參數不是有效的對象引用。

當我嘗試使用xml-maven-plugin轉換XML文檔時,會發生這種情況。

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>xml-maven-plugin</artifactId> 
    <version>1.0</version> 
    <executions> 
     <execution> 
      <goals> 
       <goal>transform</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <transformationSets> 
      <transformationSet> 
       <dir>target/generated/wsdl</dir> 
       <stylesheet>src/test/resources/transform/my-transformation.xsl</stylesheet> 
       <includes> 
        <include>**/*.xsd</include> 
       </includes> 
       <outputDir>target/generated/wsdl</outputDir> 
      </transformationSet> 
     </transformationSets> 
    </configuration> 
</plugin> 

錯誤消息

[INFO] --- xml-maven-plugin:1.0:transform (transform--xsd) @ my-pom --- 
Warning: org.apache.xerces.parsers.SAXParser: Feature 'http://javax.xml.XMLConstants/feature/secure-processing' is not recognized. 
Warning: org.apache.xerces.parsers.SAXParser: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD' is not recognized. 
Warning: org.apache.xerces.parsers.SAXParser: Property 'http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit' is not recognized. 
ERROR: 'The first argument to the non-static Java function 'isTheRightElement' is not a valid object reference.' 
FATAL ERROR: 'The first argument to the non-static Java function 'isTheRightElement' is not a valid object reference.' 

XSL轉換文件的內容

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://mysite/mycreate" xmlns:xalan="http://xml.apache.org/xslt" xmlns:mytag="http://andreas/ps-transformations" version="2.0"> 
<xsl:strip-space elements="*"/> 
<xsl:output method="xml" indent="yes" xalan:indent-amount="4"/> 

<xsl:variable name="myAnnotations" select="document('element-list.xml')"/> 

<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="xs:element">  
    <xsl:param name="myValue" select="normalize-space(mytag:isTheRightElement(@class))"/> 

    <xsl:choose> 
     <xsl:when test="$myValue=''"> 
      <xsl:copy> 
       <xsl:apply-templates select="@*|node()"/> 
      </xsl:copy> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:copy> 
       <xsl:apply-templates select="@*|node()"/> 
       <xsl:element name="xs:annotation"> 
        <xsl:element name="xs:documentation"> 
         <xsl:value-of select="$elementValue"/> 
        </xsl:element> 
       </xsl:element> 
      </xsl:copy> 
     </xsl:otherwise> 
     </xsl:choose> 
</xsl:template> 

<xsl:function name="myTag:isTheRightElement"> 
    <xsl:param name="class"/> 
     <xsl:for-each select="$myAnnotations/elementList/element"> 
      <xsl:if test="lower-case(@class) = 'something'"> 
       <xsl:value-of select="text()"/>   
      </xsl:if> 
     </xsl:for-each> 
    </xsl:function>  
</xsl:stylesheet> 
+0

「我得到這個例外」不是問題。我還建議從標題中拉出錯誤信息,並將其(也)放在正文文本中 – Joeblade

+0

我正確地提出了問題並演示了整個案例。提出積極的觀點是公平的,因爲這個問題引起了你的興趣。 –

+0

因爲「我有這個問題」,我會帶走我的推論,即使沒有問號,也是一個有效的問題。但是我很難理解這個問題。可能是我困惑/分心。但重申你的問題可能會幫助那些容易分心的人。 – Joeblade

回答

0

我在嘗試使用XSLT 2.0 '樣式表' 改造的時候這個錯誤,但我使用XSLT 1.0分析器。

在我的情況下,當我刪除saxon的maven依賴項(這是一個XSLT 2.0分析器)時會發生這種情況。 Java 7附帶的XSLT解析器只能處理XSLT 1.0。

Maven的解決辦法是保持撒克遜:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>xml-maven-plugin</artifactId> 
    <version>1.0</version> 
    <executions> 
     <execution> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>transform</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <transformerFactory>net.sf.saxon.TransformerFactoryImpl</transformerFactory> 
     <transformationSets> 
      <transformationSet> 
       <dir>src/main/resources/xml</dir> 
       <stylesheet>src/main/resources/xslt/my-transformation.xsl</stylesheet> 
       <includes> 
        <include>**/*.xml</include> 
       </includes> 
       <outputDir>target/generated/wsdl</outputDir> 
      </transformationSet> 
     </transformationSets> 
    </configuration> 
    <dependencies> 
     <dependency> 
      <groupId>net.sf.saxon</groupId> 
      <artifactId>Saxon-HE</artifactId> 
      <version>9.6.0-5</version> 
     </dependency> 
    </dependencies> 
</plugin> 
+0

正如我在我的回答中所說的,如果切換到不同的XSLT處理器,您可能希望調用外部Java函數以完全不同的方式工作。 –

+0

我知道,但基於例外,處理器顯示它根本不清楚。同意,當我使用撒克遜處理器是行得通的。這就是這個問題/答案所展現的。 –

0

從XSLT到Java調用的處理是不以任何標準定義的 - 這完全取決於你所使用的XSLT處理器。

另請注意,如果您使用的是撒克遜版本,則版本8.7非常陳舊(約爲2006年)。目前的版本是9.6。

+0

撒克遜的版本是不相關的,但我仍然把最新版本,以防有人盲目複製代碼。 我只是顯示如何解決異常(至少在這種情況下)。當我得到例外時,沒有在線來源幫助我,儘管網絡中存在許多類似的問題。 –

相關問題