How to still get the Key-Value pair even though Value in the Hash is null

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to still get the Key-Value pair even though Value in the Hash is null

ven12344
Hi,

I am getting the nodes from an xml file, like this

<#local titles = doc[XPATH] >

lets say for one element, this xpath does not exist. What I need to do is that I need to replace the "null" with "empty".

But I am not even getting this key-value pair in the local variable "titles", like Java Hash concept, if the value of the element in Hash is null then entire element becomes null, right.

I am looking solution in XPath, to check if the xpath exists then get the value of it otherwise put the value as "empty". But could not able to get the solution.

If anybody knows how to do it in Freemarker or XPath, I will appreciate the help.

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: How to still get the Key-Value pair even though Value in the Hash is null

Daniel Dekany
XML DOM nodes aren't regular hashes. They return a 0 length sequence
of nodes when there's no match. This is need for chaining
(foo.bar.baaz) and #list-ing to work regardless the number of hits (0,
1 or multiple). So, quoted from the Manual
(http://freemarker.org/docs/xgui_imperative_learn.html):

  It is important to realize the consequence that, for example, if
  book has no chapter-s then book.chapter is an empty sequence, so
  doc.book.chapter?? will not be false, it will be always true!
  Similarly, doc.book.somethingTotallyNonsense?? will not be false
  either. To check if there was no children found, use
  doc.book.chapter[0]?? (or doc.book.chapter?size == 0). Of course you
  can use similarly all the missing value handler operators (e.g.
  doc.book.author[0]!"Anonymous"), just don't forget that [0].


Tuesday, September 16, 2014, 12:08:49 AM, ven12344 wrote:

> Hi,
>
> I am getting the nodes from an xml file, like this
>
> <#local titles = doc[XPATH] >
>
> lets say for one element, this xpath does not exist. What I need to do is
> that I need to replace the "null" with "empty".
>
> But I am not even getting this key-value pair in the local variable
> "titles", like Java Hash concept, if the value of the element in Hash is
> null then entire element becomes null, right.
>
> I am looking solution in XPath, to check if the xpath exists then get the
> value of it otherwise put the value as "empty". But could not able to get
> the solution.
>
> If anybody knows how to do it in Freemarker or XPath, I will appreciate the
> help.
>
> Thanks.
>
>
>
> --
> View this message in context:
> http://freemarker.624813.n4.nabble.com/How-to-still-get-the-Key-Value-pair-even-though-Value-in-the-Hash-is-null-tp4655272.html
> Sent from the freemarker-user mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Want excitement?
> Manually upgrade your production database.
> When you want reliability, choose Perforce
> Perforce version control. Predictably reliable.
> http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

--
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce.
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to still get the Key-Value pair even though Value in the Hash is null

ven12344
Hi Daniel,

Thanks for replying. But I did what you are asking me to do, yet it is not working

I made changes like this

<#local map = {} >

<#if titleIndex < titles?size>
 <#if titles[titleIndex]?? >
  <#local map = map +  {"${chapter}" : "${titles[titleIndex]}"} >
 <#else>
  <#local map = map +  {"${chapter}" : ""} >
 </#if>
</#if>

Please let me know if anything is wrong. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: How to still get the Key-Value pair even though Value in the Hash is null

Daniel Dekany
For starters, if `titleIndex < titles?size` then
`titles[titleIndex]??` will be always true.

--
Thanks,
 Daniel Dekany


Tuesday, September 16, 2014, 4:05:41 PM, ven12344 wrote:

> Hi Daniel,
>
> Thanks for replying. But I did what you are asking me to do, yet it is not
> working
>
> I made changes like this
>
> <#local map = {} >
>
> <#if titleIndex < titles?size>
>  <#if titles[titleIndex]?? >
>   <#local map = map +  {"${chapter}" : "${titles[titleIndex]}"} >
>  <#else>
>   <#local map = map +  {"${chapter}" : ""} >
>  </#if>
> </#if>
>
> Please let me know if anything is wrong. Thanks.
>
>
>
> --
> View this message in context:
> http://freemarker.624813.n4.nabble.com/How-to-still-get-the-Key-Value-pair-even-though-Value-in-the-Hash-is-null-tp4655272p4655276.html
> Sent from the freemarker-user mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Want excitement?
> Manually upgrade your production database.
> When you want reliability, choose Perforce.
> Perforce version control. Predictably reliable.
> http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce.
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user