2009-12-28 36 views
1

問題:麻煩的xsl:for-每個使用XSL的選擇:可變

我有麻煩發生,其選擇在由位置不同的部分的XML文檔的一個節點的xpath表達式。我使用xsl:variable來創建表達式,但是當我使用xsl:for-each作爲我的select語句的xsl:變量的值時,我得到一個錯誤。

<xsl:variable name="input_params_query"> 
    <xsl:text disable-output-escaping="yes">/example/inputs/dataset[</xsl:text> 
    <xsl:number value="position()" format="1" /> 
    <xsl:text disable-output-escaping="yes">]/parameter</xsl:text> 
</xsl:variable> 

<xsl:for-each select="$input_params_query"> 
    <input rdf:resource="#{@name}"/> 
</xsl:for-each> 

導致錯誤:

The 'select' expression does not evaluate to a node set. 

當我打印出來的XSL的值變量我使用,我得到:

/example/inputs/dataset[1]/parameter 

這是一個有效和正確的Xpath表達式我試圖在for-each調用中選擇節點。

是否將xsl:for-each select屬性的xsl:variable用法不正​​確?

背景和充分的解釋:

我使用XSLT來生成在以下XML結構的可用信息的RDF/XML表示。

在這種情況下,什麼XML實際上是指一個過程進行兩次;第一次生成輸出文件「a」和第二次生成輸出文件「b」。參數「p1」和「p2」是生成文件「a」的執行的輸入,參數「p3」是生成文件「b」的執行的輸入。

對於'process'的每個輸出,我都生成一個RDF個體併爲該流程執行定義輸入和輸出。基本上,我想將/ example/inputs/dataset [n] /參數中的所有值定義爲生成輸出/ example/process/outputs/file [n]的進程的輸入。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
version="1.0"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:template match="//process"> 
    <xsl:for-each select="outputs/*"> 

    <!-- declare rdf resource of type Process, define Process outputs --> 
    <!-- ... this I already have working so I have withheld for brevity --> 

    <!-- define input parameters --> 

    <xsl:variable name="input_params_query"> 
    <xsl:text disable-output-escaping="yes">/example/inputs/dataset[</xsl:text> 
    <xsl:number value="position()" format="1" /> 
    <xsl:text disable-output-escaping="yes">]/parameter</xsl:text> 
    </xsl:variable> 

    <xsl:for-each select="$input_params_query"> 
    <input rdf:resource="#{@name}"/> 
    </xsl:for-each> 

    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 
+0

什麼XSLT引擎您使用的? – 2009-12-28 20:56:56

回答

3

「我是否將xsl:variable作爲xsl:for-each select屬性使用不正確?」

是的。在select屬性中指定的值必須是一個valide節點集表達式,也就是說,它必須是一個Xpath表達式,其值爲一個(可能爲空)節點集。

您定義的變量是一個字符串類型。該字符串恰好是一個有效的Xpath表達式,但它仍然只是一個字符串。

我想你可以寫像這樣實現你想要的結果:

<xsl:template match="//process"> 
    <xsl:for-each select="outputs/*"> 
    <!-- declare rdf resource of type Process, define Process outputs --> 
    <!-- ... this I already have working so I have withheld for brevity --> 

    <!-- define input parameters --> 
    <xsl:variable name="position" select="position()"/> 

    <xsl:for-each select="/example/inputs/dataset[$position]"> 
     <input rdf:resource="#{@name}"/> 
    </xsl:for-each> 
    </xsl:for-each> 
</xsl:template> 
+0

工作!謝謝。 – LokiPatera 2009-12-28 21:05:47