Home

XPath and Boolean Operations

   

Boolean Node Searching

 

AND Both Child Nodes

You may have noticed that, in our XML document, some parent nodes include some child nodes that are not found in other parent nodes. For example, the first, the second, and the third videos of our XML document have a <format> child node but the fourth video does not. On the other hand, the second, the third, and the fourth videos have a <categories> child node while the first does not.

You can ask the XmlNodeList.SelectNodes() method (in fact the XML interpreter) to produce in its results only the parent nodes that have a combination of certain two nodes. To get such a result, add a first combination that includes one of the names of child nodes. Include a second pair of square brackets with the other name. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[format][categories]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

AND Both Child Nodes

AND Both Child Nodes

The "and" operator is used to check that two child nodes are found under a parent node. To use it, create the square brackets and in them, provide the names of two child nodes separated by the and operator. Here is an example:

XmlNodeList xnlVideos = xeVideo.SelectNodes("//videos/video[format and rating]");

In this case, only parent nodes that have both child nodes would be produced.

OR Either Child Node

To produce the parent nodes that include one or the other child node, use the or operator. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[cast-members or categories]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

In this case, only parent nodes that have both child nodes would be produced:

AND Both Child Nodes

Accessing the Grand-Children by Position

AND Both Child Nodes

Accessing the Nodes With Specific Grand-Children

Once you have accessed the nodes, you can apply any concept we have reviewed so far to locate a specific node. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members[actor or narrator][. = 'Danny DeVito']");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.InnerText,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

NOT This Child Node

To let you get only the nodes that do not have a certain child node, the XPath provides a Boolean function named not. To use it, pass the name of the node to it. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[not(cast-members)]");

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

When the not() function returns, it produces only the nodes that don't have the child-node whose name was passed:

NOT This Child Node

NOT This Child Node

Sets Combinations

All the expressions we have seen so far produced only one result each. To let you combine two results into one, the XPath language provides the | operator. It must be applied between two expressions. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("//cast-members[actor='Danny DeVito'] | //cast-members[actor='Eddie Murphy']");

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}
 
 
 

Boolean Operations

 

Numeric Comparisons on a Node Value

The XPath language provides some operators that can be used to perform Booleans operations. The numeric comparisons are used to find the Boolean relationship between two values. Only some operators are allowed. If you use an operator that is not valid, the compiler would throw an exception. The valid Boolean operators and the rules are:

  • = Both values are the same. This operator can be applied to strings (and characters or symbols) or numeric values. Here is an example:
    using System;
    using System.Xml;
    using System.Windows.Forms;
    
    public class Exercise
    {
        public static int Main()
        {
            XmlDocument xdVideos = new XmlDocument();
    
    	xdVideos.Load("../../Videos.xml");
    
    	XmlElement xeVideo = xdVideos.DocumentElement;
    	XmlNodeList xnlVideos = xeVideo.SelectNodes("//videos/video/cast-members[actor = 'Sheryl Lee Ralph']");    
    
    	foreach(XmlNode xnVideo in xnlVideos)
    	    MessageBox.Show(xnVideo.InnerXml,
    			    "Videos with both Format and Category sections",
    			    MessageBoxButtons.OK,
    			    MessageBoxIcon.Information);
    
    	return 0;
        }
    }
  • > or &gt; The left value is greater than the right value. This operator can be applied only to numeric values. If the operator is applied to a string, the XmlNodeList.SelectNodes() method would return nothing; no exception would be thrown. If you want to perform such comparisons as to find out if one string is alphabetically lower than another, call the Compare() method of the string class
  • >= The left value is greater than, or equal to, the right value. This operator follows the numeric and string rules of the > operator
  • < or &lt; The left value is lower than the right value
  • <= The left value is lower than, or equal to, the right value
  • != The values are different. This operator can be applied to strings and/or numeric values

Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("//videos/video[length >= 100]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.InnerXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

Logical Conjunction

Logical conjunction consists of combining two Boolean operations that must both produce true results. To create a logical conjunction, create some square brackets applied to the parent name of a node and create the conjunction operation in the square brackets. There are two main ways you can perform the operation:

  • To apply the same criterion to two different nodes, in the square brackets, type the name of first node and apply the desired Boolean operation to it, followed by the name of the other node and apply the same Boolean operation to it. Separate the operations with the and operator. For example, from our XML, we have two videos directed by Dany DeVito. That person is an actor in one of the videos but is not an actor in the other. Imagine that you want to get only the video where he is an actor. You can apply the ='Danny Devito' comparison to both the director and the actor node. Here is an example:
    using System;
    using System.Xml;
    using System.Windows.Forms;
    
    public class Exercise
    {
        public static int Main()
        {
            XmlDocument xdVideos = new XmlDocument();
    
    	xdVideos.Load("Videos.xml");
    
    	XmlElement xeVideo = xdVideos.DocumentElement;
    	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[director='Danny DeVito' and actor='Danny DeVito']");
    
    	foreach(XmlNode xnVideo in xnlVideos)
    	    MessageBox.Show(xnVideo.OuterXml,
    			    "Videos with both Format and Category sections",
    			    MessageBoxButtons.OK,
    			    MessageBoxIcon.Information);
    
    	return 0;
        }
    }
    By the way, if one the nodes in the square brackets is from a different level, precede its name with //. Here is an example:
    using System;
    using System.Xml;
    using System.Windows.Forms;
    
    public class Exercise
    {
        public static int Main()
        {
            XmlDocument xdVideos = new XmlDocument();
    
    	xdVideos.Load("Videos.xml");
    
    	XmlElement xeVideo = xdVideos.DocumentElement;
    	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[director='Danny DeVito' and //actor='Danny DeVito']");
    
    	foreach(XmlNode xnVideo in xnlVideos)
    	    MessageBox.Show(xnVideo.OuterXml,
    			    "Videos with both Format and Category sections",
    			    MessageBoxButtons.OK,
    			    MessageBoxIcon.Information);
    
    	return 0;
        }
    }
  • To apply different criteria to the same node, in the square brackets, type the name of the node and apply the desired Boolean operation to it, followed by the name of the same node and apply the other Boolean operation to it. Separate the operations with the and operator. For example, from our XML, we have two videos directed by Dany DeVito. Imagine that you want to get only one of them. You can create of the operations as direcor='Dany Devito' and the other operations that is makes one video different from the other. Here is an example:
    using System;
    using System.Xml;
    using System.Windows.Forms;
    
    public class Exercise
    {
        public static int Main()
        {
            XmlDocument xdVideos = new XmlDocument();
    
    	xdVideos.Load("Videos.xml");
    
    	XmlElement xeVideo = xdVideos.DocumentElement;
    	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[director='Danny DeVito' and title='The War of the Roses']");
    
    	foreach(XmlNode xnVideo in xnlVideos)
    	    MessageBox.Show(xnVideo.OuterXml,
    			    "Videos with both Format and Category sections",
    			    MessageBoxButtons.OK,
    			    MessageBoxIcon.Information);
    
    	return 0;
        }
    }

In the same way, you can create as many conjunctions as you want, by separating the operations with the and operator.

Logical Disjunction

Logical disjunction consists of applying the same criterion to two nodes or applying different criteria to the same node so that at least one of the operations needs to be true. To create a logical disjunction, apply the square brackets to the parent node. As seen for the conjunction, there are two main ways to use a logical disjunction:

  • To get a result where the same operation is applied to two different nodes but at least one of both comparisons produces a true result, in your XPath expression, type the name of the parent node followed by square brackets. In the square brackets, type the name of one of the nodes and apply the desired Boolean operation to it, followed by the name of the other node and apply the same Boolean operation to it. Separate the operations with the or operator. Here is an example that gets videos in which either Eddie Murphy or Danny DeVito star:
    using System;
    using System.Xml;
    using System.Windows.Forms;
    
    public class Exercise
    {
        public static int Main()
        {
            XmlDocument xdVideos = new XmlDocument();
    
    	xdVideos.Load("Videos.xml");
    
    	XmlElement xeVideo = xdVideos.DocumentElement;
    	XmlNodeList xnlVideos = xeVideo.SelectNodes("//cast-members[actor='Danny DeVito'  or actor='Eddie Murphy']");
    
    	foreach(XmlNode xnVideo in xnlVideos)
    	    MessageBox.Show(xnVideo.OuterXml,
    			    "Videos with both Format and Category sections",
    			    MessageBoxButtons.OK,
    			    MessageBoxIcon.Information);
    
    	return 0;
        }
    }
  • To get a result where different operations are applied to thwo nodes but at least one of both comparisons produces a true result, in your XPath expression, type the name of the parent node followed by square brackets. In the square brackets, type the name of node and apply the desired Boolean operation to it, followed by the name of the same node and apply the other Boolean operation. Separate the operations with the or operator. Here is an example that gets videos directed by either Danny DeVito or Roland Emmerich:
    using System;
    using System.Xml;
    using System.Windows.Forms;
    
    public class Exercise
    {
        public static int Main()
        {
            XmlDocument xdVideos = new XmlDocument();
    
    	xdVideos.Load("Videos.xml");
    
    	XmlElement xeVideo = xdVideos.DocumentElement;
    	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[director='Danny DeVito'  or director='Roland Emmerich']");
    
    	foreach(XmlNode xnVideo in xnlVideos)
    	    MessageBox.Show(xnVideo.OuterXml,
    			    "Videos with both Format and Category sections",
    			    MessageBoxButtons.OK,
    			    MessageBoxIcon.Information);
    
    	return 0;
        }
    }

By using the Boolean search operators (and, or, and the not() functions) and the logical conjunction/disjunction operations, you can create tremendous combinations to include and exclude some nodes in the XPath expression and get the desired results.

 
 
   
 

Home Copyright © 2014, FunctionX