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
kalyangkmAsked:
Who is Participating?
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
yes, but I think you should really look at your XML in an XML editor that allows you to collpase nodes, to understand the nesting. We can discuss this for ages based on flat text strings

You could open your XML in Internet Explorer, that will give you collapsing

For better understanding, I pretty formated your original XML and gave every node that is not a [1] an attribute has-preceding-sibling

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

Open in new window

0
 
Geert BormansInformation ArchitectCommented:
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
0
 
kalyangkmAuthor Commented:
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?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Geert BormansInformation ArchitectCommented:
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
0
 
kalyangkmAuthor Commented:
Can you please highlight it in the xml so that it can be better understood.
0
 
Geert BormansInformation ArchitectCommented:
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
0
 
kalyangkmAuthor Commented:
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.
0
 
Geert BormansInformation ArchitectCommented:
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
0
 
kalyangkmAuthor Commented:
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

0
 
kalyangkmAuthor Commented:
Perfect Thanks. I complicated things myself. Sorry for the trouble.
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.