Core.Xml - support XPathExpression
This commit is contained in:
@@ -19,7 +19,34 @@ namespace Umbraco.Core.Xml
|
||||
if (variables == null || variables.Length == 0 || variables[0] == null)
|
||||
return navigator.Select(expression);
|
||||
|
||||
var compiled = navigator.Compile(expression);
|
||||
// Reflector shows that the standard XPathNavigator.Compile method just does
|
||||
// return XPathExpression.Compile(xpath);
|
||||
// only difference is, XPathNavigator.Compile is virtual so it could be overriden
|
||||
// by a class inheriting from XPathNavigator... there does not seem to be any
|
||||
// doing it in the Framework, though... so we'll assume it's much cleaner to use
|
||||
// the static compile:
|
||||
var compiled = XPathExpression.Compile(expression);
|
||||
|
||||
var context = new DynamicContext();
|
||||
foreach (var variable in variables)
|
||||
context.AddVariable(variable.Name, variable.Value);
|
||||
compiled.SetContext(context);
|
||||
return navigator.Select(compiled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects a node set, using the specified XPath expression.
|
||||
/// </summary>
|
||||
/// <param name="navigator">A source XPathNavigator.</param>
|
||||
/// <param name="expression">An XPath expression.</param>
|
||||
/// <param name="variables">A set of XPathVariables.</param>
|
||||
/// <returns>An iterator over the nodes matching the specified expression.</returns>
|
||||
public static XPathNodeIterator Select(this XPathNavigator navigator, XPathExpression expression, params XPathVariable[] variables)
|
||||
{
|
||||
if (variables == null || variables.Length == 0 || variables[0] == null)
|
||||
return navigator.Select(expression);
|
||||
|
||||
var compiled = expression.Clone(); // clone for thread-safety
|
||||
var context = new DynamicContext();
|
||||
foreach (var variable in variables)
|
||||
context.AddVariable(variable.Name, variable.Value);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
using System.Xml.XPath;
|
||||
|
||||
// source: mvpxml.codeplex.com
|
||||
|
||||
@@ -29,6 +30,24 @@ namespace Umbraco.Core.Xml
|
||||
return SelectNodes(source, expression, av);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects a list of XmlNode matching an XPath expression.
|
||||
/// </summary>
|
||||
/// <param name="source">A source XmlNode.</param>
|
||||
/// <param name="expression">An XPath expression.</param>
|
||||
/// <param name="variables">A set of XPathVariables.</param>
|
||||
/// <returns>The list of XmlNode matching the XPath expression.</returns>
|
||||
/// <remarks>
|
||||
/// <para>If <param name="variables" /> is <c>null</c>, or is empty, or contains only one single
|
||||
/// value which itself is <c>null</c>, then variables are ignored.</para>
|
||||
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
||||
/// </remarks>
|
||||
public static XmlNodeList SelectNodes(this XmlNode source, XPathExpression expression, IEnumerable<XPathVariable> variables)
|
||||
{
|
||||
var av = variables == null ? null : variables.ToArray();
|
||||
return SelectNodes(source, expression, av);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects a list of XmlNode matching an XPath expression.
|
||||
/// </summary>
|
||||
@@ -50,6 +69,27 @@ namespace Umbraco.Core.Xml
|
||||
return XmlNodeListFactory.CreateNodeList(iterator);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects a list of XmlNode matching an XPath expression.
|
||||
/// </summary>
|
||||
/// <param name="source">A source XmlNode.</param>
|
||||
/// <param name="expression">An XPath expression.</param>
|
||||
/// <param name="variables">A set of XPathVariables.</param>
|
||||
/// <returns>The list of XmlNode matching the XPath expression.</returns>
|
||||
/// <remarks>
|
||||
/// <para>If <param name="variables" /> is <c>null</c>, or is empty, or contains only one single
|
||||
/// value which itself is <c>null</c>, then variables are ignored.</para>
|
||||
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
||||
/// </remarks>
|
||||
public static XmlNodeList SelectNodes(this XmlNode source, XPathExpression expression, params XPathVariable[] variables)
|
||||
{
|
||||
if (variables == null || variables.Length == 0 || variables[0] == null)
|
||||
return source.SelectNodes(expression);
|
||||
|
||||
var iterator = source.CreateNavigator().Select(expression, variables);
|
||||
return XmlNodeListFactory.CreateNodeList(iterator);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects the first XmlNode that matches an XPath expression.
|
||||
/// </summary>
|
||||
@@ -67,7 +107,25 @@ namespace Umbraco.Core.Xml
|
||||
var av = variables == null ? null : variables.ToArray();
|
||||
return SelectSingleNode(source, expression, av);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Selects the first XmlNode that matches an XPath expression.
|
||||
/// </summary>
|
||||
/// <param name="source">A source XmlNode.</param>
|
||||
/// <param name="expression">An XPath expression.</param>
|
||||
/// <param name="variables">A set of XPathVariables.</param>
|
||||
/// <returns>The first XmlNode that matches the XPath expression.</returns>
|
||||
/// <remarks>
|
||||
/// <para>If <param name="variables" /> is <c>null</c>, or is empty, or contains only one single
|
||||
/// value which itself is <c>null</c>, then variables are ignored.</para>
|
||||
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
||||
/// </remarks>
|
||||
public static XmlNode SelectSingleNode(this XmlNode source, XPathExpression expression, IEnumerable<XPathVariable> variables)
|
||||
{
|
||||
var av = variables == null ? null : variables.ToArray();
|
||||
return SelectSingleNode(source, expression, av);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects the first XmlNode that matches an XPath expression.
|
||||
/// </summary>
|
||||
@@ -87,5 +145,25 @@ namespace Umbraco.Core.Xml
|
||||
|
||||
return SelectNodes(source, expression, variables).Cast<XmlNode>().FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects the first XmlNode that matches an XPath expression.
|
||||
/// </summary>
|
||||
/// <param name="source">A source XmlNode.</param>
|
||||
/// <param name="expression">An XPath expression.</param>
|
||||
/// <param name="variables">A set of XPathVariables.</param>
|
||||
/// <returns>The first XmlNode that matches the XPath expression.</returns>
|
||||
/// <remarks>
|
||||
/// <para>If <param name="variables" /> is <c>null</c>, or is empty, or contains only one single
|
||||
/// value which itself is <c>null</c>, then variables are ignored.</para>
|
||||
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
||||
/// </remarks>
|
||||
public static XmlNode SelectSingleNode(this XmlNode source, XPathExpression expression, params XPathVariable[] variables)
|
||||
{
|
||||
if (variables == null || variables.Length == 0 || variables[0] == null)
|
||||
return source.SelectSingleNode(expression);
|
||||
|
||||
return SelectNodes(source, expression, variables).Cast<XmlNode>().FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user