1

I'm using an XML datasource in an app. One of the fields called quantity is sometimes zero, thus:

<quantity>0</quantity>

However, when I try and get that value, the actual value in the JSON data structure isn't 0, it's just absent. Other fields show up with the $t element inside them, but this one doesn't:

'item' :{
  'quantity' :{},
  'name' :{'$t' : 'ItemName'}
}

I'd like to pick() out that value and determine whether it's a positive number (if it's zero, I do something else). How can I do that?

EDIT: When there is a normal numeric value, the XML looks like this

<quantity>100</quantity>

and the JSON looks like this

'quantity' :{'$t' :100}

This is how it should be.

Alex
  • 64,178
  • 48
  • 151
  • 180
Mike Grace
  • 16,636
  • 8
  • 59
  • 79

1 Answers1

2

I believe that this may currently be a bug but I have a way to get it to work until it gets fixed.

Example XML:

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
    yahoo:count="0" yahoo:created="2010-11-24T18:16:40Z" yahoo:lang="en-US">
    <diagnostics>
        <user-time>858</user-time>
        <service-time>0</service-time>
        <build-version>9847</build-version>
    </diagnostics> 
    <results/>
</query>

Example KRL:

ruleset a60x435 {
  meta {
    name "xml test"
    description <<
      testing xml node values that are 0
    >>
    author "Mike Grace"
    logging on
  }

  global {
    dataset testXml:XML <- "http://dl.dropbox.com/u/1446072/apps/test/test1.xml" cachable for 1 month;
  }

  rule getting_zero_value_from_xml_node {
    select when pageview ".*"
    pre {
      nodeValue = testXml.pick("$..service-time.$t");
      testValue = "#{nodeValue}".replace(re/(ARRAY).*/,"$1");
      nodeValue = (testValue eq "ARRAY") => 0 | nodeValue;
    }
    {
      notify("nodeValue",nodeValue) with sticky = true;
    }
  }
}

Explanation:

  • pick node value from dataset or datasource
  • force variable conversion into string and check for 'ARRAY'
  • set value of original variable to 0 if 'ARRAY' string found

When an XML node is converted into JSON and has a value of 0 it is turned into an empty hash. On the server, the variable is a pointer to a hash. Because we can force the pointer to a string and check for it, we can then handle the case when the node value is zero.

Mike Grace
  • 16,636
  • 8
  • 59
  • 79