I have read a few relevant topics on here and now have a basic understanding of the namespace concept. But I still have some difficulties getting my template to work with my XML data.
When there is no namespace involved, this template and this XML works fine:
ftl:
${pp.doc.user1.name}
XML:
<?xml version="1.0" encoding="UTF-8" ?>
<user1>
<name>Jack</name>
</user1>
However, if there is namespace defined in the XML file, like this:
<?xml version="1.0" encoding="UTF-8" ?>
<user1 xmlns="https://example.com/xyz">
<name>Jack</name>
</user1>
I got error messages:
Error when processing this file: data\test1.xml
FreeMarker template error: For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), or "template output" , but this has evaluated to a sequence+hash (wrapper: f.e.dom.NodeListModel):
==> pp.doc.user1.name [in template "renderer/test.ftlh" at line 1, column 3]
----
Tip: This XML query result can't be used as string because for that it had to contain exactly 1 XML node, but it contains 0 nodes. That is, the constructing XML query has found no matches.
----
----
FTL stack trace ("~" means nesting-related):
- Failed at: ${pp.doc.user1.name} [in template "renderer/test.ftlh" at line 1, column 1]
----
My current understanding is that in the ftl, I need to use <#ftl ns_prefixes={"ns": "https://example.com/xyz"}>
or something like this, but none of the things I tried have worked. Please kindly help. Thank you!
In pp.doc.user1.name
you aren't using any namespace prefix, so the elements are assumed to belong to the default namespace, which is by default nothing. To set it, use <#ftl ns_prefixes={"D": "https://example.com/xyz"}>
. D
is a prefix reserved for this purpose.