0
<?xml version="1.0" encoding="ISO-8859-1"?>
  <bookstore>

      <book category="COOKING"> 
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>    
        <year>2005</year>
       <price>30.00</price>
      </book>

      <book category="CHILDREN">
       <title lang="en">Harry Potter</title>
       <author>J K. Rowling</author>
       <year>2005</year>
       <price>29.99</price>
      </book>

      <book category="WEB">
       <title lang="en">XQuery Kick Start</title>
       <author>James McGovern</author>
       <author>Per Bothner</author>
       <author>Kurt Cagle</author>
       <author>James Linn</author>
       <author>Vaidyanathan Nagarajan</author>
       <year>2003</year>
       <price>49.99</price>
      </book>

      <book category="WEB">
       <title lang="en">Learning XML</title>
       <author>Erik T. Ray</author>
       <year>2003</year>
       <price>39.95</price>
      </book>

    </bookstore>

Hi, Currently, I have a rules based system, where incoming xml messages are matched against a rule, and if the rule hits, the packet is processed. To get a match I use xpath to select individual values in the xml, and I specify these in the rule as combined xpath:regexp expressions, something like this.

/bookstore/book[1]/ title: (.+)

For example the above would match against the "Everyday Italian"

But I'm trying to find a query or perhaps a new query language expression which will allow me to select all the book nodes for the above classic msdn docs book.xml, such that if I specify the query expression in the rule, I can lift it using a parser, and use it directly against the xml file to pull out the books node.

I don't know if xpath can do it. I was looking at XQuery, but it seems wordy. XSLT could probably do it, but it's wordy. Any ideas.

Is their a simple way of specifying an expression such that it would lift all book nodes, or say 1 and 2, or 1st and 3rd.

Thanks. Bob.

scope_creep
  • 4,213
  • 11
  • 35
  • 64
  • Why are you looking to pull results by index and not by some other criterion like title keywords or year or something? – Welbog Sep 29 '09 at 16:35
  • Well, in some cased it may be values, but in other cases i'll be wanting to capture by index. Also the xml i'm showing you here, is not the true representation of the the xml i'm working against. I'm not able to show it. Any ideas how to do it. – scope_creep Sep 29 '09 at 17:15
  • I don't think it needs to be pulled from index. If the expression selects category="WEB" then I would like to select all the children in the node. But I would like to allow the rule writer the choice of selecting by index. At the moment, i'm looking to get design sorted. Bob. – scope_creep Sep 29 '09 at 18:32

2 Answers2

0

The following questions, which of a different design, answered it indirectly.

XPath Node Select

Community
  • 1
  • 1
scope_creep
  • 4,213
  • 11
  • 35
  • 64
0

using xquery and flwor:

nicholas@mordor:~/flwor/position$ 
nicholas@mordor:~/flwor/position$ basex position.xq 
<book category="children">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="web">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>nicholas@mordor:~/flwor/position$ 
nicholas@mordor:~/flwor/position$ 
nicholas@mordor:~/flwor/position$ cat position.xq 

xquery version "3.1";

let $doc := doc("bookstore.xml")
let $books := $doc/bookstore/book

for $book at $p in $books 
where $p gt 1 and $p lt 4
return $book
nicholas@mordor:~/flwor/position$ 
nicholas@mordor:~/flwor/position$ cat bookstore.xml 
<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="web">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>nicholas@mordor:~/flwor/position$ 

the where clause can find the position of each book node, and just return specific nodes.