Archive for April, 2010

XPath Query On XML File Created From DataSet Returns Zero Records

April 20, 2010

Scenario

I had an XML file called LocationExtract.xml which I had created using DataSet.WriteXMLSchema. I wanted to modify the content of some of the nodes, so I used the SelectNodes method on the DocumentElement node of the XML file to select those nodes, but SelectNodes always returned zero records.

Solution

I had to to use the .NET XMLNamespaceManager object to add the namespace of the XML document to the collection of namespaces in the scope of my local XPath query. An XPath query needs to know the namespaces which it is operating on because it is by namespaces that XPath identifies XML documents. This MSDN document XML Namespaces and How They Affect XPath and XSLT explains this in detail.

Finding The XMLNamespace Of Your DataSet

If you inspect an XML file created from a strongly-typed DataSet you will find a line in it similar to the following: xmlns:http://tempuri.org/LocationExtractDataSet.xsd. That means that the XMLNamespace of the DataSet (.xsd file) named LocationExtractDataSet is http://tempuri.org/LocationExtractDataSet.xsd

Here’s The Code which allows an XPath Query to run against the XML file created from that DataSet (crucial bits in bold)

Dim reader As XmlTextReader = New XmlTextReader(Server.MapPath("LocationExtract.xsd"))
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader)
Dim topNode As XmlElement = doc.DocumentElement

'Set XPath Namespace
Dim topNode As XmlElement = doc.DocumentElement
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("loc", topNode.NamespaceURI)

Dim loc As XmlNodeList = topNode.SelectNodes("//ext:Location", nsmgr)

For Each locNode As XmlNode In loc
locNode.InnerText.Replace("Sugar", "Cinnamon")
Next

You will notice that I create an XMLNamespaceManager and add the namespace of my XML File to it with an associated prefix. The prefix is “ext” and the namespace is http://tempuri.org/LocationExtractDataSet.xsd. The weird thing is that the namespace is NOT available in doc.NamespaceURI. You have to get it from a Node in the document such as the DocumentElement

Having done this my XPath Query topNode.SelectNodes(“//ext:Location”, nsmgr) returns all the Location Nodes in the document LocationExtract.xml because it is in the namespace associated with the prefix “ext” . I then replace “Sugar” with “Cinnamon” in the manner approved by Jerry Seinfeld.

In short, if your XPath Query is returning zero records, you probably need to load the namespace of your XML file into an XMLNamespaceManager. Twenty Billion XPath Queries can’t be wrong.

Advertisements

Javascript Error: Expected ‘)’ Code 0 Line xxx Character xxx IE Only

April 14, 2010

Are You Receiving Me ?

Our corporate website is rendered in about 8 different languages across maybe 30 different domains (thus 30 websites), so our content is multi-lingual ranging from Thai to Arabic to English. A DropDownBox on the HomePage for each website gives the user the option to see the page in any language available to that domain.

Our Sources Of Javascript
The content itself is stored in a massive DataSet keyed to Page/Section/SubSection/Language/Domain. The Pages are rendered dynamically with each SubSection (can be as small as a few HTML-styled words) doing a look up to the DataSet from Markup in order to retrieve its Language/Domain specific content. The content can include any HTML including inline Javascript

Here’s an example from our Markup (i.e.aspx Page)
div id="OtherServiceBanner"
h4 %=WebContentDictionary.OtherServiceBannerTitle% /h4

Apart from inline Javascript we have the usual external js files.

Error Time

So our Web Designers report a JS error occurring on our French Language websites only. The error only occurs in IE.

I excluded the external JS files but the error still occurred, so then, using faithful old Divide and Conquer I progressively commented out all the calls to our Content DataSet until the error disappeared.

It didn’t take too long. The error was in some inline Javascript inserted by our Web Content Management Team whose understanding of HTML and Javascript is not immense.

Here’s the bad Javascript:
a id="ColContent" onclick="return myJSFunction('Visite virtuelle d’un bureau équipé',this.href);"

You will notice that the onclick attribute includes an embedded single quote character in d’un, so IE parsed this as return myJSFunction(‘Visite virtuelle d’ and complained about the ‘missing’ right parenthesis “)”

To correct the issue, the onclick attribute was edited like so :
onclick=”return myJSFunction(‘Visite virtuelle d&aposun bureau équipé’,this.href);”>

That &apos is the way you use an embedded apostophe (single-quote) in Javascript

Bottom line: when you get Expected ‘)’ in your Javascript error, look for improperly terminated string literals and make sure you account for all your sources of Javascript. Then use Divide and Conquer to find the culprit. You’ll probably find you need to use &apos., if not you’ll have to make other plans for Nigel.