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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Odd behaviour of DOM

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
I am getting mixed results when trying to select specific nodes...

Using this XML as a reference:
Code:
<DocumentRoot>
	<Parent>
		<Child>1</Child>
		<Child>2</Child>
	</Parent>
	<Parent>
		<Child>3</Child>
		<Child>4</Child>
	</Parent>
	<Parent>				
		<Child>5</Child>
		<Child>6</Child>
	</Parent>
</DocumentRoot>

If you don't change the "SelectionLanguage" to "XPath"...
Dom.setProperty "SelectionLanguage", "XPath"
//Parent[0] selects the Parent, rather than the normal //Parent[1]

So... Using XPath as the selection language...

Dom.selectNodes("//Child") Selects all 6 Child Nodes:
Code:
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<Child>4</Child>
<Child>5</Child>
<Child>6</Child>

If you use one of these:
Dom.selectSingleNode("//Child[1]")
Dom.selectSingleNode("//Child[2]")
It will work fine...

But if you use:
Dom.selectSingleNode("//Child[3]")
Dom.selectSingleNode("//Child[4]")
Dom.selectSingleNode("//Child[5]")
Dom.selectSingleNode("//Child[6]")
It will not select anything...

Though, if you use:
Dom.selectSingleNode("//Parent[2]/Child[1]")
Dom.selectSingleNode("//Parent[2]/Child[2]")
Dom.selectSingleNode("//Parent[3]/Child[1]")
Dom.selectSingleNode("//Parent[3]/Child[2]")
It will select these nodes...

Why does selectNodes("//Child") select all of the Children, but selectSingleNode("//Child[x]") does not...

I could have sworn that these selection methods (Such As //Child[6]) worked in XSL templates before...

Should I be using a different Selection Language for Dom?

Or was I just imagining things...?

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
The position predicate works on the position of the node in the context of its own node-set rather than that of the node-set you have selected. So:
Code:
Dom.selectNodes("//Child[1]")
Will return:
Code:
<Child>1</Child>
<Child>3</Child>
<Child>5</Child>
Whereas:
Code:
Dom.selectNodes("//Child[3]")
Will not return anything. This is true of XPath, so its the same for DOM and XSL. Hope this explains things.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
That's what I got out of it too...

For some reason, I guess I thought that...
SelectSingleNode("//Child[3]")
Was creating an collection and selecting the third instance the same way that...
SelectNodes("//Child")
Creates a collection...

O-Well...

You can still use:
SelectNodes("//Child")(2)
If needed...

Which I guess would be similar to:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    [b]<xsl:variable name="test" select="//Child" />
    <xsl:value-of select="$test[3]" />[/b]
  </xsl:template>
</xsl:stylesheet>


Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top