Link to home
Start Free TrialLog in
Avatar of kalyangkm
kalyangkmFlag for United States of America

asked on

Node set explanation

Hi Folks,

I am trying to understand how node set works. I am trying to debugg the following simple code and understand why the result is coming as what it is. Please explain.

The following is the xml

<?xml version="1.0"?>
<test>
    <x a="1">
     <x a="2">
     <x a="2">
     <x a="2">
     </x>
     </x>
      </x>
   
    </x>
   <x a="1">
      <x a="2">
        <x a="2">
        <x a="6">
       <x a="6">
       </x> 
    </x> 
    </x> 
    </x> 
    </x> 
  <x a="1">
    <x a="1">
      <y>y11</y>
      <y>y12</y>
    </x> 
    </x> 
  <x a= "1">
  <x a="1">
  <x a="1">
      <y>y03</y>
      <y>y04</y>
       </x>
      </x>
    </x> 
</test>

Open in new window


XSLT for the above to get the node set counts.

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" />

<xsl:template match="/">
    //x,         <xsl:value-of select="count(//x)"/>
    //x[1],      <xsl:value-of select="count(//x[1])"/>
    //x/y,       <xsl:value-of select="count(//x/y)"/>
    //x/y[1],    <xsl:value-of select="count(//x/y[1])"/>
    //x[1]/y[1], <xsl:value-of select="count(//x[1]/y[1])"/>
</xsl:template>

</xsl:stylesheet>

Open in new window



Output:


    //x,         14
    //x[1],      11
    //x/y,       4
    //x/y[1],    2
    //x[1]/y[1], 2
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

a nodeset is a set (collection) of nodes.
An XPath expression selects a number of nodes based on a pattern
count() is a function that counts the number of nodes inside the set of nodes
//x,         14 => // means all descendants of the root, x means elements with name x, this expression equals /descendants::x... there are 14 x elements in teh XML
//x[1],      11 => all elemnts x in the document that are the first in their context (here, all elements x that don't have a preceding sibling x)
//x/y,       4 => all y that have a x parent
//x/y[1],    2 => all y that have a x parent and don't have an y preceding sibling
//x[1]/y[1], 2 => all y that have no preceding sibling y and have a parent x that does not have a preceding sibling x
Avatar of kalyangkm

ASKER

Could you please elaborate //X[1], I am having trouble understanding it. What exactly you mean by "all elements x that don't have a preceding sibling X".  

This is what I understand.

In the first set, there is one which has a preceding sibling

 
<x a="1">
     <x a="2">
     <x a="2">
    [b] <x a="2">
     </x>[/b]
     </x>
      </x>
   
    </x>

Open in new window


In the second set I see there is another as highlighted in bold

 
 <x a="1">
      <x a="2">
        <x a="2">
        <x a="6">
[b]       <x a="6">
       </x> [/b]
    </x> 
    </x> 
    </x> 
    </x> 

Open in new window


In the third set I see one

 <x a="1">
    [b]<x a="1">
      <y>y11</y>
      <y>y12</y>
    </x> [/b]
    </x> 

Open in new window


In the 4rth set I see one more.

 <x a= "1">
  <x a="1">
  <x a="1">
      <y>y03</y>
      <y>y04</y>
       </x>
      </x>
    </x> 

Open in new window


So total I see 4 out of 14 which have preceding x siblings. So why would //X[1] be 11 instead on 10?
preceding-sibling is a sibling
all the ones you "highlighted in bold" are parents
a preceding-sibling x  is an element x that is opened AND closed  before the context starts AND has the same parent

<x><x>.... first is parent of second
<x/><x>... first is preceding-sibling of second
Can you please highlight it in the xml so that it can be better understood.
I recommend that you use a tool such as www.oxygenxml.com so you can visualize this yourself

you have
<x a="1">
    <x a="1">

Open in new window

first x is not closed, so second x is a child

same here
    <x a="2">
     <x a="2">
       <x a="2">

Open in new window

the third x is a child of the second, the second is a child of the first

in your original XML
 
 line 12  <x a="1">
...
 line 21    </x> 
 line 22 <x a="1">

Open in new window


line 12 starts an element x
line 21 closes that particular element x
line 22 starts a new element x, so that is a sibling (child of the same parent) of the element x that starts on line 12
the element x that starts on line 12 is the preceding sibling for the element x that starts on line 22
I am using EditIX in mac.

So if I understand correctly from the analysis above; are the lines 3 to 5 the only ones which doesn't have preceding siblings?

and what about the ones from line 13 to 15?

Thanks.
13 to 15

      <x a="2">
        <x a="2">
        <x a="6">

Open in new window


open tag, open tag, open tag...
no closing so all nested
If so even the lines 4 to 5 have open tags just like 13 to 15. Please explain.

   <x a="2">
     <x a="2">

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect Thanks. I complicated things myself. Sorry for the trouble.