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

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Tuesday, October 21, 2008, 7:58:56 AM, Geoff Shuetrim wrote:

> 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?

It's not possible (as the prefix name in the XML source is irrelevant
according to the XML info-model), unless you set the XML parser to
namespace unaware, with
DocumentBuilderFactory.setNamespaceAware(boolean).

> 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=/
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user

-- 
Best regards,
 Daniel Dekany


-------------------------------------------------------------------------
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.