2011-05-18 59 views
1

我試圖創建一個動態匹配元素的xslt函數。在函數中,我將傳遞兩個參數 - item()*和一個逗號分隔的字符串。我記號化在<xsl:for-each> SELECT語句中的逗號分隔字符串,然後執行以下操作:XSLT中的動態匹配語句

select="concat('$di:meta[matches(@domain,''', current(), ''')][1]')" 

代替select語句「執行」中的XQuery,它只是返回的字符串。

我該如何獲得它來執行xquery?

在此先感謝!

回答

1

問題是你在concat()函數中包裝了太多的表達式。評估時,它將返回一個字符串,該字符串將是XPath表達式,而不是評估爲REGEX匹配表達式使用動態字符串的XPath表達式。

你想使用:

<xsl:value-of select="$di:meta[matches(@domain 
             ,concat('.*(' 
               ,current() 
               ,').*') 
             ,'i')][1]" /> 

雖然,因爲你現在是分開評估每個學期,而不是在一個單一的正則表達式的每個那些條款並選擇第一個,它現在將返回每個匹配的第一個結果,而不是匹配項目序列中的第一個結果。這可能是也可能不是你想要的。

如果你想從符合條件的商品的序列中的第一個項目,你可以做這樣的事情:

<!--Create a variable and assign a sequence of matched items --> 
<xsl:variable name="matchedMetaSequence" as="node()*"> 
<!--Iterate over the sequence of names that we want to match on --> 
<xsl:for-each select="tokenize($csvString,',')"> 
    <!--Build the sequence(list) of matched items, 
     snagging the first one that matches each value --> 
    <xsl:sequence select="$di:meta[matches(@domain 
         ,concat('.*(' 
           ,current() 
           ,').*') 
         ,'i')][1]" /> 
</xsl:for-each> 
</xsl:variable> 
<!--Return the first item in the sequence from matching on 
    the list of domain regex fragments --> 
<xsl:value-of select="$matchedMetaSequence[1]" /> 

你也可以把這個變成一個像這樣的自定義函數:

<xsl:function name="di:findMeta"> 
<xsl:param name="meta" as="element()*" /> 
<xsl:param name="names" as="xs:string" /> 

<xsl:for-each select="tokenize(normalize-space($names),',')"> 
    <xsl:sequence select="$meta[matches(@domain 
             ,concat('.*(' 
               ,current() 
               ,').*') 
             ,'i')][1]" /> 
</xsl:for-each> 
</xsl:function> 

然後像這樣使用它:

<xsl:value-of select="di:findMeta($di:meta,'foo,bar,baz')[1]"/> 
+0

謝謝!那工作 – 2011-05-18 12:45:43