XQuery: Return how many times a search term was found in a XML document -


i have collection of 2 xml files (tagged according tei standard , containing lots of <entry> elements) in exist database. i'm looping through file flwor routine, looking search term user typed in form ($searchterm) , returning contents of elements. far good. add thing, don't know how that:

i count how many <entry> had $searchterm in <form type="hyperlemma"> , print instead of xxx. if $searchterm equals "ангелъ", number "3". need print information before loop through collection, have no clue need do.

could help? i'm still new xquery, code isn't pretty, hints me improve appreciated, too!

my xquery:

xquery version "3.0"; declare namespace tei="http://www.tei-c.org/ns/1.0"; declare option exist:serialize "method=xhtml media-type=text/html"; declare variable $searchphrase := request:get-parameter("searchphrase", ()); declare variable $collection_path := "/db/apps/ex02/data"; <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>{$page-title}</title> </head> <body> <h1>{$page-title}</h1> <h2>results</h2> <p>your input: "{$searchphrase}"</p> <h3>found xxx entries</h3>    {     $file in collection($collection_path),         $hyperlemma in $file/(descendant::tei:entry | descendant::tei:cit)/tei:form[@type='hyperlemma']/tei:orth [ft:query(., $searchphrase)]         let $title := $hyperlemma/ancestor::*/tei:head         let $entry_number := $hyperlemma/ancestor::tei:entry[1]/@xml:id         let $lemma := $hyperlemma/ancestor::tei:entry/tei:form[@type='lemma']/tei:orth     return         <div>         {string($title)} ({data($entry_number)}):<br/>         <strong><font color="red">{string($lemma)}</font></strong><br/>         {             $counterpart in $hyperlemma/ancestor::tei:entry/(tei:form[@type='lemma'] | tei:form[@type='variant'])/tei:cit/tei:form[@type='lemma']/tei:orth             return             <font color="green">&#160;&#160;&#160;&#160;{string($counterpart)}<br/></font>         }         </div> } </body> 

these snippets of 2 xml files:

(a)

... <text> <head>euch.</head> <entry xml:id="pen-26">     <form type="hyperlemma" xml:lang="grc">         <orth>ἄγγελος</orth>     </form>     <form type="lemma" xml:lang="grc">         <orth>ἄγγελος</orth>         <cit type="counterpart" xml:lang="cu">             <form type="hyperlemma" xml:lang="cu">                 <orth>ангелъ</orth>             </form>             <form type="lemma" xml:lang="cu">                 <orth>аньꙉелъ</orth>             </form>         </cit>     </form> </entry> <entry xml:id="pen-336">     <form type="hyperlemma" xml:lang="grc">         <orth>ἀρχάγγελος</orth>     </form>     <form type="lemma" xml:lang="grc">         <orth>ἀρχάγγελος</orth>         <cit type="counterpart" xml:lang="cu">             <form type="hyperlemma" xml:lang="cu">                 <orth>ангелъ</orth>             </form>             <form type="lemma" xml:lang="cu">                 <orth>аньꙉелъ</orth>             </form>         </cit>     </form> </entry> </text> ... 

(b)

... <text> <head>syn.tr. [1]</head> <entry xml:id="tas-12"> <form type="hyperlemma" xml:lang="grc">     <orth>ἄγγελος</orth> </form> <form type="lemma" xml:lang="grc">     <orth>ἄγγελος</orth>     <cit type="counterpart" xml:lang="cu">         <form type="hyperlemma" xml:lang="cu">             <orth>ангелъ</orth>         </form>         <form type="lemma" xml:lang="cu">             <orth>ангєлъ</orth>         </form>     </cit>     <cit type="counterpart" xml:lang="cu">         <form type="hyperlemma" xml:lang="cu">             <orth>вѣстьникъ</orth>         </form>         <form type="lemma" xml:lang="cu">             <orth>вѣстьникъ</orth>         </form>     </cit> </form> </entry> </text> 

you need re-organise query , html output perform query before want output number of results , results display. can call fn:count on results number of results. note, here have package twice many things $results sequence, divide 2. example:

xquery version "3.0"; declare namespace tei="http://www.tei-c.org/ns/1.0"; declare option exist:serialize "method=xhtml media-type=text/html"; declare variable $page-title := "slavacomp-db"; declare variable $searchphrase := request:get-parameter("searchphrase", ()); declare variable $collection_path := "/db/apps/ex02/data"; <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>{$page-title}</title> </head> <body> <h1>{$page-title}</h1> {     let $results :=                       $file in collection($collection_path),             $hyperlemma in $file/(descendant::tei:entry | descendant::tei:cit)/tei:form[@type='hyperlemma']/tei:orth [ft:query(., $searchphrase)]         return             ($file, $hyperlemma)     return         let $count := count($results)          let $offsets :=             $i in (0 ($count div 2) cast xs:integer - (if($count mod 2 eq 0)then 1 else 0))              return                 2 * $i + 1         return         <div>             <h2>results</h2>             <p>your input: "{$searchphrase}"</p>             <h3>found {$count div 2} entries</h3>             {                 $offset in $offsets                 let $hyperlemma := $results[$offset + 1]                 let $title := $hyperlemma/ancestor::*/tei:head                 let $entry_number := $hyperlemma/ancestor::tei:entry[1]/@xml:id                 let $lemma := $hyperlemma/ancestor::tei:entry/tei:form[@type='lemma']/tei:orth                 return                     <div>                     {string($title)} ({data($entry_number)}):<br/>                     <strong><font color="red">{string($lemma)}</font></strong><br/>                     {                         $counterpart in $hyperlemma/ancestor::tei:entry/(tei:form[@type='lemma'] | tei:form[@type='variant'])/tei:cit/tei:form[@type='lemma']/tei:orth                         return                         <font color="green">&#160;&#160;&#160;&#160;{string($counterpart)}<br/></font>                     }                     </div>             }         </div> } </body> 

the query above complex pack both $file , $hyperlemma sequence, odd numbered offsets file , numbered offsets hyperlemma.

i notice though don't use $file in results, if don't need query simplified to:

xquery version "3.0"; declare namespace tei="http://www.tei-c.org/ns/1.0"; declare option exist:serialize "method=xhtml media-type=text/html"; declare variable $page-title := "slavacomp-db"; declare variable $searchphrase := request:get-parameter("searchphrase", ()); declare variable $collection_path := "/db/apps/ex02/data"; <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>{$page-title}</title> </head> <body> <h1>{$page-title}</h1> {     let $hyperlemmas := collection($collection_path)/(descendant::tei:entry | descendant::tei:cit)/tei:form[@type='hyperlemma']/tei:orth [ft:query(., $searchphrase)]     return         <div>             <h2>results</h2>             <p>your input: "{$searchphrase}"</p>             <h3>found {count($hyperlemmas)} entries</h3>             {                 $hyperlemma in $hyperlemmas                 let $title := $hyperlemma/ancestor::*/tei:head                 let $entry_number := $hyperlemma/ancestor::tei:entry[1]/@xml:id                 let $lemma := $hyperlemma/ancestor::tei:entry/tei:form[@type='lemma']/tei:orth                 return                     <div>                     {string($title)} ({data($entry_number)}):<br/>                     <strong><font color="red">{string($lemma)}</font></strong><br/>                     {                         $counterpart in $hyperlemma/ancestor::tei:entry/(tei:form[@type='lemma'] | tei:form[@type='variant'])/tei:cit/tei:form[@type='lemma']/tei:orth                         return                         <font color="green">&#160;&#160;&#160;&#160;{string($counterpart)}<br/></font>                     }                     </div>             }         </div> } </body> 

you might interested read kwic if want highlight various keyword searches matched in text: http://www.exist-db.org/exist/apps/doc/kwic.xml


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -