Freemarker:从节点中删除特定属性

Freemarker: Remove particular attribute from node

我有一个这样的 XML 节点:

    <table version="1.0" border="1" rules="all" cellpadding="10">
        .
        .
        .
    </table>        

我希望 Freemarker "echo" 这个,但排除版本属性,给这个:

    <table border="1" rules="all" cellpadding="10">
        .
        .
        .
    </table>        

我想我需要使用这样的宏:

    <#macro table>
        <table [include all attributes except version]>
            <#recurse>
        </table>
    </#macro>

但我不知道“[包括除版本之外的所有属性]”部分的内容。

我该怎么做?

FreeMarker 的 XML 包装器并不是真正用于将 XML 转换为类似的 XML,它只是一种公开数据的方式......但是,如果你稍微扩展一下,毕竟这是可能的(使用 http://freemarker-online.kenshoo.com/):

模板:

<#visit doc>

<#macro table>
  <table<@atts except=['a', 'c'] />>
    <#recurse>
  </table>
</#macro>

<#macro atts element=.node except=[]>
  <#list element.@@ as att>
    <#if !except?seqContains(att.@@qname)> ${att.@@qname}="${att?xml}"</#if><#t>
  </#list>
</#macro>

数据模型:

doc=<table a="1" b='2' c='3' />

但是,如果您具有属于命名空间的属性,那么这会存在一些陷阱。然后你需要用 <#ftl nsPrefixes="...">.

声明前缀