/// <summary> /// Sets up the context for expression execution. /// </summary> private static XmlNamespaceManager PrepareContext(XPathNavigator source, XmlNamespaceManager context, XmlPrefix[] prefixes, XPathVariable[] variables) { XmlNamespaceManager ctx = context; // If we have variables, we need the dynamic context. if (variables != null) { DynamicContext dyn; if (ctx != null) dyn = new DynamicContext(ctx); else dyn = new DynamicContext(); // Add the variables we received. foreach (XPathVariable var in variables) { dyn.AddVariable(var.Name, var.Value); } ctx = dyn; } // If prefixes were added, append them to context. if (prefixes != null) { if (ctx == null) ctx = new XmlNamespaceManager(source.NameTable); foreach (XmlPrefix prefix in prefixes) ctx.AddNamespace(prefix.Prefix, prefix.NamespaceURI); } return ctx; }
/// <summary> /// Evaluates the given expression and returns the typed result. /// </summary> public static object Evaluate(string expression, XPathNavigator source, XmlPrefix[] prefixes, params XPathVariable[] variables) { XPathExpression expr = GetCompiledExpression(expression, source); expr.SetContext(PrepareContext(source, null, prefixes, variables)); return source.Evaluate(expr); }
/// <summary> /// Selects a node set using the specified XPath expression. /// </summary> public static XPathNodeIterator SelectSorted(string expression, XPathNavigator source, object sortExpression, IComparer comparer, XmlPrefix[] prefixes, params XPathVariable[] variables) { XPathExpression expr = GetCompiledExpression(expression, source); XmlNamespaceManager ctx = PrepareContext(source, null, prefixes, variables); expr.SetContext(ctx); PrepareSort(expr, source, sortExpression, comparer, ctx); return source.Select(expr); }
/// <summary> /// Selects the first XmlNode that matches the XPath expression. /// </summary> public static XmlNode SelectSingleNode(string expression, XmlNode source, XmlPrefix[] prefixes, params XPathVariable[] variables) { foreach (XmlNode node in SelectNodes(expression, source, prefixes, variables)) return node; return null; }
/// <summary> /// Selects a node set using the specified XPath expression. /// </summary> public static XmlNodeList SelectNodesSorted(string expression, XmlNode source, object sortExpression, IComparer comparer, XmlPrefix[] prefixes, params XPathVariable[] variables) { return XmlNodeListFactory.CreateNodeList( SelectSorted(expression, source.CreateNavigator(), sortExpression, comparer, prefixes, variables)); }
/// <summary> /// Selects a node set using the specified XPath expression. /// </summary> public static XmlNodeList SelectNodesSorted(string expression, XmlNode source, object sortExpression, XmlSortOrder order, XmlCaseOrder caseOrder, string lang, XmlDataType dataType, XmlPrefix[] prefixes, params XPathVariable[] variables) { return XmlNodeListFactory.CreateNodeList( SelectSorted(expression, source.CreateNavigator(), sortExpression, order, caseOrder, lang, dataType, prefixes, variables)); }
/// <summary> /// Selects a list of nodes matching the XPath expression. /// </summary> public static XmlNodeList SelectNodes(string expression, XmlNode source, XmlPrefix[] prefixes, params XPathVariable[] variables) { XPathNodeIterator it = Select(expression, source.CreateNavigator(), prefixes, variables); return XmlNodeListFactory.CreateNodeList(it); }