1
次のコードは動作しますが、xpathで指定された要素の出現が影響を受ける(置き換えられた、削除された、挿入されたなどの)パラメータとして指定します。再帰的なXQuery関数で一致する要素を数える方法
declare function local:replace($doc as node(), $new-content as item()*,
$target-path as xs:string) {
local:transform($doc, $new-content, $target-path, 'replace', "")
};
declare function local:transform($node as node(), $new-content as item()*,
$target-path as xs:string, $action as xs:string, $path as xs:string)
as node()+ {
if ($node instance of element() and concat($path, "/", node-name($node)) = $target-path)
then
if ($action = 'insert-before')
then ($new-content, $node)
else
if ($action = 'insert-after')
then ($node, $new-content)
else
if ($action = 'insert-as-child')
then element {node-name($node)}
{
$node/@*
,
$new-content
,
for $child in $node/node()
return $child
}
else
if ($action = 'replace')
then $new-content
else
if ($action = 'delete')
then()
else()
else
if ($node instance of element())
then
element {node-name($node)}
{
$node/@*
,
for $child in $node/node()
return
local:transform($child, $new-content, $target-path, $action, concat($path, "/", node-name($node)))
}
else $node
};
let $doc :=
<fo:Test xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:books>
<!-- These are my books -->
<book title='xQuery for Dummys'>
<author>Jack Wizard</author>
<details>
<pages>221</pages>
</details>
</book>
<book title='Mysteries of xQuery'>
<author>Lost Linda</author>
<details>
<replace-this>Goodbye World!</replace-this>
<pages>40</pages>
</details>
</book>
</fo:books>
</fo:Test>
(: -------- My test -------- :)
let $new-content := <replaced>Hello World!</replaced> return
local:replace($doc, $new-content, '/fo:Test/fo:books/book/details/replace-this')
明らかに$global-counter = $global-counter + 1
は動作しません。代わりに、私はlocal:replace($doc, $new-content, '/fo:Test/fo:books/book/details/pages')
<fo:Test xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:books>
<!-- These are my books -->
<book title='xQuery for Dummys'>
<author>Jack Wizard</author>
<details>
<replace-this>Goodbye World!</replace-this>
</details>
</book>
<book title='Mysteries of xQuery'>
<author>Lost Linda</author>
<details>
<replace-this>Goodbye World!</replace-this>
<replaced>Hello World!</replaced>
</details>
</book>
</fo:books>
</fo:Test>
を使用して取得したい、この不要な結果の
<fo:Test xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:books>
<!-- These are my books -->
<book title='xQuery for Dummys'>
<author>Jack Wizard</author>
<details>
<pages>221</pages>
</details>
</book>
<book title='Mysteries of xQuery'>
<author>Lost Linda</author>
<details>
<replace-this>Goodbye World!</replace-this>
<replaced>Hello World!</replaced>
</details>
</book>
</fo:books>
</fo:Test>
:
local:replace($doc, $new-content, '/fo:Test/fo:books/book/details/pages', 2)
そして、この出力を得る:私はこのような別のパラメータとして、私が欲しいの発生に渡したいです
私が得たポジティブな一致の数をカウントして、それを条件に追加する方法はありますか?私は困惑している!
私の理解では、 '/ fo:Test/fo:books/book/details/pages [2]'はサンプルXMLの中に何も選択しません。 2番目の 'ページ'子。 –
しかし、どの要素がそのパスで選択されるはずですか?/ fo:Test/fo:books/book/details/pages [2] '?あなたのサンプルではありません。たとえば、/fo:Test/fo:books/book [2]/details/pages'または '/ fo:Test/fo:books/book/details/pages [2]'のようになります。 –
私はそれが欲しいと望むことを選んでいます。入力の構文を定義するのは私の責任です。これは、別のパラメータを作成せずにオカレンス値を渡す単なる方法です。しかし、私の望む出力に示されているように、再帰関数でなく単純なxpathの選択問題でなければ '(/ fo:Test/fo:books/book/details/pages)[2]' –