search - Searching HTML with XQuery and returning the parent node -
i've inherited xquery app running in exist-db , i've been able learn enough xquery tidy bit, i'm struggling final change need do.
the app has html file stored in collection in following format:
<html> <head>...</head> <body> <div class="part"> <div class="chapter"> <div class="section"> <p>text 1</p> <p>text 2</p> <p>text 3</p> </div> <div class="section"> <p>text 4</p> <p>text 5</p> <p>text 6</p> </div> </div> </div> ... <div class="part"> <div class="chapter"> <div class="section"> <p>text 1</p> <p>text 2</p> <p>text 3</p> </div> <div class="section"> <p>text 1</p> <p>text 2</p> <p>text 3</p> </div> </div> </div> ... </body> </html>
the <body>
has multiple "part"s, each "part" having multiple "chapter"s can have multiple "section"s etc.
i'm trying search feature, , i've got following code works, not need to:
declare function app:search-result($node node(), $model map(*), $q xs:string) { let $content := doc("/db/apps/ei/resources/publications/ei.html")) $hit in $content//p[ft:query(., concat("'", $q, "'"))] order ft:score($hit) descending return $hit };
at moment function returns pure text within <p>
found, need find out every part, chapter , section divs found in
e.g., if search "text 1" need know found in:
1st part, 1st chapter, 1st section
2nd part, 1st chapter, 1st section
2nd part, 1st chapter, 2nd section
etc
this i'm stumped. ideas?
use parent
or ancestor
axes "climb down tree" again. if you're @ text node, , want parent node (containing text node), use $hit/parent::*
. if want find matching chapter, go $hit/ancestor::div[@class="chapter"]
(and other elements).
for case have nested chapters, add positional predicate [1]
find specific ancestor.
be aware class attribute might contain multiple classes, , needs special handling when matching it!
Comments
Post a Comment