PDA

View Full Version : Deep XPath + predicate syntax


johnafitch
07-28-2007, 06:14 PM
I'd like to set the xpath of a datapointer to a node within an arbitrarily deep XML dataset by using its attribute, decid. Currently I do this by using its depth in the dataset (an attribute, dlevel) and using (dlevel - 1) "*/" strings to look for the node of the appropriate type (typex) at the correct depth:

if (dlevel == "3") {
var xp = "my_ds:/mystuff/*/*/typex[@id=\'" + decid + "\']";
}
if (dlevel == "4") {
var xp = "my_ds:/mystuff/*/*/*/typex[@id=\'" + decid + "\']";
}

If my XML data is very deep, say 10 levels, I'll need the rather awkward string:

if (dlevel == "10") {
var xp = "my_ds:/mystuff/*/*/*/*/*/*/*/*/*/typex[@id=\'" + decid + "\']";
}

This is obviously very verbose and perhaps computationally inefficient. Given the xpath constructs supported by OL, is there a simple recursive approach to doing this?

Thanks,
JohnAFitch

Evangelus
07-29-2007, 03:21 AM
Hi John

Here is how I do something similar in a very different way.


<script>
function scan_subscription(p, sub)
{
if (p.selectChild(1))
{
if (p.getNodeName() == 'subscription')
if (p.getNodeText() == sub)
{
var f = p.dupePointer();
f.selectParent();
return f;
}

var t = scan_subscription(p, sub);
if (t != null)
return t;

while (p.selectNext())
{
if (p.getNodeName() == 'subscription')
if (p.getNodeText() == sub)
{
var f = p.dupePointer();
f.selectParent();
return f;
}

var t = scan_subscription(p, sub);
if (t != null)
return t;
}
p.selectParent();
}
return null;
}
</script>


HTH
Phil