Replacing NaN in XSLT 1.0

5. March 2011

As I was building a nice little XSLT I was encountering NaN (Not a Number) in my database. Here is the offending code.

<xsl:value-of select="number(string($RepairOrder/VehicleYear))"/>

This code can potentially return the NaN as a value because of the number function.

To solve this I used the test function in XSLT. My finally solution looks like this:

<Year>
   <xsl:choose>
      <xsl:when test="number(string($RepairOrder/VehicleYear)) 
!= number(string($RepairOrder/VehicleYear))
"> <xsl:number/> </xsl:when> <xsl:otherwise> <xsl:value-of select="number(string($RepairOrder/VehicleYear))"/> </xsl:otherwise> </xsl:choose> </Year>

 

Using the test I can determine that the resulting number NaN is not contained within my second express:

<xsl:when test="number(string($RepairOrder/VehicleYear)) 
!= number(string($RepairOrder/VehicleYear))
">

If it’s true I just set it to be the default number. Otherwise I pull the value of the number. To test this you can use the xslt test tool built into Visual Studio 2008/2010.

Wow that's backwards isn't it. Basically comparing NaN != NaN would = true and a Number != NaN would = false. My only conclusion here is that maybe because NaN isn't a comparable entity that saying (NonComparable != NonComparable) = true. Somewhat like trying to compare a null in SQL it isn't a valid comparable.

 

Looking at my example above, it's not very transparent either. So a better way would have been for me to further nest the number into a string and test it against 'NaN' like this:

<Year>
   <xsl:choose>
      <xsl:when test="string(number(string($RepairOrder/VehicleYear)) = 'NaN'">
<xsl:value-of select="number(string($RepairOrder/VehicleYear))"/> </xsl:when>
</xsl:choose>
</Year>

Xml, XSLT

Surround highlighted text with custom strings in Visual Studio 2010

1. September 2010

Oh how lazy I truly is. 

Currently I am doing some heavy manipulation of Xml tags. I ended up with a Excel spreadsheet of new Xml tags I wanted to generate and I wanted to build a Xml document to send off to our QA team for review. MAN was I getting sick of typing "<" and then looking at the text in my excel spreadsheet and typing it in, ensuring I get the case sensitivity correct and then typing ">". At least Visual Studio will generate the closing tag for you. But that was getting very annoying. In comes Macros in Visual Studio. So here is what I did. 

 

  1. Open Macros IDE
    1. Click Tools
    2. Macros
    3. Macros IDE...
  2. Add Module for your Custom Macros
    1. Right click the "MyMacros" in the Project Explorer
    2. Click Add
    3. Add Module
    4. Type in whatever name you want to name this module
    5. Click Add
  3. Program Functions inside your module. 
    1. Public Module CreateXmlTag
    2.     Sub SurroundWithXmlTag()
    3.         DTE.ActiveDocument.Selection.Text = "<" + DTE.ActiveDocument.Selection.Text + ">"
    4.     End Sub
  4. I didn't add the second half of the tags since Visual Studio will auto generate the closing tag. You could do many manipulations here. Like adding <b> for HTML, or whatever you want :)
  5. Save and Close the Macros IDE window
  6. Then I added the macro to a toolbar for convenience
    1. Tools
    2. Customize
    3. Click New
    4. Type in name of Toolbar and click Ok. You will then have a toolbar appear
    5. Click the "Commands" tab
    6. Select the radio button "Toolbar:" (Mine was GenerateXmlTag)
    7. Select "Add Command..."
    8. Under Categories scroll down to "Macros"
    9. Find your Macro
    10. Click Add
  7. Next I wanted to bind it to a keyboard command. 
    1. Select Keyboard...
    2. I filtered by "Xml"
    3. Found my Command 
    4. "Used the new shortcut in:" Xml Editor with Alt+J, Alt+K
  8. Viola!
  9. I can now copy and past my text from Excel

 

OpenDate VoidDate

CloseDate

Highlight and press either my button or my keyboard command and it works like a charm. 

  <OpenDate></OpenDate>
  <VoidDate></VoidDate
  <CloseDate></CloseDate>

P.S. If you don't like the ugly looking text in your toolbar. Visual Studio has a way to add a button image to a command. I personally used Ryan Molden's tool which made things much easier for me. 

http://blogs.msdn.com/b/visualstudio/archive/2010/06/17/command-image-changing-extension.aspx

 

I hoped you enjoyed my lazy post. 

 

 

 

 

 

 

Xml, VS2010, Macros