Пример #1
0
        /// <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);
                }
            }
        }
Пример #2
0
 /// <summary>
 /// Determine whether this item type matches a given item
 /// </summary>
 /// <param name="item">the item to be tested against this item type</param>
 /// <returns>true if the item matches this item type, false if it does not match</returns>
 public override bool Matches(XdmItem item)
 {
     return(type.matches(XdmItem.FromXdmItemItemToJXdmItem(item)));
 }