C#在规则中指定哪种查询语言以拉出两个书节点 [英] C# What query language to specify in rule to pull out the two book nodes
问题描述
<?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>
当前,我有一个基于规则的系统,其中将传入的xml消息与一条规则进行匹配,如果命中该规则,则将处理该数据包.为了获得匹配,我使用xpath在xml中选择单个值,然后在规则中将它们指定为组合的xpath:regexp表达式,类似这样.
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]/标题:(.+)
/bookstore/book[1]/ title: (.+)
例如,上面的代码与"Everyday Italian"(每天的意大利语)相匹配
For example the above would match against the "Everyday Italian"
但是我试图找到一个查询或一个新的查询语言表达式,这将允许我选择上述经典msdn docs book.xml的所有书节点,例如,如果我在规则中指定查询表达式,我可以使用解析器将其提起,然后直接将其用于xml文件以拉出books节点.
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.
我不知道xpath是否可以做到.我在看XQuery,但看起来有些罗word.XSLT可能可以做到这一点,但这很罗word.有任何想法吗.
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.
它们是指定表达式的简单方法,这样它将抬起所有书本节点,或者说1和2,或者说1st和3rd.
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.
谢谢.鲍勃.
推荐答案
使用xquery和flwor:
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$
where子句可以找到每个书本节点的位置,并只返回特定的节点.
the where clause can find the position of each book node, and just return specific nodes.
这篇关于C#在规则中指定哪种查询语言以拉出两个书节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!