在 Sparql 输出中不指定数据类型的情况下打印日期时间

Print dateTime without specifying datatype in Sparql output

我在 Jena 中使用以下 sparql 查询来打印一些信息:

   String qr = "PREFIX : <http://www.example.com/tempsensor#>\n" +
               "PREFIX  xsd: <http://www.w3.org/2001/XMLSchema#> \n"+
                 "SELECT \n"+
                "?Place ?Temperature ?Date \n"+ 
                 "WHERE\n"+
                "{ ?ind :locatedIn ?Place .\n " +
                  "?ind :measures ?Temperature .\n " +
                  "?ind :onDate ?Date .\n " +
                "FILTER(regex(str(?Place),'Delhi', 'i'))\n"+
                "FILTER ( datatype(?Date) = xsd:dateTime)\n"+
                "}";

获得的输出是:

 --------------------------------------------------------------
| Place  | Temperature | Date                                |
==============================================================
| :Delhi | 16          | "2014-10-02T03:20:10"^^xsd:dateTime |

但是,我不想在日期列中附加数据类型。我需要像

这样的输出
--------------------------------------------------------------
| Place  | Temperature | Date                                |
==============================================================
| :Delhi | 16          | 2014-10-02T03:20:10                 |

如何在查询中指定它?

您可以使用项目表达式来获取文字的字符串形式:

String qr = "PREFIX : <http://www.example.com/tempsensor#>\n" +
            "PREFIX  xsd: <http://www.w3.org/2001/XMLSchema#> \n"+
            "SELECT \n"+
            "?Place ?Temperature (STR(?Date) AS ?StrDate) \n"+ 
            "WHERE\n"+
            "{ ?ind :locatedIn ?Place .\n " +
            "  ?ind :measures ?Temperature .\n " +
            "  ?ind :onDate ?Date .\n " +
            "  FILTER(regex(str(?Place),'Delhi', 'i'))\n"+
            "  FILTER ( datatype(?Date) = xsd:dateTime)\n"+
            "}";

这里我们使用内置的 STR() 函数将 ?Date 的值转换为没有数据类型的字符串形式。

SPARQL 定义了广泛的内置函数,值得您花时间阅读 - http://www.w3.org/TR/sparql11-query/#expressions