> We have a book xml containing following tags:
>       Books, Chapter, Title, Subtitle, Para
> 
> for $x in cts:search(doc("C:\book.xml"),"Data2 on the Web")
> 
> The text "Data2 on the Web" can be anywhere in the book. How to pull out
> the chapter containing the text "Data2 on the Web" or the node
> containing this text.

There are a few ways to do this. If I understand your question properly,
one way would b e do perform a cts:word-query to run a phrase search on
the elements of interest.

For example, we could say that if we know we are interested in either
Chapter elements or elements which contain no children, we can check
for that criteria first, and then check for the phrase by combining
cts:contains and cts:word-query:

let $doc    := doc('/book.xml')
let $phrase := "Data2 on the Web"
for $element in
  $doc//*[node-name(.) eq xs:QName('Chapter') or count(*) = 0]
         [cts:contains(., cts:word-query($phrase))]
  return
    <match>{$element}</match>

Basically we first narrow our criteria down to Chapter elements or
elements which contain no child elements (count(*) = 0), and then check
to see if it contains the phrase.  You could reverse the predicates
of course.

Given the /book.xml:

<Books>
  <Chapter>
    <Title>Data2</Title>
    <SubTitle>Data2 on the Web</SubTitle>
    <Para>Data1</Para>
    <Para>Data2</Para>
    <Para>Data1 on the Web</Para>
    <Para>Data2 on the Web</Para>
  </Chapter>
  <Chapter>
    <Title>Data3</Title>
    <SubTitle>Data3 on the Web</SubTitle>
    <Para>Data1</Para>
    <Para>Data3</Para>
    <Para>Data1 on the Web</Para>
    <Para>Data3 on the Web</Para>
  </Chapter>
</Books>

The following 'match' elements are returned:

<match>
  <Chapter>
    <Title>Data2</Title>
    <SubTitle>Data2 on the Web</SubTitle>
    <Para>Data1</Para>
    <Para>Data2</Para>
    <Para>Data1 on the Web</Para>
    <Para>Data2 on the Web</Para>
  </Chapter>
</match>
<match>
  <SubTitle>Data2 on the Web</SubTitle>
</match>
<match>
  <Para>Data2 on the Web</Para>
</match>


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James A. Robinson                       [EMAIL PROTECTED]
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

Reply via email to