2011-05-18 51 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