如何在foreach中处理xml? ->不兼容的类型:预期的“ xml”,找到的“xml | string”

问题描述

有人可以帮助我了解xml元素相对于foreach语句如何工作吗?

下面的示例代码显示了两种不同的方法来访问xml元素“ Child”。首先,直接访问所有“孩子”(第3行),然后仅访问foreach循环内的特定“人”的“孩子”(第5行)。

  1. 为什么会出现编译错误?
  2. 在遍历所有“人”时,我需要做什么以访问特定“人”的所有“子”元素?

test.bal:

function foo(xml input) returns boolean{
  xml listOfPersons = input/<Person>;
  xml listOfChildren = input/<Person>/<Child>;
  foreach var person in listOfPersons{
    xml childrenOfSinglePerson = person/<Child>;
  }
}

编译结果:

Compiling source
        test.bal
error: .::test.bal:5:20: incompatible types: expected 'xml',found '(xml|string)'

我正在使用Ballerina 1.2

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)