Jacob
Jacob

Reputation: 1004

xpath query only child element

I am trying to select a array of interaction nodes and then loop though each node and select data from it's child nodes

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

Loop

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);
}

My issue is that the parseInteraction fetches nodes from the entire document and not just the current interaction node and outputs an array of all my interactions

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>

Upvotes: 3

Views: 1459

Answers (1)

har07
har07

Reputation: 89305

If interaction as in //interaction/@type supposed to be the child of interaction as in //interaction[@aoinclude="true"], then you can simply remove the descendant-or-self axis (//) from the former :

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

Or use dot (.) at the beginning of the XPath to make it relative to current context node :

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

Upvotes: 4

Related Questions