2012-07-25 78 views
2

下面的XSLT轉換會在我嘗試使用函數node-name()時顯示錯誤。xpath-functions不標識外部Java類

Error: E[Saxon6.5.5]The URI http://www.w3.org/2005/xpath-functions does not identify an external Java class

<xsl:stylesheet version="1.1" 
    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"> 
<!-- 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
-->   

    <xsl:output method="text" /> 
    <xsl:variable name="in" select="/"/> 
    <xsl:variable name="filter" select="document('elementsToBeLeftIn.xml')"/> 

    <xsl:template match="/"> 
     <xsl:apply-templates select="*"> 
      <xsl:with-param name="f" select="$filter/*"/> 
     </xsl:apply-templates> 
    </xsl:template> 

    <xsl:template match="*"> 
     <xsl:param name="f"/> 
     <xsl:choose> 
      <xsl:when test="$f/*"> 
       <xsl:copy-of select="fn:node-name()"/> 

       <!-- 
       <xsl:for-each select="*[fn:node-name(.) = $f/*/fn:node-name(.)]"> 
        <xsl:apply-templates select="."> 
         <xsl:with-param name="f" select="f/*[fn:node-name() = current()/fn:node-name()]"/> 
        </xsl:apply-templates> 
       </xsl:for-each> 
       --> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:copy-of select="."/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet>  

感謝大衛。 這就是我真正想做的工作(它是遞歸的)。使用name()我仍然收到錯誤*Unexpected tocken [<function>] in path expression*

後您

<xsl:stylesheet version="1.1" 
    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"> 
<!-- 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
-->   

    <xsl:output method="text" /> 
    <xsl:variable name="in" select="/"/> 
    <xsl:variable name="filter" select="document('elementsToBeLeftIn.xml')"/> 

    <xsl:template match="/"> 
     <xsl:apply-templates select="*"> 
      <xsl:with-param name="f" select="$filter/*"/> 
     </xsl:apply-templates> 
    </xsl:template> 

    <xsl:template match="*"> 
     <xsl:param name="f"/> 
     <xsl:choose> 
      <xsl:when test="$f/*"> 
       <xsl:for-each select="*[name() = $f/*/name()]"> 
        <xsl:apply-templates select="."> 
         <xsl:with-param name="f" select="f/*[name() = current()/name()]"/> 
        </xsl:apply-templates> 
       </xsl:for-each> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:copy-of select="."/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet>  

回答

0

即使在XSLT2你永遠不需要像前綴節點名稱標準功能()。但是你使用的是XSLT1的saxon 6,所以你不能在函數前加上它們,否則它們將永遠不會被識別。 (1個XPath的標準功能不是在一個命名空間)

只使用select="name()"

不過,我不認爲你希望你的代碼就可以了(但你沒有說你要它做什麼),但它只會將模板應用於一個元素(頂層文檔元素),因爲模板永遠不會遞歸應用。

在過濾器測試爲真的情況下,<xsl:copy-of select="name()"會放出該元素的名稱,但沒有標記(所以結果不會很好地形成xml)。

在過濾器測試爲false的情況下,包括其所有子項的整個文檔元素都被複制到輸出,並且不再進行進一步的處理。

$f/*/name() 

是合法的XPath2但不是在XPath中1中,其中使用/路徑表達式僅可以使用節點不與返回字符串的函數結束。不確定你想要做什麼,因此不能立即提供替代品。

current()/name() 

可以在XPath中1

寫成

name(current()) 

但由於使用的是撒克遜Java實現,爲什麼不直接使用撒克遜9,而不是撒克遜6和受益於過去十年的值得在xslt引擎中進一步發展?

+0

謝謝。這適用於該測試線。實際上,我想運行的代碼是遞歸地運行的代碼。我想要做的是過濾XML文件中的一些元素。 'elementsToBeFilterOut.xml'告訴我要讓什麼,最後我想改變它列出要排除的元素。 – 2012-07-25 14:15:43

+0

非常感謝您的第二個提示。我去了我的oXygen的「配置轉換場景」,並將其改爲使用Xason 9引擎,現在確實通過了測試。 – 2012-07-25 15:19:37

0

Saxon 6.5.5是一款XSLT 1.0引擎。名稱空間http://www.w3.org/2005/xpath-functions適用於XPATH 2.0,XSLT 2.0和XSLT 3.0。 XSLT 1.0無法識別這個名稱空間。這就是你遇到錯誤的原因。

沒有XSLT 1.0相當於http://www.w3.org/2005/xpath-functions。只需調用沒有前綴的函數即可。

+0

謝謝肖恩和大衛。我現在可以在你的幫助下。 – 2012-07-25 15:20:29