Difficulty with XPath and Namespaces using Eclipse with simple ANT build
Robert Johnson <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Hello
Put simply, I cannot get Freemarker to process XPath queries or recognise XML namespaces - even using the examples out of the documentation. I am sure this must mean I'm doing something silly, but I cannot work out what.
Specifically; my XML document looks like this:-
<?xml version="1.0" encoding="UTF-8"?>
<book title="Test">
<chapter title="Ch1">
<para>p1.1</para>
<para>p1.2</para>
<para>p1.3</para>
</chapter>
<chapter title="Ch2">
<para>p2.1</para>
<para>p2.2</para>
</chapter>
</book>
and my template like this
<#assign book=document.book>
<h1>${book.@title}</h1>
<#list book.chapter as ch>
<h2>${ch.@title}</h2>
<#list ch.para as p>
<p>${p}
</#list>
</#list>
<---- Next list --->
<#list document["book/chapter[title='Ch1']/para"] as p>
<p>${p}
</#list>
<--- do the original again -->
<#list book.chapter as ch>
<h2>${ch.@title}</h2>
<#list ch.para as p>
<p>${p}
</#list>
</#list>
<---- Count -->
${document["count(//para)"]}
and my output looks like
<h1> title="Test"</h1>
<h2> title="Ch1"</h2>
<p><para>p1.1</para>
<p><para>p1.2</para>
<p><para>p1.3</para>
<h2> title="Ch2"</h2>
<p><para>p2.1</para>
<p><para>p2.2</para>
<---- Next list --->
<--- do the original again -->
<h2> title="Ch1"</h2>
<p><para>p1.1</para>
<p><para>p1.2</para>
<p><para>p1.3</para>
<h2> title="Ch2"</h2>
<p><para>p2.1</para>
<p><para>p2.2</para>
<---- Count -->
As you can see, the XPath just doesn't return anything (nor any error) and yet all this is a direct copy out of the documentation on-line.
It gets worse if I try and use namespaces.
Change the basic XML to look as below
<?xml version="1.0" encoding="UTF-8"?>
<e:book xmlns:e="http://example.com/ebook"
title="Test">
<e:chapter title="Ch1">
<e:para>p1.1</e:para>
<e:para>p1.2</e:para>
<e:para>p1.3</e:para>
</e:chapter>
<e:chapter title="Ch2">
<e:para>p2.1</e:para>
<e:para>p2.2</e:para>
</e:chapter>
</e:book>
Use the template that follows:
<#ftl ns_prefixes={"e":"http://example.com/ebook"}>
<#recurse>
<#macro "e:book">
I'm the book element handler. ${.node.@title}
</#macro>
and get this output (which is correct)
I'm the book element handler. Test
BUT, use this template
<#ftl ns_prefixes={"e":"http://example.com/ebook"}>
<#assign book=document["e:book"]>
<h1>${book.@title}</h1>
<#list book["e:chapter"] as ch>
<h2>${ch.@title}</h2>
<#list ch["e:para"] as p>
<p>${p}
</#list>
</#list>
<---- Next list --->
<#list document["e:book/e:chapter[title='Ch1']/e:para"] as p>
<p>${p}
</#list>
<--- do the original again -->
<#list book["e:chapter"] as ch>
<h2>${ch.@title}</h2>
<#list ch["e:para"] as p>
<p>${p}
</#list>
</#list>
<---- Count -->
${document["count(//e:para)"]}
and you get a Java exception
[freemarker] Mar 26, 2012 4:15:35 PM freemarker.log.JDK14LoggerFactory$JDK14Logger error
[freemarker] SEVERE: Template processing error: "Namespace prefix e is not registered."
[freemarker] Namespace prefix e is not registered.
[freemarker] The problematic instruction:
[freemarker] ----------
[freemarker] ==> assignment: book=document["e:book"] [on line 2, column 1 in books.ftl]
even if the syntax were wrong in itself, the namespace has been registered. So it seem that ns_prefixes works if you use declaratives (recurse) but not if you use imperatives.
Can any kind person suggest what I am getting wrong??
Thanks in advance
Bob Johnson
------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user