Archive for 2006
Dynamic Expression Evaluation in .Net using XSLT
Parsing and evaluating a mathematical or logical expression at runtime isn’t as easy as most people would like to think. There’s no built in .Net functionality to do it. So, if you want to include dynamic evaluation, there are several options.
- Find an already-built third-party component or ActiveX control.
- Build your own formula string parser ala Compiler Theory and Design. (This is something many of us did during our college days; doable but very time consuming.)
- Dynamically generate and compile .Net code inserting the mathematical expression into it.
- Last but not least, you can use XSL’s built-in parsing and expression evaluation functionality.
The fourth option has the luxury of being cheap, relatively efficient, and available in .Net today. With just a function or two, you can be using it in less than an hour.
To implement an XSLT solution, you need some XML. In this case, the XML is meaningless and is only defined so that we have something to apply the XSL to. For example, take the following super-simple XML:
<?xml version=”1.0″ encoding=”UTF-8″?><Root></Root>
It doesn’t get much simpler than that. Now that we have our XML, we need the XSL — also very simple.
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xml:space=”default” >
<xsl:output method=”xml” version=”1.0″ encoding=”UTF-8″ indent=”yes” standalone=”yes” omit-xml-declaration=”yes” />
<xsl:param name=”Formula” select=”0″ />
<xsl:template match=”/” >
<xsl:value-of select=”$Formula”/>
</xsl:template>
</xsl:stylesheet>
Looks easy, doesn’t it? As long as the parameter passed in as “Formula” is a valid XSL expression, the XSL will calculate it and the result will be returned as text containing the value. You can even pass in a Boolean expression, like “3=4,” which will return the value “false.” As shown in bold above, the omit-xml-declaration is important. We want the result to contain the value of the calculation without the XML header.
So how does this look inside a function? Let’s assume we already defined a function, called “ApplyXSL,” that is designed to: accept XML and XSL strings, transform them, and return the result in a string. (In your own code, you can call your favorite XSLT functionality instead.)
Further, we will forego passing in parameters and using separate XML & XSL files in favor of putting everything in one self-contained function (example in VB, but easily ported to C#) — like this:
Public Shared Function EvalAnyExpression(ByVal psExpression As String, ByVal psDefault As String) As String
‘ returns the String evaluation of the passed string
Dim sRes As String = psDefault
Dim sXSL As String = “”
Const csXML As String = “<?xml version=”1.0″ ?><Root></Root>”
Const csXSL As String = “<?xml version=”"1.0″” encoding=”"UTF-8″” ?>” & vbCrLf & _
“<xsl:stylesheet version=”"1.0″” xmlns:xsl=”"http://www.w3.org/1999/XSL/Transform”" >” & _
“<xsl:output method=”"xml”" version=”"1.0″” encoding=”"UTF-8″” indent=”"yes”" standalone=”"yes”" omit-xml-declaration=”"yes”" />” & vbCrLf & _
“<xsl:template match=”"/”" xml:space=”"default”" >” & _
“<xsl:value-of select=”"EXPRESSION”"/></xsl:template></xsl:stylesheet>”
Try
sXSL = Replace(csXSL, “EXPRESSION”, psExpression)
sRes = ApplyXSL(csXML, sXSL)
Catch ex As Exception
‘ Log the error
‘ and any other Error trapping functionality your application needs
End Try
Return sRes
End Function
Notice how the XML and XSL are inline constants, then the EXPRESSION string is replaced with the value of the actual expression to be evaluated.
Once a formula is passed into the function, it’s inserted into the XSL, parsed, and the results are returned as a string. The resulting string can then be converted to an Int, Single, Boolean, or whatever you might be expecting from the passed formula.
This is just an example, of course, meant as a quick-start for your code. It can be optimized in several ways, like pre-parsing the XSL and storing it in an XPath object somewhere. It could also be made more flexible, like putting the XML/XSL into files or fields in a database.
Buying a computer… again.
Time goes on, the computer gets older, slower, less productive… now what? Upgrade old? Buy new? Just deal?
Every person or business has a different set of requirements for their computer. The first step in determining what to buy (or not buy) is: Make a list.
Here’s how the list should flow:
- What do I do with my computer now?
Note all the programs you use and how often you use them.
If more than one person uses the computer, include their tasks. - What would I like to do with my computer later?
Note all the tasks you would like to perform with your computer in the future. - How much money can I spend?
This needs to be greater than zero in order to proceed, so be specific.
Once this list is defined in detail, it’s time to determine next steps. The solution is not always going to include buying a new computer, so it’s important to define the need thoroughly. From here, we compare your current and future needs with the available options.
Option 1: Upgrade Your Computer
Depending on your existing computer’s overall capability, it may be feasible to improve it and meet your needs in the process. This is usually the case with custom-built computers, as they are typically more upgradable than a pre-built/pre-packaged machine. A careful review of the specifications along with pricing research on new hardware will reveal this option’s practicality. This option saves money, usually.
Option 2: Buy a New Computer (or build one)
After determining that an upgrade is out of the question, we move on to the “new machine” category. The decision tree splits again and we have to decide on whether to buy a pre-built machine or a custom-built machine. And note: When a computer seller allows you to “customize” your computer purchase is does not necessarily mean that you are custom-building your machine. It often means you are selecting from a limited set of choices that are predetermined.
There are MANY computers out there and research is key. With any component of the machine we need to know these three things:
- Does it meet my requirements?
- Does it have an reputable warranty and service program?
- Is it within my budget?
The most difficult part of the research is answering question #1. A thorough understanding of how a computer works will serve you well here. An incorrect answer here will cause problems later. Especially when building a machine, the details are very important. As an example of how many details there are, just take a peek at this hardware tutorial! You may want a translator to help you decipher it.
Option 3: Just Deal
Okay, so you’re not ready for a new machine. That’s okay! There are plenty of ways to keep your machine up and running so you can continue your daily routine. Here are the big things:
- Defragment your hard disk regularly
- Update virus and spyware scanners and scan daily
- Install all appropriate Windows updates
- Back-up your data regularly
Consensus:
This is a lot of information to absorb, but it should give you a starting point for your computer-buying adventure. Don’t forget that you can look on the Internet for buying guides, reviews and other sources that will help you make a decision.
Note: The information above is specific to PCs with Microsoft Windows.
Why do I need a website?
Today’s information directory is online. Everything on the Internet is quickly becoming a mirror image of what you’ll find driving through town.
In addition to providing information about you and/or your organization, websites let others interact with you before they have to get in the car and visit — and sometimes the need to visit is eliminated, saving everyone time and money.
The relationship begins here. Websites are increasingly a first impression, and we all know how important that is. Here are some of the helpful things a website can provide:
- contact information (i.e., phone numbers, mailing address, e-mail address)
- driving directions
- hours of operation
- description of services
- connections to related websites
And, those are just the basics!
It’s your virtual calling card and can be as simple or as complex as you choose. As you can see from this overview of Internet usage in the United States, over 68% of the population uses the Web. That’s a lot of clicks, and a really good reason to make sure your organization is on the other end of them.
Spyware Everywhere!
There are actually quite a few names for the things that can infest your computer and render it inoperable, but we just like to call it all “spyware” and get rid of it as quickly as possible.
Since there are so many different types of malicious software, naturally there are many different tools for removing them. We like to start with what costs the least, and for home users we mean $0.00!
Your best chance for survival is to start with Windows XP SP2. From there, the items below (when used together) help to protect your machine. Many websites and downloads transfer harmful things to your computer, and even with the best tools you can still do a lot of damage by visiting questionable sites and running downloaded programs of unknown origin.
Here’s our hotlist of helpful tools:
We’ve noted that older versions of Windows are particularly vulnerable and, if you can, an upgrade to XP is the best way to start fresh with the best chance for survival.

