On Tuesday, 20 October 2015 at 16:53:19 UTC, holo wrote:
When im checking instance name with such code:

auto test = list.parseXPath(`//tagSet/item[key="Name"]/value`)[0].goCData;

it is compiling properly but it is breaking program when is no name set.

I make quick workaround:

auto tmp = list.parseXPath(`//tagSet/item[key="Name"]/value`);
            if(tmp == null)
            {
                instances[tmpinst].instanceName = "";
            }
            else
            {
auto instances[tmpinst].instanceName = tmp[0].getCData;
            }


but is there any reason why it is not taken into consideration in "getCData" method?

The issue you're running into is that parseXPath always returns an array of results, even when there are zero or only 1 result. kxml can't know in advance how many results there will be for the given query, so it will always return an array no matter how many results are found.

To work around this issue, you could define a function like so:

string getCData(XmlNode[]nodes) {
  if (!nodes.length) return "";
  return nodes[0].getCData();
}

and in use:

auto test = list.parseXPath(`//tagSet/item[key="Name"]/value`).getCData();

This takes advantage of UFCS to keep the call chaining and hide [0] while handling the possibility of an empty list.

Reply via email to