neo4j:用 HEADERs 调用 APOC.LOAD.HTML

neo4j : CALL APOC.LOAD.HTML with HEADERs

我有以下table

<table>
    <tr>
        <th> header 1</th>
        <th> header 2</th>
        <th> header 3</th>
    <tr>
        <td> keyword1 </td>
        <td> value1.2 </td>
        <td>
            <p> paragraph 1 </p>
        </td>
    </tr>
    <tr>
        <td> keyword2 </td>
        <td> value2.2 </td>
        <td>
            <p> paragraph 2 </p>
            <p> paragraph 3 </p>
        </td>
    </tr>
    <tr>
        <td> keyword3 </td>
        <td> value3.2</td>
        <td>
            <p> paragraph 1 </p>
            <p> paragraph 3 </p>
            <p> </p>
        </td>
    </tr>
</table>

您建议使用什么方法通过apoc.load.html加载它 和 apoc.node.create 或 apoc.node.merge 以便 headers 动态用作节点属性名称?

它应该创建与以下静态代码等效的动态:

MERGE(:node {name:keyword1, header2:value1.2})-[:R]->(:header3 {name:paragrap1})

MERGE(:node {name:keyword2, header2:value2.2})-[:R]->(:header3 {name:paragrap2})
MERGE(:node {name:keyword2, header2:value2.2})-[:R]->(:header3 {name:paragrap3})

MERGE(:node {name:keyword3, header2:value3.2})-[:R]->(:header3 {name:paragrap1})
MERGE(:node {name:keyword3, header2:value3.2})-[:R]->(:header3 {name:paragrap3})

下面是我写的代码...

// 999. SAMPLE CODE
CALL apoc.load.html("file:///C:/Users/sesa407003/Desktop/CURRENT%20PROJECTS/NEO4J/doc_start.html",{line: "table tr"}) yield value as lineList

CALL apoc.load.html("file:///doc_start.html",{header: "table tr th"}) yield value as headersList

UNWIND range(1, length(lineList.line) -1) as j
//with j,i,source
CALL apoc.load.html("file:///doc_start.html",{value: "table tr:eq("+j+") td"}) yield value as valueList
CALL apoc.merge.node(["node"], {name:valueList.value[2].text}) yield node as source
UNWIND range(0,length(headersList.header)-2) as i
CALL apoc.create.setProperties(source,[headersList.header[i].text],[valueList.value[i].text]) yield node
CALL apoc.load.html("file:///doc_start.html",{paragraphs: "table tr:eq("+j+") td:eq(2) p"}) yield value as paragraphsList
UNWIND paragraphsList.paragraphs as paragraph
MERGE(target:dashboard {name:paragraph.text})
MERGE(source)-[:R]->(target)
return *

它似乎有效...但是当我尝试删除空段落时,例如 keyword3 上的最后一个...我找不到 WHERE 或 CASE WHEN 或 apoc.case.when[= 的正确语法15=]

我看了看你的密码,做了一些修改。我希望它能让你更接近你的最终状态。

为了删除空段落,我添加了这个小 WITH

WITH source, paragraph.text AS para
WHERE trim(para) <> ""

我还更改了一些数组索引以从 table 中获取正确的数据。

CALL apoc.load.html("file:///table.html",{line: "table tr"}) yield value as lineList
CALL apoc.load.html("file:///table.html",{header: "table tr th"}) yield value as headersList
UNWIND range(1, size(lineList.line) - 1) as j
CALL apoc.load.html("file:///table.html",{value: "table tr:eq("+j+") td"}) yield value as valueList
CALL apoc.merge.node(["node"], {name:valueList.value[0].text}) yield node as source
UNWIND range(0,size(headersList.header)-2) as i
CALL apoc.create.setProperties(source,[headersList.header[i].text],[valueList.value[i].text]) yield node
CALL apoc.load.html("file:///table.html",{paragraphs: "table tr:eq("+j+") td:eq(2) p"}) yield value as paragraphsList
UNWIND paragraphsList.paragraphs as paragraph
WITH source, paragraph.text AS para
WHERE trim(para) <> ""
MERGE(target:dashboard {name:para})
MERGE(source)-[:R]->(target)
RETURN *