本文整理汇总了Java中net.sf.saxon.s9api.Processor.newXPathCompiler方法的典型用法代码示例。如果您正苦于以下问题:Java Processor.newXPathCompiler方法的具体用法?Java Processor.newXPathCompiler怎么用?Java Processor.newXPathCompiler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.saxon.s9api.Processor
的用法示例。
在下文中一共展示了Processor.newXPathCompiler方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: applyXpath
import net.sf.saxon.s9api.Processor; //导入方法依赖的package包/类
public static List<String> applyXpath(String xml, String xpathString) {
List<String> result = new ArrayList<>();
try {
Processor proc = new Processor(false);
XPathCompiler xpath = proc.newXPathCompiler();
DocumentBuilder builder = proc.newDocumentBuilder();
// Load the XML document.
StringReader reader = new StringReader(xml);
XdmNode doc = builder.build(new StreamSource(reader));
// Compile the xpath
XPathSelector selector = xpath.compile(xpathString).load();
selector.setContextItem(doc);
// Evaluate the expression.
XdmValue nodes = selector.evaluate();
for (XdmItem item : nodes) {
result.add(item.toString());
}
} catch (Exception e) {
LOGGER.error("Error applying XPath", e);
}
return result;
}
示例2: evaluateXPath2
import net.sf.saxon.s9api.Processor; //导入方法依赖的package包/类
/**
* Evaluates an XPath 2.0 expression using the Saxon s9api interfaces.
*
* @param xmlSource
* The XML Source.
* @param expr
* The XPath expression to be evaluated.
* @param nsBindings
* A collection of namespace bindings required to evaluate the
* XPath expression, where each entry maps a namespace URI (key)
* to a prefix (value).
* @return An XdmValue object representing a value in the XDM data model;
* this is a sequence of zero or more items, where each item is
* either an atomic value or a node.
* @throws SaxonApiException
* If an error occurs while evaluating the expression; this
* always wraps some other underlying exception.
*/
public static XdmValue evaluateXPath2(Source xmlSource, String expr,
Map<String, String> nsBindings) throws SaxonApiException {
Processor proc = new Processor(false);
XPathCompiler compiler = proc.newXPathCompiler();
for (String nsURI : nsBindings.keySet()) {
compiler.declareNamespace(nsBindings.get(nsURI), nsURI);
}
XPathSelector xpath = compiler.compile(expr).load();
DocumentBuilder builder = proc.newDocumentBuilder();
XdmNode node = null;
if (DOMSource.class.isInstance(xmlSource)) {
DOMSource domSource = (DOMSource) xmlSource;
node = builder.wrap(domSource.getNode());
} else {
node = builder.build(xmlSource);
}
xpath.setContextItem(node);
return xpath.evaluate();
}
示例3: constructElementParserDatatype
import net.sf.saxon.s9api.Processor; //导入方法依赖的package包/类
private Datatype constructElementParserDatatype(final QName qn, final boolean allowsEmpty, final boolean allowsMultiple) throws ValidationException {
return new Datatype() {
@Override
public boolean isAtomic() { return false; }
@Override
public boolean allowsMultiple() { return allowsMultiple; }
@Override
public boolean allowsEmpty() { return allowsEmpty; }
@Override
public XdmValue convert(String input, Configuration configuration) throws ValidationException {
Processor proc = new Processor(configuration);
DocumentBuilder builder = proc.newDocumentBuilder();
String wrappedInput="<fake:document xmlns:fake=\"top:marchand:xml:gaulois:wrapper\">".concat(input).concat("</fake:document>");
InputStream is = new ByteArrayInputStream(wrappedInput.getBytes(Charset.forName("UTF-8")));
try {
XdmNode documentNode = builder.build(new StreamSource(is));
XPathCompiler compiler = proc.newXPathCompiler();
compiler.declareNamespace("fake", "top:marchand:xml:gaulois:wrapper");
XPathSelector selector = compiler.compile("/fake:document/node()").load();
selector.setContextItem(documentNode);
XdmValue ret = selector.evaluate();
if(ret.size()==0 && !allowsEmpty()) throw new ValidationException(qn.toString()+" does not allow empty sequence");
if(ret.size()>1 && !allowsMultiple()) throw new ValidationException(qn.toString()+" does not allow sequence with more than one element");
return ret;
} catch(SaxonApiException ex) {
throw new ValidationException(input+" can not be casted to "+qn.toString());
}
}
};
}
示例4: testExtFunctions
import net.sf.saxon.s9api.Processor; //导入方法依赖的package包/类
@Test
public void testExtFunctions() throws Exception {
Configuration config = new DefaultSaxonConfigurationFactory().getConfiguration();
Processor proc = new Processor(config);
XPathCompiler xpc = proc.newXPathCompiler();
xpc.declareNamespace("ex", "top:marchand:xml:extfunctions");
QName var = new QName("connect");
xpc.declareVariable(var);
XPathExecutable xpe = xpc.compile("ex:basex-query('for $i in 1 to 10 return <test>{$i}</test>',$connect)");
assertNotNull("unable to compile extension function", xpe);
}
示例5: XPathCache
import net.sf.saxon.s9api.Processor; //导入方法依赖的package包/类
public XPathCache(Configuration configuration) {
this.configuration = configuration;
final Processor processor = new Processor(configuration);
compiler = processor.newXPathCompiler();
declareNamespace(KeyRefInterface.NAMESPACE_PREFIX, KeyRefInterface.NAMESPACE_URI);
declareNamespace(KeyDefInterface.NAMESPACE_PREFIX, KeyDefInterface.NAMESPACE_URI);
declareNamespace(XsltConref.NAMESPACE_PREFIX, XsltConref.NAMESPACE_URI);
declareNamespace(XsltConref.NAMESPACE_PARAMETER_PREFIX, XsltConref.NAMESPACE_PARAMETER_URI);
}
示例6: compiler
import net.sf.saxon.s9api.Processor; //导入方法依赖的package包/类
public static synchronized XPathCompiler compiler()
throws ToolsException
{
if ( null == COMPILER ) {
Processor saxon = new Processor(false);
Library lib = new EXPathFileLibrary();
lib.register(saxon.getUnderlyingConfiguration());
COMPILER = saxon.newXPathCompiler();
COMPILER.declareNamespace(
EXPathFileLibrary.NS_PREFIX,
EXPathFileLibrary.NS_URI);
}
return COMPILER;
}
示例7: XPathEvaluator
import net.sf.saxon.s9api.Processor; //导入方法依赖的package包/类
public XPathEvaluator() {
// The boolean argument indicates whether this is the licensed edition or not.
processor = new Processor(false);
xPathCompiler = processor.newXPathCompiler();
}
示例8: init
import net.sf.saxon.s9api.Processor; //导入方法依赖的package包/类
/**
* Initialization to improve performance for repetitive invocations of filter and evaluate expressions
*
* @throws XPathException
*/
private void init() throws XPathException {
try {
// Get the processor
proc = new Processor(false);
// Set any specified configuration properties for the processor
if (featureMappings != null) {
for (Entry<String, Object> entry : featureMappings.entrySet()) {
proc.setConfigurationProperty(entry.getKey(), entry.getValue());
}
}
//proc.setConfigurationProperty(FeatureKeys.ENTITY_RESOLVER_CLASS, "com.elsevier.spark_xml_utils.common.IgnoreDoctype");
// Get the XPath compiler
XPathCompiler xpathCompiler = proc.newXPathCompiler();
// Set the namespace to prefix mappings
this.setPrefixNamespaceMappings(xpathCompiler, namespaceMappings);
// Compile the XPath expression and get a document builder
xsel = xpathCompiler.compile(xPathExpression).load();
builder = proc.newDocumentBuilder();
// Create and initialize the serializer
baos = new ByteArrayOutputStream();
serializer = proc.newSerializer(baos);
serializer.setOutputStream(baos);
serializer.setOutputProperty(Serializer.Property.METHOD, "xml");
serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION,"yes");
serializer.setProcessor(proc);
} catch (SaxonApiException e) {
log.error("Problems creating an XPathProcessor. " + e.getMessage(),e);
throw new XPathException(e.getMessage());
}
}