2

いいえで使用されるxmlタグの次の詳細を生成するためにXQueryを作成しようとしています。発生の

本 - 3 発生条 - 2 発生会議 - 1 発生

これは可能ですか?私は試してきましたが、うまくいきません

<test>
   <book>
        <year>1992</year>
   ....
   </book>
   <article>
        <year>1992</year>
   ....
   </article>
   <conference>
        <year>1992</year>
   ....
   </conference>
   <article>
        <year>1992</year>
   ....
   </article>
   <book>
        <year>1992</year>
   ....
   </book>
   <book>
        <year>1992</year>
   ....
   </book>
</test>
4

3 に答える 3

2

この純粋で単純なXPath2.0式を使用してください

for $name in distinct-values(/*/*/name())
 return
   ($name, count(index-of(/*/*/name(),$name)),'&#xA;')

この式が提供されたXMLドキュメントに対して評価される場合:

<test>
   <book>
        <year>1992</year>
   ....
   </book>
   <article>
        <year>1992</year>
   ....
   </article>
   <conference>
        <year>1992</year>
   ....
   </conference>
   <article>
        <year>1992</year>
   ....
   </article>
   <book>
        <year>1992</year>
   ....
   </book>
   <book>
        <year>1992</year>
   ....
   </book>
</test>

必要な正しい結果が生成されます。

 book 3
 article 2 
 conference 1 
于 2013-03-21T03:51:24.767 に答える
1

Here is an approach that should work if the group by syntax is not supported in the XQuery you are using:

for $dn in distinct-values(for $x in (/test/*) 
                           return local-name($x))
let $count := count(/test/*[local-name() = $dn])
order by $count descending
return ($dn, "-", $count, "occurrence(s) ")

The result from this is:

book - 3 occurrence(s) article - 2 occurrence(s) conference - 1 occurrence(s)
于 2013-03-20T15:21:06.700 に答える
0

私が理解していることから。「XQuery 3」ソリューションは次のとおりです。

for $x in (/test/*) 
    let $eltName := local-name($x)
    group by $eltName 
return ($eltName, "-",count($x), "occurrence(s) /")

提供された XML の場合、結果は次のようになります。

conference - 1 occurrence(s) / book - 3 occurrence(s) / article - 2 occurrence(s) /
于 2013-03-20T14:57:05.093 に答える