/// <summary> /// Compile and execute an expression supplied as a <c>String</c>, with a given context item, where /// the expression is expected to return a single item as its result. /// </summary> /// <param name="expression">A string containing the source text of the XPath expression</param> /// <param name="contextItem">The context item to be used for evaluation of the XPath expression. /// May be null, in which case the expression is evaluated without any context item.</param> /// <returns>If the XPath expression returns a singleton, then the the <c>XdmItem</c> /// which is the result of evaluating the XPath expression. If the expression returns an empty sequence, /// then null. If the expression returns a sequence containing more than one item, then the first /// item in the result.</returns> /// <exception cref="StaticError"> /// Throws a <c>Saxon.Api.StaticError</c> if there is any static error in the XPath expression. /// This includes both syntax errors, semantic errors such as references to undeclared functions or /// variables, and statically-detected type errors. /// </exception> /// <exception cref="DynamicError"> /// Throws a <c>Saxon.Api.DynamicError</c> if there is any dynamic error during evaluation of the XPath expression. /// This includes, for example, referring to the context item if no context item was supplied. /// </exception> public XdmItem EvaluateSingle(String expression, XdmItem contextItem) { try { JXdmItem value = compiler.evaluateSingle(expression, contextItem == null ? null : XdmItem.FromXdmItemItemToJXdmItem(contextItem)); return(value == null ? null : (XdmItem)XdmValue.Wrap(value.getUnderlyingValue())); } catch (JSaxonApiException err) { if (err.getCause() is JXPathException) { JXPathException xpathException = (JXPathException)err.getCause(); if (xpathException.isStaticError()) { throw new StaticError(err); } else { throw new DynamicError(err.getMessage()); } } else { throw new StaticError(err); } } }
/// <summary> /// Evaluate the XPath expression, returning the result as an <c>XdmItem</c> (that is, /// a single node or atomic value). /// </summary> /// <returns> /// An <c>XdmItem</c> representing the result of the expression, or null if the expression /// returns an empty sequence. If the expression returns a sequence of more than one item, /// any items after the first are ignored. /// </returns> /// <exception cref="DynamicError"> /// Throws <c>Saxon.Api.DynamicError</c> if the evaluation of the XPath expression fails /// with a dynamic error. /// </exception> public XdmItem EvaluateSingle() { try { JXdmItem item = selector.evaluateSingle(); return(item == null ? null : (XdmItem)XdmValue.Wrap(item.getUnderlyingValue().materialize())); } catch (JSaxonApiException err) { throw new DynamicError(err); } }