Re: XMLProcessing : testing optional value in a NodeModel
Daniel López <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Hi,
The explanation is clear and rational, but given that there cannot be
two attributes in one node with the same name, it feels
counter-intuitive to think in terms of "a sequence" when dealing with
node attributes, which probably explains why this sintax issues bites
people regularly.
Coming from a XML/XSLT/Xpath background, I'm afraid FreeMarker's syntax
to deal with XML does not feel intuitive. Using this case as an example,
why would one want to check if the "sequence of attributes exist"
(connection.@saveDir??) if the only way it can exist is if there is one,
so I will always have to use (connection.@saveDir[0]??). The same thing
happens in many cases with nodes, as checking if "it exists" is often
useless as one has to check if the size of the sequence is greater than
zero.
However, don't take that as a complain against FreeMarker. It's just
that the syntax is "close" to XPath but "not that close" and that causes
confusion to people with experience with XPath. And the "conditional
handling code" is usually more verbose in FreeMarker.
Again, not a complain but an explanation why I think people have
problems with this area. I'm very happy FM is even able to handle XML
and I do appreciate it :).
S!
D.
Daniel Dekany escribió:
> Tuesday, February 5, 2008, 5:49:14 PM, thierry.henrio wrote:
>
>> Hi all,
>>
>> I would like to conditionaly provide xml section if some attributes exists
>> in model
>>
>> here is my model
>> <?xml version="1.0" encoding="UTF-8"?>
>> <connection name="sle-001" template="sle-optional.ftl" saveDir="save">
>> </connection>
>>
>> model is parsed using dom api and a NodeModel is made out of it
>>
>> here is my template
>> <connections>
>> <connection name="${connection.@name}" url="imc:gl:slev42">
>> <#if (connection.@saveDir)??>
> [snip]
>
> The result of an expression like that is a node-set, as it can be not
> only a single node but multiple nodes as well, or 0 nodes. It's just
> like in XPath. Actually it's XPath syntactically ("@attName"). In the
> case of FreeMarker (with this ObjectWrapper anyway), the node-set is
> presented as a sequence of nodes. Thus the result itself is never
> "missing", it's just possibly an empty sequence. So, you had to write
> connection.@saveDir[0]??.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/