Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Node set explanation

Posted on 2015-01-30
11
Medium Priority
?
96 Views
Last Modified: 2015-02-05
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
0
Comment
Question by:kalyangkm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40581336
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
 

Author Comment

by:kalyangkm
ID: 40582028
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40582218
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kalyangkm
ID: 40587755
Can you please highlight it in the xml so that it can be better understood.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40588104
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
 

Author Comment

by:kalyangkm
ID: 40588788
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40588962
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
 

Author Comment

by:kalyangkm
ID: 40589603
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
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 40589802
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
 

Author Comment

by:kalyangkm
ID: 40591480
Perfect Thanks. I complicated things myself. Sorry for the trouble.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40591608
welcome
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Starting up a Project

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question