如何使用fn:在Java中匹配XPath的函数?

时间:2011-02-27 22:18:41

标签: java xpath

我想使用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();
        }

    }

0 个答案:

没有答案