【问题标题】:C# What query language to specify in rule to pull out the two book nodesC#在规则中指定什么查询语言来拉出两个书节点
【发布时间】:2010-12-02 09:26:07
【问题描述】:
<?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 表达式,如下所示。

/bookstore/book[1]/标题:(.+)

例如,上面将匹配“Everyday Italian”

但我正在尝试找到一个查询或者可能是一个新的查询语言表达式,这将允许我为上述经典 msdn docs book.xml 选择所有书籍节点,这样如果我在规则中指定查询表达式,我可以使用解析器将其提升,并直接针对 xml 文件使用它来拉出书籍节点。

我不知道 xpath 是否可以做到。我在看 XQuery,但它似乎罗嗦。 XSLT 可能可以做到,但它很冗长。有任何想法吗。

他们是一种简单的方法来指定一个表达式,这样它就可以提升所有书籍节点,或者说 1 和 2,或者 1 和 3。

谢谢。 鲍勃。

【问题讨论】:

  • 您为什么要按索引而不是按其他标准(如标题关键字或年份之类)提取结果?
  • 好吧,在某些情况下它可能是值,但在其他情况下,我希望通过索引捕获。此外,我在这里向您展示的 xml 并不是我正在处理的 xml 的真实表示。我无法展示它。任何想法如何做到这一点。
  • 我认为不需要从 index.html 中提取它。如果表达式选择 category="WEB" 那么我想选择节点中的所有子节点。但我想允许规则编写者选择按索引进行选择。目前,我正在寻找对设计进行排序。鲍勃。

标签: xpath c#-3.0 xquery


【解决方案1】:

以下问题的设计不同,间接回答了它。

XPath Node Select

【讨论】:

    【解决方案2】:

    使用 xquery 和 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子句可以找到每个书节点的位置,只返回具体的节点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 2012-03-14
      • 2011-10-04
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多