XPath和XSL转换用XPath表达式查询XML

如何使用 XPath 表达式查询 XML

此示例阐释如何使用提供给 XPathNavigator 类的 W3C XML 路径语言 (XPath) 表达式查询 XPathDocument。XPathNavigator 类仅用于对文档进行只读 XPath 查询,而对于可扩展样式表语言转换 (XSLT) 处理,则由 XslTransform 类实现。对于 XSLT,不需要创建 XPathNavigator。

注意:XPath 是 W3C 的通用查询语言规范,用于对 XML 文档的某些部分进行寻址。XPath 的 .NET 框架实现符合 WWW 联合会 (W3C) XML 路径语言 (XPath) 1.0 版规范。

要使用 XSLT 对 XML 文档进行快速和高效的处理,请使用 XPathDocument 类。可以认为 XPathDocument 类与 XML DOM 相似,但前者为 XSLT 处理及 XPath 数据模型进行了高度优化。然而,与 W3C XML DOM 类不同的是,XPathDocument 类不维护节点标识,也不执行 DOM 类所要求的所有规则和验证检查。

您也可以针对 XmlDocument 或 XmlDataDocument 类执行 XPath 查询。如果您使用这两个类中的一个,则需要使用 XmlNode 类的 SelectNodes 和 SelectSingleNode 方法。

 
VB QueryXmlDocumentXPath.aspx

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

此示例加载一个包含示例文件 books.xml 的 XPathDocument。然后,该示例将 XPath 表达式传递给 XPathQuery 函数。要传递的第一个 XPath 表达式选择从根节点开始的所有书籍元素节点的所有价格节点。第二个 XPath 表达式获取从根节点开始的、与最后的书籍元素节点关联的 ISBN 属性节点的文本。

要选择与给定 XPath 表达式匹配的节点,XPathQuery 函数使用 XPathNavigator 的 Select 方法。然后,该函数创建一个 XPathNodeIterator,并使用此迭代程序重复定位某个节点并把该节点显示在屏幕上。

注意:因为 XPathNodeIterator 表示一个 XPath 节点集,所以 XPathNodeIterator 支持在此节点集上的操作。

private const String localURL = "http://localhost/quickstart/howto/samples/Xml/QueryXmlDocumentXPath/cs/books.xml";
            public static void Main()
            {
            QueryXmlDocumentXPathSample myQueryXmlDocumentXPathSample = new QueryXmlDocumentXPathSample();
            myQueryXmlDocumentXPathSample.Run(localURL);
            }
            public void Run(String args)
            {
            Console.WriteLine("XPath Test started ...");
            XPathDocument myXPathDocument = new XPathDocument(args);
            XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();
            // Get all the book prices
            XPathQuery(myXPathNavigator, "descendant::book/price");
            // Get the ISBN of the last book
            XPathQuery(myXPathNavigator, "bookstore/book[3]/@ISBN");
            }
            private void XPathQuery(XPathNavigator myXPathNavigator, String xpathexpr )
            {
            try
            {
            Console.WriteLine("XPath query: " + xpathexpr);
            // Create a node interator to select nodes and move through them (read-only)
            XPathNodeIterator myXPathNodeIterator =  myXPathNavigator.Select (xpathexpr);
            while (myXPathNodeIterator.MoveNext())
            {
            Console.WriteLine("<" + myXPathNodeIterator.Current.Name + "> " + myXPathNodeIterator.Current.Value);
            }
            Console.WriteLine();
            }
            catch (Exception e)
            {
            Console.WriteLine ("Exception: {0}", e.ToString());
            }
            }
            
C# VB  

第一个查询选择从根节点开始的所有书籍的“价格”节点。第二个查询获取从根节点开始的最后的书籍元素节点的“ISBN”属性文本。

也可使用 XPathExpression 类执行查询。使用 XPathExpression 类而不使用 Select 方法(在 XPathNavigator 上)中的字符串表达式的原因主要有两个:
1) 表达式只编译一次,并可重复使用多次,这样可提高性能
2) 可使用 XmlNamespaceManager 类将 XPath 表达式中使用的前缀绑定到某个命名空间,这样当选择一组节点时,就允许使用带前缀的 XPath 表达式。
下列代码显示一般用法,即选择其 ISBN 属性在 urn:samples 命名空间中的所有书籍。

// compile the expression
            XPathExpression expr = nav.Compile("book/@mybk:ISBN");
            // set namespace(s) used in the expression
            XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());
            mngr.AddNamespace("mybk","urn:samples");
            expr.SetContext(mngr);
            // select the nodes
            XPathNodeIterator myXPathNodeIterator = nav.Select(expr);
            
C# VB  

如果 XPathExpression 没有前缀,则假设该命名空间 URI 为空的命名空间。如果 XML 包括默认的命名空间,则必须仍然使用 SetContext 方法,并提供一个包含前缀和命名空间 URI 的 XmlNamespaceManager,以处理默认命名空间。有关使用 XmlNamespaceManager 的更多信息,请参阅“XmlNameSpace 管理器与命名表”主题。

摘要

  1. 要查询 XPathDocument、XmlDataDocument 或 XmlDocument,可以为 XPathNavigator 类提供 XPath 表达式。
  2. XPathNavigator 类仅用于对 XML 文档进行只读 XPath 查询。
  3. XPathDocument 类为使用 XSLT 处理 XML 文档提供快速、高效的特定缓存。
原文地址:https://www.cnblogs.com/chorrysky/p/584502.html