图形重构的xml图

问题描述 投票:0回答:1

我正在尝试对我导入neo4j的xml文件进行图形重构。要导入这个xml,你需要这个特殊的apoc-library:https://seafile.rlp.net/f/bf9fbe3cde30491ea26a/?dl=1

然后你可以导入一个示例图:

CALL apoc.xml.import('https://seafile.rlp.net/f/5db7bdc77c5447faad88/?dl=1', {createNextWorkRelationships: true}) yield node return node;

此模型在图形中显示XML文本。文本节点与NEXT边连接,并且可以在NEXT边之后找到XML的序列化。

现在我想找到成对的XmlTag节点,其中_name-property'lb'是连接的XmlWord节点,然后创建一个新的line-node,它与G-FIRST_CHILD_OF-和G-LAST_CHILD_OF-edges连接到第一个和最后一行的Xml-Word节点。 edge-names中的新G-用于将它们与前边缘分开。

neo4j cypher
1个回答
0
投票

好的,我有一个在3.2.x中运行的查询,但在3.3.x中失败(将此报告为错误)。

你需要APOC procedures,因为你需要split()过程(用于拆分链节点中的元素节点),indexOf()过程(用于查找第一个和最后一个元素节点的索引,所以我们可以在拆分之前修剪节点集合),以及将值转换回节点的转换过程(当我们使用混合类型集合时,密码会有点混乱)。

// replace with however you match to the start node of a chain to process
match (start)
where size(()-->(start)) = 0

match path = (start)-[:NEXT*]->(end)
where not (end)-->()

// derive important collections of things from the path
with nodes(path) as nodes, 
     relationships(path) as rels, 
     [node in nodes(path) where node:element | node] as elements, 
     [node in nodes(path) where not node:element | node] as chain, 
     [node in nodes(path) | 
       case when node:element then false else node end] as splitColl

// trim splitColl so it starts and ends with false elements (our standins for :element nodes, as it makes it easier to split
with nodes, rels, elements, chain, splitColl, apoc.coll.indexOf(nodes, head(elements)) as firstElement, apoc.coll.indexOf(nodes, last(elements)) as lastElement
with rels, elements, chain, splitColl[firstElement..lastElement+1] as splitColl

// splits into groupings of nodes; each grouping will be surrounded by an :element
call apoc.coll.split(splitColl, false) yield value as group
with rels, elements, chain, apoc.convert.toNode(head(group)) as first, apoc.convert.toNode(last(group)) as second
create (el:element{type:'lb'})
create (el)-[:FIRST_CHILD]->(first)
create (el)-[:SECOND_CHILD]->(second)

// delete all relationships in the chain
with distinct rels, elements, chain
unwind rels as rel
delete rel

// create chain relationships only between non-element nodes
with distinct elements, chain
call apoc.nodes.link(chain, 'NEXT')

// delete the old elements
with distinct elements
unwind elements as el
detach delete el

这里是我正在测试的测试数据,以你的为基础,但在链中为连续的:元素节点添加额外的节点,并且对于when:元素节点不在链的开头和结尾出现:

create (:word{text:'Wort0'})-[:NEXT]->(e1:element {type:'lb'})-[:NEXT]->(w1:word {text:'Wort1'})-[:NEXT]->(w2:word {text:'Wort2'})-[:NEXT]->(w3:word {text:'Wort3'})-[:NEXT]->(w4:word {text:'Wort4'})-[:NEXT]->(w5:word {text:'Wort5'})-[:NEXT]->(e2:element {type:'lb'})-[:NEXT]->(:element{type:'lb'})-[:NEXT]->(w6:word {text:'Wort6'})-[:NEXT]->(w7:word {text:'Wort7'})-[:NEXT]->(w8:word {text:'Wort8'})-[:NEXT]->(e3:element {type:'lb'})-[:NEXT]->(:word{text:'Wort9'})

鉴于这是一个相当复杂的查询,您可能还需要考虑编写自己的自定义过程以在Java中执行此类处理,并在匹配起始节点后调用该过程。

© www.soinside.com 2019 - 2024. All rights reserved.