使用 XPath 排序 - 不是 XSL [英] Sort with XPath - Not XSL
问题描述
在 XPath DOM 编程中有什么方法可以使用 System.Xml
运行带有排序参数的 selectNodes
(XPATH)?
Is there any way in XPath DOM Programming with using System.Xml
to run selectNodes
(XPATH) with a sort parameter?
例如,使用以下 XML 和程序以与文档相同的顺序(降序)写入值.有没有办法使用 XPath 按升序获取值?
For example, with the following XML and program writes values in the same order as the document (descending). Is there a way using XPath to get the values in ascending order?
注意.当然,您可以在 XSL 中预先排序,但是我需要在循环遍历它们时更新这些值.由于 XSL 为我提供了元素的排序副本,不是实际元素本身,因此我无法使用 XSL.
NOTE. Of course, you could pre-sort in XSL, however I need to update the values as I'm looping through them. Since XSL gives me a sorted copy of the elements, not the actual elements themselves, I can't use XSL.
这是一些 XML,一个程序输出
Here's some XML, a program out output
public static void Main() {
XmlDocument xml = new XmlDocument();
xml.Load( "t.xml" );
// SelectNodes gets in document order, but I want in
// ascending order based on @value
foreach( XmlNode ndNode in xml.SelectNodes( "/xml/ele" ) ) {
Console.WriteLine( ndNode.Attributes["value"].Value );
}
}
这里是 XML
<xml>
<ele value='3' test='Third'/>
<ele value='2' test='Second'/>
<ele value='1' test='First'/>
</xml>
最后按文档(降序)顺序输出.我想要一个按升序返回节点的 XPath.
Finally the output in document (descending) order. I'd like an XPath that returns the nodes in ascending order.
3
2
1
PS,我在 Visual Studio 2008 .NET 3.5 中使用 System.Xml
PS, I'm using System.Xml
in Visual Studio 2008 .NET 3.5
推荐答案
XPath 不支持排序,但是您可以查看 AddSort
方法.
XPath doesn't supprort ordering, however you may look at AddSort
method.
这篇关于使用 XPath 排序 - 不是 XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!