本文整理汇总了Java中net.sf.saxon.s9api.XPathCompiler类的典型用法代码示例。如果您正苦于以下问题:Java XPathCompiler类的具体用法?Java XPathCompiler怎么用?Java XPathCompiler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XPathCompiler类属于net.sf.saxon.s9api包,在下文中一共展示了XPathCompiler类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: applyXpath
import net.sf.saxon.s9api.XPathCompiler; //导入依赖的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.XPathCompiler; //导入依赖的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: getEnvironment
import net.sf.saxon.s9api.XPathCompiler; //导入依赖的package包/类
/**
* This method return the environment to evaluate a test case.
*
* The environment contains the processor to use. This is the best
* opportunity to catch it and register the extension functions. It first
* checks whether the functions already have been registered on that
* processor.
*/
@Override
protected Environment getEnvironment(XdmNode testCase, XPathCompiler xpc)
throws SaxonApiException
{
// the environments
Environment env = super.getEnvironment(testCase, xpc);
// the name of file:exists($path)
SymbolicName exists = new SymbolicName(
StandardNames.XSL_FUNCTION,
new StructuredQName("file", "http://expath.org/ns/file", "exists"),
1);
// the config object
Configuration conf = env.processor.getUnderlyingConfiguration();
// has file:exists($path) already been registered?
if ( ! conf.getIntegratedFunctionLibrary().isAvailable(exists) ) {
try {
Library lib = new EXPathFileLibrary();
lib.register(conf);
}
catch ( ToolsException ex ) {
throw new SaxonApiException("Error registering the EXPath File functions", ex);
}
}
return env;
}
示例4: initialize
import net.sf.saxon.s9api.XPathCompiler; //导入依赖的package包/类
@Override
public void initialize(InputSplit inSplit, TaskAttemptContext context)
throws IOException, InterruptedException {
Path file = ((FileSplit)inSplit).getPath();
FileSystem fs = file.getFileSystem(context.getConfiguration());
FSDataInputStream fileIn = fs.open(file);
DocumentBuilder docBuilder = builderLocal.get();
try {
Document document = docBuilder.parse(fileIn);
net.sf.saxon.s9api.DocumentBuilder db = saxonBuilderLocal.get();
XdmNode xdmDoc = db.wrap(document);
XPathCompiler xpath = proc.newXPathCompiler();
xpath.declareNamespace("wp",
"http://www.mediawiki.org/xml/export-0.4/");
XPathSelector selector = xpath.compile(PATH_EXPRESSION).load();
selector.setContextItem(xdmDoc);
items = new ArrayList<XdmItem>();
for (XdmItem item : selector) {
items.add(item);
}
} catch (SAXException ex) {
ex.printStackTrace();
throw new IOException(ex);
} catch (SaxonApiException e) {
e.printStackTrace();
} finally {
if (fileIn != null) {
fileIn.close();
}
}
}
示例5: constructElementParserDatatype
import net.sf.saxon.s9api.XPathCompiler; //导入依赖的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());
}
}
};
}
示例6: testExtFunctions
import net.sf.saxon.s9api.XPathCompiler; //导入依赖的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);
}
示例7: compiler
import net.sf.saxon.s9api.XPathCompiler; //导入依赖的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;
}
示例8: evaluate
import net.sf.saxon.s9api.XPathCompiler; //导入依赖的package包/类
public static XdmValue evaluate(String xpath)
throws ToolsException
, SaxonApiException
{
XPathCompiler compiler = compiler();
return evaluate(compiler, xpath);
}
示例9: setPrefixNamespaceMappings
import net.sf.saxon.s9api.XPathCompiler; //导入依赖的package包/类
/**
* Set the namespaces in the XPathCompiler.
*
* @param xpathCompiler
* @param namespaceMappings Namespace prefix to Namespace uri mappings
*/
private void setPrefixNamespaceMappings(XPathCompiler xpathCompiler, HashMap<String,String> namespaceMappings) {
if (namespaceMappings != null) {
// Get the mappings
Set<Entry<String, String>> mappings = namespaceMappings.entrySet();
// If mappings exist, set the namespaces
if (mappings != null) {
Iterator<Entry<String, String>> it = mappings.iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
xpathCompiler.declareNamespace(entry.getKey(), entry.getValue());
}
}
}
// Add in the defaults
xpathCompiler.declareNamespace("xml",NamespaceConstant.XML);
xpathCompiler.declareNamespace("xs",NamespaceConstant.SCHEMA);
xpathCompiler.declareNamespace("fn",NamespaceConstant.FN);
}
示例10: init
import net.sf.saxon.s9api.XPathCompiler; //导入依赖的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());
}
}