So I’ve just started experimenting with FreeMarker and I’ve hit this problem.
When I use online tutorials with the XML file supplied and write something like
${doc.book.chapter.para[0]}
I get the expected output.
However, when I try with my own XML document I get an error Expression…. is undefined.
I think I’ve worked out that this is because my XML has attributes and the tutorial one does not.
I’ve reached this conclusion because if I type
${doc.MyElement[0]}
I get the undefined error
But, if I remove the Attribute from the XML document, and type the same thing
${doc.MyElement[0]}
I get a different error, to do with MyElement having Children.
I obviously expected and understand the second error, but why does it change simply by removing the attribute? Surely these should just get ignored?
Has anyone else had this problem? What’s the solution?
Thanks,
Basil
PS. Just to check I have the terminology right, when I say Attribute, I mean
“book type = fiction” as opposed to just “book”
From the example provided in the comments, the cause of the difference is that you add/remove the
xmlnsattribute, which has special meaning in XML. For thedoc.Dataquery to match theDataelement, both the element name and the XML namespace has to match. If there’s no XML namespace used,doc.Datawill match theDataelement. But if you put the elements in the XML into a XML namespace,doc.Datawill match 0 elements (as you haven’t set the default namespace in FreeMarker), sodoc.Datais a 0-length sequence, thus[0]doesn’t exist. See XML namespaces on http://freemarker.org/docs/xgui_imperative_learn.html for more information.