xpath仅查询子元素

时间:2015-09-25 09:08:37

标签: xpath

我正在尝试选择一个交互节点数组,然后遍历每个节点并从它的子节点中选择数据

var interactions = xpath.select('//interaction[@aoinclude="true"]', doc);

循环

async.each(interactions, parseInteraction, function (err) {
    if(err){
        // A interaction failed
    } else {
        callback(null, 'Fetched quiz data')
    }
});


function parseInteraction(interaction, callback){
    var type = xpath.select('//interaction/@type', interaction);
}

我的问题是parseInteraction从整个文档中获取节点,而不仅仅是当前交互节点,并输出我所有交互的数组

的xml:

<some elements>
    <interactions>
        <interaction>
            <some elements></some nodes>
        </interaction
    </interactions>
    <interactions>
        <interaction>
            <some elements></some nodes>
        </interaction
    </interactions>
    <interactions>
        <interaction>
            <some elements></some nodes>
        </interaction
    </interactions> 
</some nodes>

1 个答案:

答案 0 :(得分:2)

如果interaction中的//interaction/@type应该是interaction//interaction[@aoinclude="true"]的孩子,那么您只需移除descendant-or-self轴({{ 1}})来自前者:

//

或者在XPath开头使用dot(function parseInteraction(interaction, callback){ var type = xpath.select('interaction/@type', interaction); } )使其相对于当前上下文节点:

.