Loading XML data failure

CALL apoc.load.xml("file:///xxxx/xx.xml")

YIELD value

UNWIND value._children AS GraphicElements

WITH GraphicElements,GraphicElements._children as EmbeddedSymbols

with GraphicElements, EmbeddedSymbols,

[x in EmbeddedSymbols where x._type = 'EmbeddedSymbol'][0] as embeddedSymbol

with embeddedSymbol.Name as name,embeddedSymbol

return embeddedSymbol,name

EmbeddedSymbol is an XML tag and name is property of this tag , this query returned 5 null records and 1 non-null record , actually I need to create "EmbeddedSymbol" Node and Set "Name" as its unique property .

so i wrote the following query to check if the value is not null and pass name value to it but i faced a lot of errors :

CALL apoc.load.xml("file:///xxxx/xx.xml")

YIELD value

UNWIND value._children AS GraphicElements

WITH GraphicElements,GraphicElements._children as EmbeddedSymbols

with GraphicElements, EmbeddedSymbols,

[x in EmbeddedSymbols where x._type = 'EmbeddedSymbol'][0] as embeddedSymbol

with embeddedSymbol.Name as name,embeddedSymbol

call apoc.do.when(

embeddedSymbol IS NOT NULL,

'MERGE (e:EmbeddedSymbol{Name:name})',

{name:name}

)

The current Error is :

Type mismatch: expected String but was Map (line 11, column 2 (offset: 461))
" {name:name}"

The third parameter of the apoc.do.when statement is supposed to be a string representing the cypher query to executed when the conditions is false. You have passed a literal map '{name:name}' instead.

Okay I understand the error but what should I do?

Because i tried alot of fixes but none of them worked for me

I tried {name:name} , name:name, name:$name

What do you want to happen when the condition is not true, I.e, embeddedSymbol is null?

Sorry I just saw your reply now

I want if the embeddedSymbol not null , do the merge query and pass the name correctly to the query

knowing that embedded symbol object is as the following :
{
"Transparency": "0",
"Left": "0.5",
"TabStop": "True",
"Name": "UPPER_BAR1",

You have the case of 'NOT NULL' handled. The issue is with the 'NULL' condition, where you passed '{name: name}' as the third parameter of the 'apoc.do.when' procedure. What behavior do you want when 'embeddedSymbol is NULL'? You need valid cypher for this third parameter.

If you want to do nothing, then you don't need the 'apoc.do.when' procedure, as it allows you to implement an if/else construct. You can use a 'with' statement followed by 'where' to filter only the case when embeddedSymbol is not null.

1 Like

Thank you so much , Your solution worked for me

1 Like