每次附加附加了父属性的节点的遍历遍历的RML映射

如何解决每次附加附加了父属性的节点的遍历遍历的RML映射

我正在尝试将XML源映射到RDF,并且想知道如何在每次跟踪父级属性的同时进行递归遍历。在所示示例中,我希望提取所有Person个节点,并将每个父节点的name附加到当前节点。考虑所有父名称是必要的,因为节点名称可能不是唯一的。 (在示例中,两次访问grandChild1

请注意,我事先不知道嵌套可以进行多少个级别,因此为每个级别添加一个TriplesMap都不可行。

在经历了documentationexamplestest cases之后,我不确定这是否可能。

下面是一个简化的示例XML,我到目前为止创建的rml映射,使用RMLMapper生成的当前RDF输出以及我期望的RDF输出

数据
<Root>
    <Person>
        <name>parent1</name>
        <Children>
            <Person>
                <name>child1</name>
                <Person>
                    <name>grandchild1</name>
                    <Children>
                        <Person>
                            <name>greatgrandchild1</name>
                        </Person>
                    </Children>
                </Person>
            </Person>
            <Person>
                <name>child2</name>
                <Person>
                    <name>grandchild1</name>
                </Person>
            </Person>
        </Children>
    </Person>
</Root>
制图
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-Syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

@prefix testont: <http://www.example.com/ontology/> .
@prefix : <http://www.example.com/rules/> .
@base <http://www.example.com/instance/> .


:TriplesMapPerson a rr:TriplesMap;
    rml:logicalSource [
        rml:source "recursion_data.xml";
        rml:referenceFormulation ql:XPath;
        rml:iterator "//Root/Person"
    ].

:TriplesMapPerson rr:subjectMap [
    rr:template "{name}"
].

:TriplesMapPerson rr:predicateObjectMap [
    rr:predicate rdf:type;
    rr:object testont:Person
].

:TriplesMapChild a rr:TriplesMap;
    rml:logicalSource [
        rml:source "recursion_data.xml";
        rml:referenceFormulation ql:XPath;
        rml:iterator "//Root/Person/Children/Person"
    ].

:TriplesMapChild rr:subjectMap [
    rr:template "{name}_{../../name}"
].

:TriplesMapChild rr:predicateObjectMap [
    rr:predicate rdf:type;
    rr:object testont:Person
].

当前的RDF
<http://www.example.com/instance/parent1> <http://www.w3.org/1999/02/22-rdf-Syntax-ns#type> <http://www.example.com/ontology/Person>.
<http://www.example.com/instance/child1_parent1> <http://www.w3.org/1999/02/22-rdf-Syntax-ns#type> <http://www.example.com/ontology/Person>.
<http://www.example.com/instance/child2_parent1> <http://www.w3.org/1999/02/22-rdf-Syntax-ns#type> <http://www.example.com/ontology/Person>.
预期的RDF
<http://www.example.com/instance/parent1> <http://www.w3.org/1999/02/22-rdf-Syntax-ns#type> <http://www.example.com/ontology/Person>.
<http://www.example.com/instance/child1_parent1> <http://www.w3.org/1999/02/22-rdf-Syntax-ns#type> <http://www.example.com/ontology/Person>.
<http://www.example.com/instance/grandchild1_child1_parent1> <http://www.w3.org/1999/02/22-rdf-Syntax-ns#type> <http://www.example.com/ontology/Person>.
<http://www.example.com/instance/greatgrandchild1_grandchild1_child1_parent1> <http://www.w3.org/1999/02/22-rdf-Syntax-ns#type> <http://www.example.com/ontology/Person>.
<http://www.example.com/instance/child2_parent1> <http://www.w3.org/1999/02/22-rdf-Syntax-ns#type> <http://www.example.com/ontology/Person>.
<http://www.example.com/instance/grandchild1_child2_parent1> <http://www.w3.org/1999/02/22-rdf-Syntax-ns#type> <http://www.example.com/ontology/Person>.

感谢您为解决此问题所提供的帮助。预先感谢!

解决方法

您可以利用XPath的//运算符的递归性质对所有人员进行迭代,而不管其深度如何。

然后,在主题模板中,您可以使用XPath的“祖先或自身”轴来获取具有名称的祖先节点的列表,将其反向以获得所需的顺序,并获取每个节点的name属性:

for $ancestor in reverse(ancestor-or-self::*[name]) return $ancestor/name

然后,您可以使用XPaths字符串连接函数来连接值以获得所需的结果。

映射将如下所示

:Person a rr:TriplesMap ;
  rml:logicalSource [
    rml:source [a carml:Stream ];
    rml:referenceFormulation ql:XPath;
    rml:iterator "//Person"
  ] ;
    rr:subjectMap [
    rr:template "{string-join(for $ancestor in reverse(ancestor-or-self::*[name]) return $ancestor/name,'_')}" ;
    rr:class testont:Person ;
  ] ;
.

给出以下结果:

<http://example.com/base/parent1> a <http://www.example.com/ontology/Person> .

<http://example.com/base/child1_parent1> a <http://www.example.com/ontology/Person> .

<http://example.com/base/grandchild1_child1_parent1> a <http://www.example.com/ontology/Person> .

<http://example.com/base/greatgrandchild1_grandchild1_child1_parent1> a <http://www.example.com/ontology/Person> .

<http://example.com/base/child2_parent1> a <http://www.example.com/ontology/Person> .

<http://example.com/base/grandchild1_child2_parent1> a <http://www.example.com/ontology/Person> .

请注意,要使其正常工作,映射工具的XPath实现需要支持反向轴遍历。我通过支持该示例的https://github.com/carml/carml运行了该示例。

还要注意,您的xml输入示例缺少某些Children节点的某些中间Person节点。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?