Solved

Node set explanation

Posted on 2015-01-30
11
88 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
  • 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 500 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Adobe Customization Wizard XI issues 26 166
squareUp  challenge 22 106
scoresAverage challenge 8 75
Path to Python 9 42
This article will show, step by step, how to integrate R code into a R Sweave document
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now