/// <summary> /// Implements the following function /// object dyn2:evaluate(node-set, string, string?) /// </summary> /// <param name="contextNode">Context node</param> /// <param name="expression">Expression to evaluate</param> /// <param name="namespaces">Namespace bindings</param> /// <returns>Result of evaluating given Xpath expression WRT to context node.</returns> /// <remarks>THIS FUNCTION IS NOT PART OF EXSLT!!!</remarks> public object Evaluate(XPathNodeIterator contextNode, string expression, string namespaces) { if (expression == string.Empty || contextNode == null) { return(string.Empty); } if (contextNode.MoveNext()) { try { XPathExpression expr = contextNode.Current.Compile(expression); ExsltContext context = new ExsltContext(contextNode.Current.NameTable); XPathNavigator node = contextNode.Current.Clone(); if (node.NodeType != XPathNodeType.Element) { node.MoveToParent(); } if (node.MoveToFirstNamespace()) { do { context.AddNamespace(node.Name, node.Value); } while (node.MoveToNextNamespace()); } if (namespaces != string.Empty) { try { Regex regexp = new Regex(@"xmlns:(?<p>\w+)\s*=\s*(('(?<n>.+)')|(""(?<n>.+)""))\s*"); Match m = regexp.Match(namespaces); while (m.Success) { try { context.AddNamespace(m.Groups["p"].Value, m.Groups["n"].Value); } catch { } m = m.NextMatch(); } } catch { } } expr.SetContext(context); return(contextNode.Current.Evaluate(expr, contextNode)); } catch { //Any exception such as syntax error in XPath return(string.Empty); } } //Empty nodeset as context node return(string.Empty); }
/// <summary> /// Implements the following function /// object dyn2:evaluate(node-set, string, string?) /// </summary> /// <param name="contextNode">Context node</param> /// <param name="expression">Expression to evaluate</param> /// <param name="namespaces">Namespace bindings</param> /// <returns>Result of evaluating given Xpath expression WRT to context node.</returns> /// <remarks>THIS FUNCTION IS NOT PART OF EXSLT!!!</remarks> public object evaluate(XPathNodeIterator contextNode, string expression, string namespaces) { if (expression == String.Empty || contextNode == null) return String.Empty; if (contextNode.MoveNext()) { try { XPathExpression expr = contextNode.Current.Compile(expression); ExsltContext context = new ExsltContext(contextNode.Current.NameTable); XPathNavigator node = contextNode.Current.Clone(); if (node.NodeType != XPathNodeType.Element) node.MoveToParent(); if (node.MoveToFirstNamespace()) { do { context.AddNamespace(node.Name, node.Value); } while (node.MoveToNextNamespace()); } if (namespaces != String.Empty) { try { Regex regexp = new Regex(@"xmlns:(?<p>\w+)\s*=\s*(('(?<n>.+)')|(""(?<n>.+)""))\s*"); Match m = regexp.Match(namespaces); while (m.Success) { try { context.AddNamespace(m.Groups["p"].Value, m.Groups["n"].Value); } catch { } m = m.NextMatch(); } } catch { } } expr.SetContext(context); return contextNode.Current.Evaluate(expr, contextNode); } catch { //Any exception such as syntax error in XPath return String.Empty; } } //Empty nodeset as context node return String.Empty; }
public static void Main(string[] args) { if (args.Length != 2) { PrintUsage(); return; } XPathDocument doc = new XPathDocument(args[0]); XPathNavigator nav = doc.CreateNavigator(); XPathExpression expr = nav.Compile(args[1]); ExsltContext ctxt = new ExsltContext(nav.NameTable); expr.SetContext(ctxt); object o = nav.Evaluate(expr); if (o is XPathNodeIterator) { XPathNodeIterator ni = (XPathNodeIterator)o; while (ni.MoveNext()) { Console.WriteLine("Node - type:{0}, name:{1}, value:{2}", ni.Current.NodeType, ni.Current.Name, ni.Current.Value); } } else { Console.WriteLine(o); } }