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]> |
Wednesday, October 22, 2008, 1:02:37 PM, Jonathan Revusky wrote: > If you simply parse the document using a parser that is not > namespace-aware, then you'll have the element names with the prefixes. > > Otherwise, I guess you need some way of exposing the namespace->prefix > reverse lookup table to your template. > > That actually should not be too difficult. You just need to expose a > method to the template that wraps the element.getPrefix() method from > the DOM API. Of course, element.prefix can't work, since there is no > way of knowing for sure that there isn't one or more child elements > named 'prefix' But element.@@prefix is the way we do these things (only @@prefix is not supported yet). Also it would be a bit confusing, as we prefer to use the prefixes assigned by <#ftl ns_prefixes=...> for obvious reasons, so it should be @@original_prefix. Still, the xmlns:... attributes had to be accessible as well, also CDATA vs PCDATA, etc., if someone wants to use this for XML source preproduction. > JR > > On Wed, Oct 22, 2008 at 12:49 PM, Geoff Shuetrim <[email protected]> wrote: >> Daniel >> >> Thanks for the quick response. Just to be absolutely clear, I am >> generating an XML DOM object using a namespace-aware document builder >> factory. That produces XML DOM objects where I can call the getPrefix() >> method (see http://tinyurl.com/56onqp) for element nodes and get back >> the namespace prefix used for that node. Thus, the original prefixes >> are exposed by my original data structure. However, when I access those >> same element nodes in the XML DOM from within a freemarker template and >> then use the getPrefix() method (eg: element.prefix) I run into a >> situation where I get back an empty freemarker.ext.dom.NodeListModel. >> >> Is there something about the way that the element nodes are being >> wrapped that hides the prefixes from me? >> >> Cheers >> >> Geoff Shuetrim >> >> >> >> >> On Wed, 2008-10-22 at 08:29 +0200, Daniel Dekany wrote: >>> 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] >>> > >>> > [/#list] >>> > [/#if] >>> > [/#macro] >>> > >>> > [#macro leftBracket symbol="<"] >>> > <span class="element">${symbol}</span> >>> > [/#macro] >>> > >>> > [#macro rightBracket symbol=">"] >>> > <span class="element">${symbol}</span> >>> > [/#macro] >>> > >>> > [#macro closeRightBracket symbol=">"] >>> > <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] -- 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=/