简体   繁体   English

如何在javax XPath中写入日期转换条件?

[英]How to write date conversion condition in javax XPath?

My XML is: 我的XML是:

<?xml version="1.0"?>
<company>
    <staff id="1001" thedate="2013-01-01">
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff id="2001" thedate="2014-01-01">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
</company>

I need a java code, which can compare dates from XML. 我需要一个java代码,它可以比较XML中的日期。 I wrote this java code, but it does not work: 我写了这个java代码,但它不起作用:

public static void main(String[] args) {
    try {
        File fXmlFile = new File("c:\\tmp\\test.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;
        dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        NodeList nList = doc.getElementsByTagName("staff");
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            String exp = "xs:date(@thedate) &gt; xs:date('2013-05-01')"; //here comes the expression, but it does not work
            System.out.println(selectBooleanValue(nNode,exp));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private static boolean selectBooleanValue(Node node, String xpathString) {
    try {
        XPath xpath = XPATH_FACTORY.get().newXPath();
        XPathExpression expr = xpath.compile(xpathString);
        boolean result = Boolean.valueOf(expr.evaluate(node, XPathConstants.BOOLEAN).toString());
        return result;
    } catch (XPathExpressionException e) {
        return false;
    }
}
private static ThreadLocal<XPathFactory> XPATH_FACTORY = new ThreadLocal<XPathFactory>() {
    @Override
    protected XPathFactory initialValue() {
        return XPathFactory.newInstance();
    };
};

I always get "false, false" output, so it does not work. 我总是得到“假,假”输出,所以它不起作用。 The corresponding output would be: "false, true" 相应的输出将是:“false,true”

This line is the main question here: 这一行是这里的主要问题:

String exp = "xs:date(@thedate) &gt; xs:date('2013-05-01')"; //here comes the expression, but it does not work

I tried without xs: but it is also does not work. 我试过没有xs:但它也不起作用。 I know there are other ways, but I need to solve this problem with that line! 我知道还有其他方法,但我需要用这条线来解决这个问题! What kind of expression should I use to compare date values which are String variables actually? 我应该使用什么样的表达式来比较实际上是String变量的日期值?

I tried my code to compare strings, and it is works with it, but I need to compare dates! 我尝试使用我的代码比较字符串,它可以使用它,但我需要比较日期!

Thank you! 谢谢!

XPath 2.0 which you seem to use has two greater than comparison operators, one is > , the other is gt . 你似乎使用的XPath 2.0比比较运算符大两个,一个是> ,另一个是gt So try the expression xs:date(@thedate) > xs:date('2013-05-01') or the expression xs:date(@thedate) gt xs:date('2013-05-01') . 因此,请尝试使用表达式xs:date(@thedate) > xs:date('2013-05-01')或表达式xs:date(@thedate) gt xs:date('2013-05-01') &gt; is only used inside XSLT code to make sure the code is following XML rules, that is not something needed in pure XPath. 仅在XSLT代码中使用,以确保代码遵循XML规则,这不是纯XPath中所需的。 But I wonder whether you use an XPath 2.0 implementation at all and whether you also do not need to set up a namespace resolver to map the xs prefix to the XML schema namespace. 但我想知道你是否完全使用XPath 2.0实现,以及是否也不需要设置命名空间解析器来将xs前缀映射到XML模式命名空间。

Assuming you have the Java built-in XPath 1.0 implementation all you can do is compare numbers so you could use number(translate(@thedate, '-', '')) > 20130501 . 假设你有Java内置XPath 1.0实现,你所能做的就是比较数字,这样就可以使用number(translate(@thedate, '-', '')) > 20130501

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM