2010-12-17 1 views
4

xmlファイルに特定のキーワードを含むすべてのテキストノードを見つけるXQueryを作成しようとしています。テキストノードは長いので、一致するキーワードから始まるテキストの部分文字列(希望する長さの文字列)を返したいと思います。XQueryでfunctx:index-of-match-firstを使用してテキストノードの部分文字列を返します。

Samplefile.xml

<books> 
<book> 
    <title>linear systems</title> 
    <content>vector spaces and linear system analysis </content> 
</book> 
<book> 
    <title>some title</title> 
    <content>some content</content> 
</book> 
</books> 

samplexquery.xq

declare namespace functx = "http://www.functx.com"; 

for $match_result in /*/book/*[contains(.,'linear')]/text() 
    return substring($match_result, functx:index-of-match-first($match_result,'linear'), 50) 

私は結果[線形システム、線形システム解析]を得ることを期待。最初の本のタイトルノードには「線形」という単語が含まれています。 'linear ....'から50文字を返します。最初の本のコンテンツノードについても同様です。

私は、XQuery 1.0を使用していますが、私が例に示すように、名前空間のfunctxを含ま:http://www.xqueryfunctions.com/xq/functx_index-of-match-first.html

しかし、これは私にエラーを与えている:[XPST0017]不明な機能「functx:インデックスの-match-最初(...)"。

おかげで、 ソニー

+0

良い質問、+1。説明と解決方法については私の答えを見てください。 :) –

答えて

2

I am using XQuery 1.0 and I included the namespace functx as shown in the example at: http://www.xqueryfunctions.com/xq/functx_index-of-match-first.html

But, this is giving me an error: [XPST0017] Unknown function "functx:index-of-match-first(...)".

それが唯一の名前空間を宣言するのに十分ではありません。

また、機能のコードも必要です。標準XQuery and XPath functions and operatorsのみが言語であらかじめ定義されています。

これはコード補正(修正いくつかの非整形式エラーで)提供されるXML文書に適用した場合

declare namespace functx = "http://www.functx.com"; 
declare function functx:index-of-match-first 
    ($arg as xs:string? , 
    $pattern as xs:string) as xs:integer? { 

    if (matches($arg,$pattern)) 
    then string-length(tokenize($arg, $pattern)[1]) + 1 
    else() 
} ; 

for $match_result in /*/book/*[contains(.,'linear')]/text() 
    return substring($match_result, functx:index-of-match-first($match_result,'linear'), 50) 

を:

<books> 
    <book> 
    <title>linear systems</title> 
    <content>vector spaces and linear system analysis </content> 
    </book> 
    <book> 
    <title>some title</title> 
    <content>some content</content> 
    </book> 
</books> 

は、期待される結果を生成します

linear systems linear system analysis 

import moduleディレクティブを使用して、既存の関数ライブラリからモジュールをインポートすることをお勧めします。

+0

ああ、私はこれらがあらかじめ定義された機能だと思った。彼らは再利用可能な関数のように見えます。ありがとうございます:) – sony

+0

@sony: 'import module'ディレクティブを使用して、既存の関数ライブラリからモジュールをインポートすることをお勧めします –

関連する問題