Extracting information from XML
Eric Willigers <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Hello,
I'm trying to extract information from some XML files, but I'm
encountering a couple of unusual cases (below).
Any suggestions are welcome.
Lots of thanks,
Eric.
Question 1.
We want to extract "hyphenated-name" nodes from some XML:-
<root>
<hyphenated-name data="first"/>
<hyphenated-name data="second"/>
</root>
I would like to use
<#list document.root.hyphenated-name as node>
but the hyphen prevents this from working. None of these work either:-
<#list document.root."hyphenated-name" as node>
<#list document.root.'hyphenated-name' as node>
<#list document.root.`hyphenated-name` as node>
I ended up using
<#list document.root?children as child>
<#if child?node_type = 'element' && child?node_name =
"hyphenated-name">
Are there better options?
Question 2.
How should I extract an attribute from an XML node, when the name of the
attribute is given by a variable?
e.g. is there something like ${node.@$attrName}
Here's my XML:-
<root>
<attr name="a" />
<attr name="b" />
<data b="B" a="A" />
</root>
I tried
<#list document.root.data as data>
<#list document.root.attr as attr>
${data.@$attr.@name}
</#list>
</#list>
but this caused
SEVERE: javax.xml.transform.TransformerException: A node test that
matches either NCName:* or QName was expected.
A bug?
------------------------------------------------------------------------------