Tuesday, July 14, 2009

Xpath Struggles

Warning: This is a note about coding in C#. If that kind of thing is boring to you, don't read any more. It's also a little long.
This information was difficult to find, so I figured I would post it in case anyone else needed it in the future, including myself.

Giving an xml file like the following, I needed to write a parser(in C#) that would verify the existence of tags, and store them in a class.

<feed xmlns="http://www.w3.org/2005/Atom" lang="en-US">
<link rel="self" href="http://www.google.com">
<link rel="alternate" href="http://www.netflix.com">
<link rel="assetlinklogo" href="http://www.google.com%5Cimagefile.gif">
<link rel="assetlinkdemo" href="http://www.netflix.com%5Cdemo.aspx">
<link rel="assetlinkscreenshot1" href="http://www.google.com%5Cscreenshot1.gif">
<link rel="assetlinkscreenshot2" href="http://www.netflix.com%5Cscreenshot2.gif">
<averagerating xmlns="http://testserver:8080/syndicate.xsd">

There are 2 major problems.

The xml file resides in a "http://www.w3.org/2005/Atom" namespace, however specific tags reside in a different namespace: "http://testserver:8080/syndicate.xsd". Dealing with one namespace was annoying enough, how do we juggle two?

Also the assetlink tags are optional, there may be one hundred or none. I wanted to be able to put them in a list, but to exclude the self and alternate links which are required and will always show up.

To store the data I used a struct like this:

public struct BasicData
public XmlNode feed;
public XmlNode updated;
public XmlNode selfLink;
public XmlNode altLink;
public XmlNodeList assetLinks;
public XmlNode AverageRating;

I was used to reading xml files with XmlDocument (in the using System.Xml namespace):

BasicData basicData = new BasicData();
XmlDocument xmlFile = new XmlDocument();
XmlNode root;
root = this.xmlFile.DocumentElement;

"root" is now pointing to the base node in the xml document, in the case of this example it is the node. However we cannot access the children using xpath, only something like root.GetFirstChild() and GetNextSibling(). The normal xpath query would appear something like this:

basicData.updated = xmlFile.SelectSingleNode(@"/feed/entry/updated");

This function call is null because all the tags reside in a namespace. To deal with this we need to create a namespace manager and give our namespace a custom name(sometimes xml namespaces will be declared with names, but not in this case). This namespace manager is done as follows:

XmlNamespaceManager nsmgr;
nsmgr = new XmlNamespaceManager(this.xmlFile.NameTable);
nsmgr.AddNamespace("atom", @"http://www.w3.org/2005/Atom");
nsmgr.AddNamespace("ppt", @"http://testserver:8080/syndicate.xsd");

Remember I pointed out the file has 2 namespaces? We need to add both of them to the namespace manager since we will need both. Now that we have a namespace manager and the namespaces added, we can access the updated node and the self link node as follows:

basicData.updated = xmlFile.SelectSingleNode(@"/atom:feed/atom:entry/atom:updated", nsmgr);
basicData.selfLink = xmlFile.SelectSingleNode(@"/atom:feed/atom:entry/atom:link[@rel='self']", nsmgr);

And to access the node with the second namespace it is as follows:

basicData.updated = xmlFile.SelectSingleNode(@"/atom:feed/atom:entry/ppt:AverageRating", nsmgr);

Using the second namespace is done by using both in a single xPath.

Now to get the asset links but not the other links there is actually a "starts-with" function in xPath. In this case you would use it as follows:

basicData.assetLinks = xmlFile.SelectNodes(@"atom:feed/atom:entry/atom:link[starts-with(@rel, 'assetlink')]", nsmgr);

And there you go! The full code for this will look like this:

public struct BasicData
public XmlNode feed;
public XmlNode updated;
public XmlNode selfLink;
public XmlNode altLink;
public XmlNodeList assetLinks;
public XmlNode AverageRating;

BasicData basicData = new BasicData();
XmlDocument xmlFile = new XmlDocument();
XmlNode root;
XmlNamespaceManager nsmgr;
nsmgr = new XmlNamespaceManager(this.xmlFile.NameTable);
nsmgr.AddNamespace("atom", @"http://www.w3.org/2005/Atom");
nsmgr.AddNamespace("ppt", @"http://testserver:8080/syndicate.xsd");

basicData.updated = xmlFile.SelectSingleNode(@"/atom:feed/atom:entry/atom:updated", nsmgr);
basicData.selfLink = xmlFile.SelectSingleNode(@"/atom:feed/atom:entry/atom:link[@rel='self']";
basicData.assetLinks = xmlFile.SelectNodes(@"atom:feed/atom:entry/atom:link[starts-with(@rel, 'assetlink')]", nsmgr);

...(and so on)...

Wednesday, October 15, 2008

My last Political Post, I promise!


Obama and Biden released a plan for improving the United States standing in the sciences. As avid readers of my various blogs know, I was somewhat hesitant about supporting Obama back in the primaries due to his statement of cutting funding to NASA(he has since rescinded this position). America is not producing scientists or engineers of world class quality, rather those people are coming here. But that will not always be the case, as demonstrated by the Hadron Collidor that was constructed in Europe. There will be a time in the future when other countries are more attractive to the science community. That is why I find education, and in particular, science education, to be the most important issue of this or any political race.

The released plan is summarised as such:

• Restoring integrity to U.S. science policy to ensure that decisions that can be informed by science are made on the basis of the strongest possible evidence.

• Doubling over a 10 year period the federal investment in basic research by key science agencies, with a special emphasis on supporting young researchers at the beginning of their careers, and backing high-risk, high-return research.

• Making a national commitment to science education and training by recruiting some of America’s best minds to teach K-12 math and science and by tripling the number of the National Science Foundation’s Graduate Research Fellowships.

• Encouraging American innovation to flourish by making the R&D tax credit permanent, streamlining our patent system, eliminating the capital gains tax on start-ups and small businesses, and promoting the deployment of next-generation broadband networks.

• Addressing the “grand challenges” of the 21st century through accelerating the transition to a lowcarbon, oil-free economy, enabling all Americans to live longer and healthier lives, and protecting our country from emerging threats to our national security.
--Taken from the introduction to the linked article

I recommend everyone go read it, it only makes me feel better about the upcoming vote on Nov. 4th.

Wednesday, October 8, 2008

McCain hates planetariums

Watching last night's "Town Hall" style debate, I was struck by McCain's distaste for providing city children an updated solar system project for the Chicago Planetarium.

As America falls further and further behind the rest of the world in math and science, do we want a president that is willing to spend billions of dollars a year on a war with no end, but is going to withhold the splendor of the universe to inner city kids? Kids that probably can't even see the stars in the city?


Wednesday, September 10, 2008

Wednesday, July 30, 2008

Sesame Street was an amazing show.

I thought I would share some of these old Sesame Street songs with everyone. They are not all about Math/Science, but the first one is!

Born To Add

Listen to the Bells

Rebel L

Kids Just Love to Brush (I love the squeaks she makes in this song)

Angle Dance from Square One (Credit goes entirely to Noel, I forgot about this one)

I miss being a kid and watching these.

Tuesday, July 22, 2008

What is the output of the following C program?

Just thought I would share a little piece of hell with all of you. What does the following program print?

void main()
char *c[] = {"ENTER","NEW","POINT","FIRST"};
char **cp[] = { c+3, c+2, c+1, c };
char ***cpp = cp;
printf("%s", **++cpp );
printf("%s ", *--*++cpp+3);
printf("%s", *cpp[-2]+3);
printf("%s\n", cpp[-1][-1]+1);

Highlight below this line to see the answer.

Friday, July 18, 2008

Interview with a medium, the tough questions...

For some reason I read this interview with a psychic(oops, I mean medium...) and I am struck by the inanity of the questions. So here are questions I would like to put to this lady if I ever meet her. Feel free to add to these questions.

1. You say that the dead have memories of life, but also they do not have physical form. How is this possible, when the storing of memory appears to be linked to the physical structure of the brain? An example would be brain damage leading to memory loss. Wouldn't death be the ultimate brain damage?

2. You say that the dead have no physical form, yet also say they have tried to have sex with you(seriously?). How is this possible when sex is a physical act?

3. Do you see them with your eyes or hear them with your ears? If not, why do you say they appear to you in clothes?

4. If the dead don't have the 5 senses(as you claim) how can a dimensional choice(whatever that means), involve something like wonderful music or beautiful scenery?

Ultimately it seems like this sort of reality is exceptionally complex and improbable. What seems far more likely is people like this lady are so full of shit, they can't help having it come out of their mouths.

Wednesday, July 16, 2008

"Now we are all sons of bitches."

The quote above was spoken by Kenneth Tompkins Bainbridge, on July 16, 1945, sixty-three years ago today, after the detonation of the first atomic bomb.

I advocate scientific research and education nearly above all else, but we need to remember all advances must walk hand in hand with responsibility and maturity. By learning about the structure of matter, we also gained the ability to extinguish hundreds of thousands of lives in a matter of milliseconds.

Research cannot and should not operate in a vacuum. I would never advocate halting advances, but they must happen with the knowledge of the consequences. I ask that everyone, including myself, think about what happened 63 years ago and how it changed the world.

Friday, June 20, 2008

Easy day, Let's make fun of Creationists!

Should Evolutionists be allowed to vote?

"The facts warrent the violent expulsion of
all evolutionists from civilized society. I am quite serious that
their danger to society is so great that, in a sane society, they
would be, at a minimum, denied a vote in the administration of
the society, as well as any job where they might influence immature
humans, e.g., scout, or youth, leader, teacher and, obviously,

Thursday, June 5, 2008

Donald in Mathmagic Land

Part 1:

Part 2:

Part 3:

Special thanks to Felicia Chávez for the suggestion.