怎样用xpath一次选出(price/pageCount)值最小的book结点

怎样用xpath一次选出(price/pageCount)值最小的book结点
string xpath = "";//求此xpath的值
XmlNode root = m_xmlDoc.DocumentElement;
XmlNode node = root.SelectNodes(xpath);

 1<books>
 2<book id="1">
 3<title>SAP 程序设计</title>
 4<subtitle>SAP Programming</subtitle>
 5<publicationdate>2005年4月</publicationdate>
 6<publisher>机械工业出版社</publisher>
 7<isbn>7-111-15935-7</isbn>
 8<edition></edition>
 9<author>黄佳</author>
10<pagecount>600</pagecount>
11<description>国内SAP方面的第一本专著</description>
12<price>55</price>
13</book>
14<book id="2">
15<title>人月神话</title>
16<subtitle>软件类书籍神话</subtitle>
17<publicationdate>1985年4月</publicationdate>
18<publisher>机械工业出版社</publisher>
19<isbn>7-111-15935-7</isbn>
20<edition>2</edition>
21<author>不是人</author>
22<pagecount>300</pagecount>
23<description>软件工程专著</description>
24<price>55</price>
25</book>
26<book id="3">
27<title>设计模式</title>
28<subtitle>你要懂我</subtitle>
29<publicationdate>1995年4月</publicationdate>
30<publisher>机械工业出版社</publisher>
31<isbn>7-111-15935-7</isbn>
32<author>不是人</author>
33<pagecount>300</pagecount>
34<description>软件方法学专著</description>
35<price>30</price>
36</book>
37</books>

---------------------------------------------------------------

price div pageCount is not a valid node, so it is not possible to do the following in XPATH 1.0:

price div pageCount > //book/price div //book/pageCount

you probably need to write your function to sort on price/pageCount, for excample, like

XmlNode theNode = null;
double d = Double.MaxValue;
foreach(XmlNode node in root.SelectNodes("//book"))
{
double v = Convert.ToDouble(node["price"].InnerText)/Convert.ToDouble(node["pageCount"].InnerText);
if (v < d)
{
d = v;
theNode = node;
}
}

//....
---------------------------------------------------------------

使用 XPathNavigator:

XPathDocument xpathdoc=new XPathDocument(Server.MapPath("test.xml"));
XPathNavigator nav=xpathdoc.CreateNavigator();
XPathExpression express=nav.Compile("//book");
XPathExpression ex=nav.Compile("price div pageCount");
express.AddSort(ex,XmlSortOrder.Descending,XmlCaseOrder.None,"",XmlDataType.Number);
XPathNodeIterator iter=nav.Select(express);
while(iter.MoveNext())
{
XPathNavigator nav2=iter.Current.Clone();
nav2.MoveToFirstChild();
nav2.MoveToFirstChild();
Response.Write(nav2.Value);
Response.Write("

1<br/>

");
}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus