使用规则在数据映射器中进行 Xpath 调节

Xpath conditioning in datamapper using rule

如果下面的 XML 使用 datamapper 进行评估以仅将偶数编号的 id 映射到输出,那么使用 xpath(Rule) 的条件是: ?

 <employees>
        <employee>
            <id>1</id>
            <name>aaa</name>
        </employee>
        <employee>
            <id>2</id>
            <name>bbb</name>
        </employee>
        <employee>
            <id>3</id>
            <name>ccc</name>
        </employee>
        <employee>
            <id>4</id>
            <name>ddd</name>
        </employee>
    </employees>

我尝试使用 /employee[id mod 2 = 0]/id。如果输入只有一个偶数,它会映射;如果它在 input.xml 中有一个以上的偶数 id,它就会失败。 错误是

Result of xpath filling field 'id' contains two or more values!

规则独立于 DM 中配置的每个元素映射。规则将在要使用的元素映射配置中创建。

对于提供的示例 xml,应在 Foreach 'employee' -> 'employee' 元素映射中创建此规则。

试试这个规则配置 Name : {Rule Name} Type : Boolean Context : /employees/employee Xpath : /matches(string(id mod 2), '0')

这将创建一个规则,如果 id 为偶数则返回 true,如果 id 为奇数则返回 false。

在 DM 脚本视图中使用 if 块并检查规则的值。如果规则值为 true,则只映射 id。

It also worked in this way                                                                          
    Name : {Rule Name}
    Type : String
    Context : /employees/employee
    Xpath : /id mod 2 = 0 
and used the if condition as below : 
if(input.Ruleid.equals("true")){
    output.id = input.id;
    output.name = input.name;
} 

有没有其他不使用 if 条件的方法。