繁体   English   中英

XQuery 不会返回文档

[英]XQuery wont return a document

我刚开始学习 XQuery 并尝试使用它。 现在,iv'e 构建了我认为是一个简单的查询和一个正确的查询(据我所知),但是 SQL Manger 向我打印错误:XQuery [query()]: "=" 是预期的。 我回顾了代码,但我无法理解如何修复它。这是我的代码:

DECLARE @x AS XML = N'
<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" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>';

SELECT @x.query('for $i in bookstore/book
    let $a := $i/author
    let $t := $i/title
    let $p := $i/price
    where $i/@price < 40
    return
    <This is a test>
    <author>$a</author>
    <title>$t</title>
    <price>$p</price>
    </This is a test>')
AS [I hope this works]

在此先感谢您的帮助:)

这不是有效的 xml 元素:

<This is a test>

将其更改为

<ThisIsATest>

将修复您的查询,但在修复后,您的查询不会返回任何内容。 我的 T-SQL XQuery 技能生疏了,但是您可以使用nodesvalues方法来获得您想要做的事情。

这个:

SELECT 
  author = bs.book.value('(author/text())[1]', 'varchar(8000)'),
  title  = bs.book.value('(title/text())[1]', 'varchar(8000)'),
  price  = bs.book.value('(price/text())[1]', 'varchar(8000)')
FROM        (VALUES(@x))                 AS ml(x)
CROSS APPLY ml.x.nodes('bookstore/book') AS bs(book);

回报:

author                title                 price
--------------------- --------------------- ----------
Giada De Laurentiis   Everyday Italian      30.00
J K. Rowling          Harry Potter          29.99
James McGovern        XQuery Kick Start     49.99
Erik T. Ray           Learning XML          39.95

(包括您的示例 XML):

DECLARE @x AS XML = N'
<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" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>';

SELECT 
  author = bs.book.value('(author/text())[1]', 'varchar(8000)'),
  title  = bs.book.value('(title/text())[1]', 'varchar(8000)'),
  price  = bs.book.value('(price/text())[1]', 'varchar(8000)')
FROM        (VALUES(@x))                 AS ml(x)
CROSS APPLY ml.x.nodes('bookstore/book') AS bs(book)
FOR XML PATH('Book');

回报:

<Book>
  <author>Giada De Laurentiis</author>
  <title>Everyday Italian</title>
  <price>30.00</price>
</Book>
<Book>
  <author>J K. Rowling</author>
  <title>Harry Potter</title>
  <price>29.99</price>
</Book>
<Book>
  <author>James McGovern</author>
  <title>XQuery Kick Start</title>
  <price>49.99</price>
</Book>
<Book>
  <author>Erik T. Ray</author>
  <title>Learning XML</title>
  <price>39.95</price>
</Book>

你很接近,但有三件事要提:

  1. 正如 Alan Burstein 告诉您的,标签<This is a test>无效。 您收到的消息是因为引擎考虑了一个元素<This>并期望is是第一个属性,应该跟在 <This is = <This is="some value">之后。
  2. 您的where尝试查询@price属性
  3. 为了解决您需要{}围绕变量的值。

尝试这个:

SELECT @x.query('for $i in bookstore/book
    let $a := $i/author
    let $t := $i/title
    let $p := $i/price
    where $p < 40
    return
    <This-is-a-test>
    <author>{$a}</author>
    <title>{$t}</title>
    <price>{$p}</price>
    </This-is-a-test>')
AS [I hope this works]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM