• XPath和XSL转换使用XPathExpression类


    如何使用 XPathExpression 类

    此示例阐释如何使用 XPathExpression 类编译 XML 路径语言 (XPath) 表达式,以从 XML 文件中选择节点集。然后,该示例说明如何添加自定义排序例程,并使用 XPathExpression 对编译的 XPath 所返回的元素进行排序。最后,它还说明如何使用可扩展样式表语言转换 (XSLT) 参数,以重复使用编译的 XPath 的方式进行排序。

    此示例使用 XPathNavigator 类,该类提供对 XPathDocument 进行光标样式的浏览。此示例还使用表示已编译的 XPath 表达式的 XPathExpression 类。该 XPathExpression 类是从 XPathNavigator 的 Compile 方法返回的,并被提供给 Select、Evaluate 和 Matches 方法。

     
    VB XPathExpression.aspx

    [运行示例] | [查看源代码]

    首先,该示例编译一个 XPath 表达式,以从 XML 文件选择节点集。为此,该示例先创建一个 XPathDocument,并将 books.xml 文件加载到该对象中。然后,该示例从 XPathDocument 创建一个 XPathNavigator。该示例使用 XPathNavigator 对象在使用 XPathNodeIterator 的文档中移动。

    XPathDocument myXPathDocument = new XPathDocument(args);
                //Create an XPathNavigator
                XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();
                
    C# VB  

    在创建了选择 XPathDocument 中的书籍元素的 XPath 表达式之后,该示例将表达式编译到 XPathNavigator 中,然后使用 Select 方法执行该 XPath 表达式。该示例使用 FormatXml 函数显示得到的 XML 数据。通过将 XPathNodeIterator 传递到此函数,此函数可连续地选择各个节点以进行格式化。

    // Get the Book elements
                selectExpr = "bookstore/book";
                ...
                // Ensure we are at the root node
                myXPathNavigator.MoveToRoot();
                myXPathExpr = myXPathNavigator.Compile(selectExpr);
                ...
                // Create an XPathNodeIterator to walk over the selected nodes
                XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select (myXPathExpr);
                FormatXml(myXPathNodeIterator);
                
    C# VB  

    此示例的第二部分显示如何使用 XPath 表达式对节点集进行排序。

    第二部分使用在该示例的第一部分中已编译的相同 XPathExpression 来选择书籍元素。然而,第二部分中的代码添加了一个自定义排序例程,以按 ISBN 属性的属性值对结果进行排序。然后,该示例使用 FormatXml 函数和 XPathNodeIterator 显示得到的 XML。

    XPathExpression 的 AddSort 方法支持一般排序功能,XSLT 处理器将该功能应用于节点集中的节点。该排序操作通过 IComparer 接口进行。该接口提供 Compare 方法的实现,该方法比较对象的数据类型。

    // Create comparision class for the ISBN number
                ISBNCompare myISBNCompare = new ISBNCompare();
                // Add a comparer to do a string compare on the ISBN number
                myXPathExpr.AddSort("@ISBN", (IComparer)myISBNCompare);
                myXPathNodeIterator = myXPathNavigator.Select (myXPathExpr);
                FormatXml(myXPathNodeIterator);
                
    C# VB  

    为了在示例的最后一部分演示通过 XSLT 参数进行排序,使用了一个自定义排序例程,该例程使用 XPathExpression 的 AddSort 方法实现新的 IComparer 例程。有了此自定义排序功能后,您就可以对用户定义的类进行排序了。这对于不方便进行比较的数据(如字符串)非常有用。

    public class ISBNCompare : IComparer
                {
                public int Compare( Object First, Object Second )
                {
                String s1 = (String) First;
                String s2 = (String) Second;
                //Console.WriteLine("ISBN's to Compare: 1. {0} 2. {1}", s1, s2);
                return s1.ToString().CompareTo (s2.ToString ());
                }
                }// End class ISBNCompare
    C# VB  

    然后,该示例在 XPathDocument 上创建第二个 XPathNavigator,并将基于 XSLT 参数的排序添加到 XPathExpression。

    XPathExpression 支持 功能,并将指定的排序条件添加到 XPath。调用 XPathExpression 时,XSLT 处理器根据所提供的值对节点集中的节点进行排序。通常情况下,可通过 XSLT 文件中的 元素使用排序方法。

    // Create Second XPathNavigator
                XPathNavigator myXPathNavigator2 = myXPathDocument.CreateNavigator();
                // Ensure we are at the root node
                myXPathNavigator2.MoveToRoot();
                myXPathExpr = myXPathNavigator2.Compile(selectExpr);
                ...
                // Add an XSLT based sort
                myXPathExpr.AddSort("@publicationdate", XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Text);
                myXPathNodeIterator = myXPathNavigator2.Select (myXPathExpr);
                FormatXml(myXPathNodeIterator);
                
    C# VB  

    摘要

    1. 使用 XPathExpression 类编译 XPath 表达式来从 XML 文件中选择节点集。
    2. 使用 XPathNavigator 提供对 XPathDocument 的快速浏览。
  • 相关阅读:
    Python 小试牛刀
    Python 流程控制
    Python
    CMDB
    CMDB
    CMDB
    C#学习日志 day 2 plus ------ hyper-V 开启方法
    C#学习日志 day 2 ------ 控制台颜色以及windowsphone 窗体应用试建
    C#学习日志 day 1 ------ hello C# !
    wamp出现You don’t have permission to access/on this server提示(转)
  • 原文地址:https://www.cnblogs.com/chorrysky/p/584507.html
Copyright © 2020-2023  润新知