Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

position of node in ancestor::

Status
Not open for further replies.

GIGN

Programmer
Oct 6, 2000
1,082
NZ
I need to be able to find the position of a parent node.

To find the path I am using:

ancestor::CONTACT

I want to be able to find the position on that CONTACT node, but cannot seem to apply position() in this context - what operator do I use?

ancestor::CONTACT/position() does not work.

The reason is - I need to send this index to another page to delete it - hence I need it's position, otherwise will have to send the entire contents of the node.

Any work arounds?

<bb/>
 
One thing would be using node ID's, but I don't think the node ID's are set by default.

What should you do, if you are using DOM (I guess so, I am not sure from what you've written above), is to get the parent's children and walk the list untill you find the node you are looking for. You can tell the position from that.

One performance enhancer would be to start walking from the ancestor untill reaching the begining or the end of the children and infere the position from there... Something like the C++ code below:

Code:
DOM_Node Parent = currentNode.getParentNode();
DOM_NodeList nl = parent.getChildNodes()
int count = nl.getLength();
int nextSiblingCount = 0;
int prevSiblingCount = 0;
bool goOn = true;

while (goOn)
{  DOM_Node sibling = currentNode.getNextSibling();
   if (sibling != NULL) // xerces has such an operator
     nextSiblingCount++;
   else
   {   position = count - nextSiblingCount;
       goOn = false;
   }
   if (goOn)
   { sibling = currentNode.getPrevSibling();
     // same as above.
   }
}

All the methods there are DOM methods; this should work; actually I used something very similar in my C++ programs.

HTH. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
hmm yes you are right.

The only way I can think of doing it is to find the position of the node within the context of the container node CONTACT is in, something like this (your version of ancestor::CONTACT/position() isnt liked by my parser tho, so i've had to use for-eachs):

<xsl:template match=&quot;blarg&quot;>
<xsl:for-each select=&quot;ancestor::contact&quot;>
<xsl:for-each select=&quot;//foo/contact[current()]&quot;>
<xsl:value-of select=&quot;position()&quot;/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>



hope that helps

 
I should have mentioned this is an XSL template, so I can't do that.

I can't add an attribute when the node is appended, because the file is very dynamic - unless I reset all the nodes every time one is added or removed, which becomes very tedious, but may actually be the only way.

The other thing I can do is use the entire ancestor:: for the node, but this seems less than efficient.

See what it is is a link to remove a node, but is inside the CONTACT - hence I need to know which CONTACT node to use to find the right node to delete.

bit of a bummer really.

<bb/>
 
well what about using keys to identify the node then?

look at
a key can be generated for any node and is unique so you can basically know which node your ancestor is via the key interface and not have to worry about positions.

Its also dynamic, so you wont have to hand enter the key values :)

hope that helps
 
Could do, but I am accessing the file from another part of application - so keys will die with the template won't they?

I guess I could id it too - actually that may work - if I tag it each time the XSL sheet is parsed - and then use this later on.

<bb/>

 
why not include the template within the template you are using, and access it as if it were part of its own template?
 
or, you could use the key tag to create external keys for the other xsl file like this:
<xsl:variable name=&quot;href&quot; value=&quot;...
<xsl:key match=&quot;document($href)//CANDIDATENODEHOLDER&quot;/>

dunno if this will work but its worth a try and its quite cool :)
 
I left it for a while then realised all I needed to do was send the position as a variable from the CONTACT node when I called the EVENT template.

Bit of a dumbass mistake realy ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top