我想使用Java文件中的XPath的fn:matches
函数来评估特定输入字符串的特定模式(regex)。我知道Java有自己的Pattern.matches
函数,但我需要使用fn:matches
来完成此操作。我不知道如何从Java文件中调用fn:matches
函数。我创建了一个新的命名空间上下文:
package com.rdf.go;
import java.io.IOException;
import java.util.Iterator;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
public class PersonalNamespaceContext implements NamespaceContext {
public String getNamespaceURI(String prefix) {
if (prefix == null) throw new NullPointerException("Null prefix");
else if ("pre".equals(prefix)) return "http://www.w3.org/2005/xpath-functions/";
else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI;
return XMLConstants.NULL_NS_URI;
}
// This method isn't necessary for XPath processing.
public String getPrefix(String uri) {
throw new UnsupportedOperationException();
}
// This method isn't necessary for XPath processing either.
public Iterator getPrefixes(String uri) {
System.out.println(uri);
throw new UnsupportedOperationException();
}
}