在sparql中返回布尔值

时间:2017-11-05 14:16:31

标签: boolean sparql

从rdf文件中,如果年龄是偶数,我需要为此人返回true,如果他们的年龄是奇数,则需要返回false。我编写了查询以显示具有年龄的人,但需要修改以在布尔值中显示结果。

select * where { ?x h:age ?age . 
         filter(    strends(?age, 0) || strends(?age, 2) || strends(?age, 4) || strends(?age, 6) || strends(?age, 8)  )
}

2 个答案:

答案 0 :(得分:3)

偶数测试是?X/2 = FLOOR(?X/2)

因此,如果?age具有数值数据类型:

where { ?x h:age ?age . 
        BIND( (?age/2 =  FLOOR(?age/2)) AS ?isEven)
       }

?isEven添加为true / false。

如果?age是字符串,则将?age替换为xsd:integer(?age)

答案 1 :(得分:0)

我有一个类似的案例。如果存在可选关系,则需要返回true / false结果。

public class TestsXmlWrapper
{
    private XmlDocument _xml;
    public TestsXmlWrapper(XmlDocument xml)
    {
        _xml = xml;
    }
    public IEnumerable<Site> Sites
    {
        get
        {
            foreach (XmlElement site in _xml.SelectNodes("SITES/SITE"))
            {
                yield return new Site(site);
            }
        }
    }
}

public class Site
{
    private XmlElement _site;
    public Site(XmlElement site)
    {
        _site = site;
    }

    public String ErrorCount => _site.SelectSingleNode("ERROR_COUNTER")?.InnerText;
    public String LoginUrl => _site.SelectSingleNode("LOGINFO/URL")?.InnerText;
    public String Username => _site.SelectSingleNode("LOGINFO/LOGIN")?.InnerText;
    public String Password => _site.SelectSingleNode("LOGINFO/PASSWORD")?.InnerText;
    public IEnumerable<Test> Test
    {
        get
        {
            foreach (XmlElement test in _site.SelectNodes("TESTS/TEST"))
            {
                yield return new Test(test);
            }
        }
    }
}

public class Test
{
    private XmlElement _test;
    public Test(XmlElement test)
    {
        _test = test;
    }
    public String Url => _test.SelectSingleNode("URL")?.InnerText;
}
...the same for email

结果将如下所示:

SELECT ?c ?hasNarrowMatch
WHERE {
?c a skos:Concept.
OPTIONAL {?c skos:narrowMatch ?nm}
BIND (exists{?c skos:narrowMatch ?nm} AS ?y)
BIND (IF(?y, "true", "false") AS ?hasNarrowMatch)
}