How do I access the original name for XML elements, including the original namespace prefix.

Geoff Shuetrim <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Organization Galexy
Message-ID <1224568736.12302.15.camel@dell>
I am trying to use Freemarker to produce an HTML representation of an
XML document, maintaining all of the information in the original
document (including xmlns:prefix="namespaceURI" declarations and the
prefixes on element names).  

Note that I do not know, in advance, what the namespaces used in the XML
document will be, so I cannot declare them in my Freemarker template.  

When using the XML processing capabilities of Freemarker to access
elements in the XML document, is there some way for me to access the
original node name including the original prefix?

Regards

Geoff Shuetrim

As background, I am currently generating the HTML rendering of the XML
using the following macros:

[#escape x as x?html]
[@displayChildren parent=someElementNode indent=0 /]
[/#escape]


[#macro displayChildren parent indent]

[#if parent?node_type="text"]
[#if parent?trim!=""]
<br/>
[@space indent=indent/]
[@elementText text="${parent?html}"/]
[/#if]
[/#if]

[#if parent?node_type="element"]

[#if indent > 0]<br/>[/#if]
[@space indent=indent/]
[@leftBracket/]
[@elementName text="${parent?node_name}"/]

[#assign children = parent?children /]
[#assign hasChildren = (children?size != 0) /]
[#assign attributes = parent.@@ /]
[#assign hasAttributes = (attributes?size != 0) /]
[#assign isLeafNode = (children?size = 0) /]

[#if hasAttributes]
[#list attributes as attribute]
[#assign markup = attribute.@@markup?html /]
[#assign parts = markup?split("=") /]
[#assign name = parts[0] /]
[#assign value = parts[1] /]
<br/>[@space indent=(indent+1)/]
[@attributeName text="${name}"/]
=
[@attributeValue text="${value}"/]
[/#list]
[/#if]

[#if isLeafNode]
[@closeRightBracket /]
[#else]
[@rightBracket /]
[#list children as child]
[@displayChildren parent=child indent=(indent+1) /]
[/#list]
<br/>
[@space indent=indent /]
[@leftBracket /]
/
[@elementName text="${parent?node_name}" /]
[@rightBracket /]
[/#if]

[/#if] 

[/#macro]

[#macro space indent]
[#if indent!=0]
[#list 1..indent as tab]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
[/#list]
[/#if]
[/#macro]

[#macro leftBracket symbol="&lt;"]
<span class="element">${symbol}</span>
[/#macro]

[#macro rightBracket symbol="&gt;"]
<span class="element">${symbol}</span>
[/#macro]

[#macro closeRightBracket symbol="&gt;"]
<span class="element">/${symbol}</span>
[/#macro]

[#macro elementName text]
<span class="elementName">
${text?html}
</span>
[/#macro]

[#macro elementText text]
<span class="elementText">${text}</span>
[/#macro]

[#macro attributeName text]
<span class="attributeName">${text}</span>
[/#macro]

[#macro attributeValue text]
<span class="attributeValue">${text}</span>
[/#macro]



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.