1

要素に対して動的に「一致」する xslt 関数を作成しようとしています。この関数では、item()* とカンマ区切りの文字列の 2 つのパラメーターを渡します。select ステートメントでカンマ区切りの文字列をトークン化<xsl:for-each>し、次の操作を行います。

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

selectステートメントがxqueryを「実行」する代わりに、文字列を返すだけです。

xqueryを実行するにはどうすればよいですか?

前もって感謝します!

4

1 に答える 1

1

問題は、関数内にあまりにも多くの式をラップしていることですconcat()。それが評価されると、REGEX 一致式に動的文字列を使用する XPath 式を評価するのではなく、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]"/>
于 2011-05-18T12:19:05.090 に答える