I have a HTML document:
<html>
<head>
<title>Title</title>
</head>
<body>
<h1><i>"Escape"</i></h1>
<p>
<b>`n`</b>
<p>& < ></p>
<aside>1.5</aside>
</body>
</html>
A file.txt
storing variables names and their corresponding XPath:
xpath:ref_1://body/h1/string()
xpath:ref_2.1://body/p[1]/string()
xpath:ref_2.2.1://body/p[2]/string()
xpath:ref_3://body/aside[1]/number()
And a JSON template file.json
:
{
"key_1": "{{ref_1}}",
"key_2": {
"key_2.1": "{{ref_2.1}}",
"key_2.2": [
0,
"{{ref_2.2.1}}"
]
}
"key_3": "{{ref_3}}"
}
I would like to replace the "{{varname}}"
s in the JSON by evaluating the corresponding XPath stored in file.txt
. The expected output would be:
{
"key_1": ""Escape"",
"key_2": {
"key_2.1": "n `\n`n ",
"key_2.2": [
0,
"& < >"
]
}
"key_3": 1.5
}
By using the implementation defined x:eval
of the XPath processor, I can get a map that stores the variables names and their processed value:
let
$vars_file := "file.txt",
$json_file := "file.json"
return
let
$doc := . ,
$map := map:merge(
unparsed-text-lines($vars_file) ! (
let
$record := tokenize(., ":"),
$key := $record[2],
$value := $doc/x:eval($record[3], map{"language": "xpath"})
return
map{$key: $value}
)
)
return
$map
Which outputs:
{
"ref_1": ""Escape"",
"ref_2.1": "n `\n`n ",
"ref_2.2.1": "& < >",
"ref_3": 1.5
}
So far so good, but how can I update the JSON with the values in $map
?