IanG
IanG

Reputation: 1549

How can I output an XML Node in Freemarker

I'm probably abusing Freemarker here, but I'd like to be able to use it to strip a wrapping element from around the outside of an XML document, something like:

<br:wrap xmlns:br="http://demo.tempuri.com/">
  <br:borrower>
     <br:id>111-11-1111</br:id>
     <br:ssn>111-11-1111</br:ssn>
     <br:city>Los Angeles</br:city>
     <br:first>John</br:first>
     <br:last>Smith</br:last>
     <br:phone>310-000-0000</br:phone>
     <br:state>CA</br:state>
     <br:zip>90025</br:zip>
  </br:borrower>
</br:wrap>

I want to remove the outer <wrap/> element. It's easy to select the inner document with:

<#ftl ns_prefixes={"D":"http://demo.tempuri.com"}>
<#assign borrower = doc.wrap.borrower>
{ "result" : "${borrower}" }

The problem here is that the 3rd line is going to result in the good old error:

For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this has evaluated to a sequence+hash

If I knew exactly what the content and structure of this inner document was I could just walk through it and output the scalar values, but changes all the time. I don't even know the namespaces of all the inner elements (this could even be a problem with the top-level inner object).

I know I could handle this scenario easily with XSLT, but I would much rather find an easy solution in Freemarker.

Any ideas?

Upvotes: 1

Views: 1185

Answers (1)

ddekany
ddekany

Reputation: 31152

${borrower.@@markup} should do this.

Upvotes: 4

Related Questions