return 相同的 XML Doc 结构,在 XPath 中具有特定条件
return the same XML Doc structure with specific conditions in XPath
我在第 http://www.w3schools.com/xml/xml_xpath.asp
页中有 xml 个类似于 xml 的文档
<?xml version="1.0" encoding="UTF-8"?>
<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>
问题是如何return这个文件与元素对应的某个值?应该如何编写 XPath 或 XQuery 命令?
例如搜索标题包含 'Learning' 那么 return xml 文档应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
如何得到这个结果?
另一个问题是如何忽略字符大小写进行搜索,所以 'learNING' 应该 return 得到相同的结果吗?
使用XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> book = doc.Descendants("book").Where(x => x.Element("title").Value.Contains("Learn")).ToList();
XDocument filteredDoc = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><bookstore></bookstore>");
XElement root = (XElement)filteredDoc.FirstNode;
root.Add(book);
}
}
}
for $d in doc('books')//book[title[contains(text(),'Beginning')]]
return <bookstore> {$d} </bookstore>
但是,此解决方案无法处理忽略字符大小写的问题。
使用 XQuery,您可以执行以下操作:
<bookstore>
{
for $d in //book[contains(lower-case(title),'learning')]
return $d
}
</bookstore>
注意只有一个 <bookstore>
包装返回的所有匹配 <book>
元素,并注意 lower-case()
函数对 'ignore' 字符大小写的使用匹配过程中的书名。
我在第 http://www.w3schools.com/xml/xml_xpath.asp
页中有 xml 个类似于 xml 的文档<?xml version="1.0" encoding="UTF-8"?>
<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>
问题是如何return这个文件与元素对应的某个值?应该如何编写 XPath 或 XQuery 命令?
例如搜索标题包含 'Learning' 那么 return xml 文档应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
如何得到这个结果?
另一个问题是如何忽略字符大小写进行搜索,所以 'learNING' 应该 return 得到相同的结果吗?
使用XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> book = doc.Descendants("book").Where(x => x.Element("title").Value.Contains("Learn")).ToList();
XDocument filteredDoc = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><bookstore></bookstore>");
XElement root = (XElement)filteredDoc.FirstNode;
root.Add(book);
}
}
}
for $d in doc('books')//book[title[contains(text(),'Beginning')]]
return <bookstore> {$d} </bookstore>
但是,此解决方案无法处理忽略字符大小写的问题。
使用 XQuery,您可以执行以下操作:
<bookstore>
{
for $d in //book[contains(lower-case(title),'learning')]
return $d
}
</bookstore>
注意只有一个 <bookstore>
包装返回的所有匹配 <book>
元素,并注意 lower-case()
函数对 'ignore' 字符大小写的使用匹配过程中的书名。