本文整理汇总了Java中com.sun.org.apache.xpath.internal.XPath.execute方法的典型用法代码示例。如果您正苦于以下问题:Java XPath.execute方法的具体用法?Java XPath.execute怎么用?Java XPath.execute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.org.apache.xpath.internal.XPath
的用法示例。
在下文中一共展示了XPath.execute方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: evaluate
import com.sun.org.apache.xpath.internal.XPath; //导入方法依赖的package包/类
/**
* The dyn:evaluate function evaluates a string as an XPath expression and returns
* the resulting value, which might be a boolean, number, string, node set, result
* tree fragment or external object. The sole argument is the string to be evaluated.
* <p>
* If the expression string passed as the second argument is an invalid XPath
* expression (including an empty string), this function returns an empty node set.
* <p>
* You should only use this function if the expression must be constructed dynamically,
* otherwise it is much more efficient to use the expression literally.
*
* @param myContext The ExpressionContext passed by the extension processor
* @param xpathExpr The XPath expression string
*
* @return The evaluation result
*/
public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
throws SAXNotSupportedException
{
if (myContext instanceof XPathContext.XPathExpressionContext)
{
XPathContext xctxt = null;
try
{
xctxt = ((XPathContext.XPathExpressionContext) myContext).getXPathContext();
XPath dynamicXPath = new XPath(xpathExpr, xctxt.getSAXLocator(),
xctxt.getNamespaceContext(),
XPath.SELECT);
return dynamicXPath.execute(xctxt, myContext.getContextNode(),
xctxt.getNamespaceContext());
}
catch (TransformerException e)
{
return new XNodeSet(xctxt.getDTMManager());
}
}
else
throw new SAXNotSupportedException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_CONTEXT_PASSED, new Object[]{myContext })); //"Invalid context passed to evaluate "
}
示例2: eval
import com.sun.org.apache.xpath.internal.XPath; //导入方法依赖的package包/类
/**
* Evaluates an XPath expression.
*
* @param expression the XPath expression.
* @param context the document containing the context being queried.
* @return the evaluated result.
* @throws TransformerException if unable to transform the expression.
*/
public Object eval(String expression, Node context) throws TransformerException {
if (context == null) {
context = contextNode;
}
XPath xpath = createXPath(expression);
XObject result = xpath.execute(xpathSupport,
xpathSupport.getDTMHandleFromNode(context),
prefixResolver);
if (result.getType() == XObject.CLASS_BOOLEAN) {
return new Boolean(result.toString());
}
else {
return result.object();
}
}
示例3: eval
import com.sun.org.apache.xpath.internal.XPath; //导入方法依赖的package包/类
/**
* Evaluate XPath string to an XObject.
* XPath namespace prefixes are resolved from the namespaceNode.
* The implementation of this is a little slow, since it creates
* a number of objects each time it is called. This could be optimized
* to keep the same objects around, but then thread-safety issues would arise.
*
* @param contextNode The node to start searching from.
* @param xpathnode
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
* @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
* @see com.sun.org.apache.xpath.internal.objects.XObject
* @see com.sun.org.apache.xpath.internal.objects.XNull
* @see com.sun.org.apache.xpath.internal.objects.XBoolean
* @see com.sun.org.apache.xpath.internal.objects.XNumber
* @see com.sun.org.apache.xpath.internal.objects.XString
* @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
*
* @throws TransformerException
*/
public static XObject eval(
Node contextNode, Node xpathnode, Node namespaceNode)
throws TransformerException {
// Since we don't have a XML Parser involved here, install some default support
// for things like namespaces, etc.
// (Changed from: XPathContext xpathSupport = new XPathContext();
// because XPathContext is weak in a number of areas... perhaps
// XPathContext should be done away with.)
FuncHereContext xpathSupport = new FuncHereContext(xpathnode);
// Create an object to resolve namespace prefixes.
// XPath namespaces are resolved from the input context node's document element
// if it is a root node, or else the current context node (for lack of a better
// resolution space, given the simplicity of this sample code).
PrefixResolverDefault prefixResolver =
new PrefixResolverDefault((namespaceNode.getNodeType()
== Node.DOCUMENT_NODE)
? ((Document) namespaceNode)
.getDocumentElement()
: namespaceNode);
String str = getStrFromNode(xpathnode);
// Create the XPath object.
XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
// Execute the XPath, and have it return the result
// return xpath.execute(xpathSupport, contextNode, prefixResolver);
int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}