Using the Shorter XSL Syntax Add-In

Environment: VC 6

What Does It Do?

This is an add in for working with XSL and ShoXS. In this age of XML, every second new language is in an XML format, which tends to be very hard to read and requires a lot of keying to conform to the XSL syntax. This code demonstrates how an XML format, in this case XSL, can be converted to another syntax and back by using an add in.

What is ShoXS?

ShoXS is a Shorter XSL Syntax. It is not a new language, just another syntax and this module allows you to toggle between XSL and ShoXS, and back to XSL.

To give you an idea of how it works, when you read in an XSL fragment like this:

  <xsl:choose>
    <xsl:when test='$level=1'>
      <xsl:value-of select="$level + 1"/>
    </xsl:when>
    <xsl:when test='$level=2'>
      <xsl:value-of select="$level * 2"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$level - 2"/>
    </xsl:otherwise>
  </xsl:choose>

the add in allows you to toggle to the ShoXS equivalent, which is:

switch {
  case ($level=1) {
    val($level + 1)
  }
  case($level=2) {
    val($level * 2)
  }
  default {
    val($level - 2)
  }
}

Then, when you want to save it, the add in allows you to toggle back to XSL, so that you can save it and use the XSL with an XSL processor. So, you do not need a special ShoXS (pre-)processor. ShOXS is not only a lot less keying work, but is also a lot better for human consumption.

This is a short conversion table for all mappings defined for ShoXS:

xsl-t syntax ShoXS syntax
xsl:for-each for[](@select) {}
xsl:if if[](@test) {}
xsl:choose switch[] {}
xsl:when case[](@test) {}
xsl:otherwise default[] {}
xsl:template fun[mode & priority atts] @name() {}
xsl:template match[mode & priority atts](@match) {}
xsl:apply-templates apply[non-select atts](@select) {}
xsl:call-template call[] @name {}
xsl:value-of val[](@select)
xsl:variable var[] @name="@select"{}
xsl:param par[] @name="@select"{}
xsl:with-param wpar[] @name="@select"{}
xsl:attribute att @name[] = {}
xsl:text text[] "bla"
<--bla--> /*bla*/ OR //bla

More info on XSL can be found on http://www.w3.org/Style/XSL/ and more info on ShoXS can be found on http://www.xm.co.nz/ShoXS.htm.

Conversion Code

This is the code that is called to convert XSL to ShoX, and it shows how to get the text from the current active window, pass it to the XSL2ShoXSFilter class for conversion to ShoXS, and replace the text in the current window.

STDMETHODIMP CCommands::ConvertXSL2ShoXS()
{
  AFX_MANAGE_STATE(AfxGetStaticModuleState());

  ITextDocument* pTextDocument;
  m_pApplication->get_ActiveDocument(
                (IDispatch **)&pTextDocument);
  if (!pTextDocument) {
    return S_OK;
  }

Okay, we have the text document handle now.

  ITextSelection * pTextSelection;
  pTextDocument->get_Selection(
               (IDispatch **)&pTextSelection);
  if (!pTextSelection) {
    return S_OK;
  }

And that gives us the handle to the selection. Let's get the current line so that we can put the cursor at the same line after conversion.

  long nCurrentLine;
  pTextSelection->get_CurrentLine(& nCurrentLine);

Copy all text from the current window.

  pTextSelection->SelectAll();
  BSTR Text;
  pTextSelection->get_Text( & Text);
  CString sText = (LPCWSTR) Text;
  SysFreeString(Text);

Do the conversion to ShoXS.

  try {
    XSL2ShoXSFilter _XSL2ShoXSFilter;
    sText = _XSL2ShoXSFilter.ConvertStringDefault(sText);
  } catch (CString sMsg)
  {
    AfxMessageBox(sMsg);
    return S_OK;
  } catch (FilterException _FilterException) {
    pTextSelection->GoToLine(_FilterException.m_nLine,
                             _variant_t((long)0));
    AfxMessageBox(_FilterException.m_aMsg);
    return S_OK;
  } catch (...)
  {
    AfxMessageBox("Unknown error trying to toggle");
    return S_OK;
  }

If we got here, no error occurred and we can replace the text in the current window with the ShoXS text and move the cursor to the right line.

  Text = _bstr_t((LPCTSTR) sText);
  pTextSelection->put_Text(Text);
  SysFreeString(Text);
  pTextSelection->GoToLine(nCurrentLine,
                              _variant_t((long)0));
  pTextSelection->Release();
  pTextDocument->Release();

  return S_OK;
} // ConvertXSL2ShoXS

ConvertShoXS2XSL is a similar method to convert ShoXS back to XSL.

The actual parsing and conversion takes place in the classes XSL2ShoXSFilter and ShoXS2XSLFilter.

Installation

  1. Close MSDEV and copy ShoXSaddin.dll to the MSDEV Addins directory (typically located in C:\Program Files\Microsoft Visual Studio\Common\MSDev98\AddIns).
  2. Start MSDEV, choose the Tools/Customize menu, and then select the "Add-ins and Macro Files" tab.
  3. Tick the "ShoXS.DSAddin.1" check box and click the close button.

A toolbar should appear now with two buttons: one for toggling from XSL to ShoXS and one from ShoXS to XSL.

To add short cut keys:

  1. Select the Tools/Customize menu and choose the "Keyboard" tab.
  2. Select the "Add-ins" in the Category combobox.
  3. Select ConvertXSL2ShoXS or ConvertShoXS2XSL to assign short cuts to the ShoXS add in functions.

I found it handy to define short cut keys to work in ShoXS, and then have a little macro do the conversion from ShoXS to XSL, save the file, and convert back to ShoXS. A macro can also be used to switch on C++ or HTML syntax coloring and then switch from XSL to ShoXS.

Downloads

Download demo project - 56 Kb
Download source - 46 Kb


Comments

  • getting error 'Unknown error trying to toggle'...

    Posted by Legacy on 12/06/2002 12:00am

    Originally posted by: Venkat

    Hi,
    This is a nice idea.
    I followed your instructions and installed it.
    I'm running Visual Studio 6.
    When I type-in some xsl text (a copy-paste of your
    code fragment) and click on the toggle button,
    I get the error message box, "Unknown error trying to toggle'.
    What am I doing wrong ?

    Regards,
    -TSV

    Reply
  • I just used XSLT !

    Posted by Legacy on 10/07/2002 12:00am

    Originally posted by: Allan Beihl

    I got frustrated with typing xslt syntax in xml.

    SO I created a xslt that takes a similar syntax as yours and converts it to ( and fro ) xsl.

    This works anywhere, with any browser/app that does xsl, without having to call any classes, objects, whatever ....

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • The first phase of API management was about realizing the business value of APIs. This next wave of API management enables the hyper-connected enterprise to drive and scale their businesses as API models become more complex and sophisticated. Today, real world product launches begin with an API program and strategy in mind. This API-first approach to development will only continue to increase, driven by an increasingly interconnected web of devices, organizations, and people. To support this rapid growth, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds