Friday, April 29, 2005

XSLT Analog to sysouts

I'm on a high. I've been stuck with this problem of trying to understand a HUGE xslt that operates on an even bigger XML. I sorely needed something that will let me trace through the xslt execution to understand the flow.

Tried a couple of IDEs - Stylus Studio (free edition) and Marrowsoft XSelerator. Stylus studio did a graceful exit, Xselerator went purple in the face and died a gruesome death :-(

Hmm... so after sometime I was wondering if I could annotate the XSL output with information on the templates matched it would atleast help partway. I was thinking of perl/C#/regular expressions and then suddenly the penny dropped "for each xsl:template node, include a comment with the template match/mode" - Hang on!!! looks like that sounds like a job for XSLT....

Anyway, there are a couple of quirks - the first one you hit will be when you try to output a template like this


<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="xsl:stylesheet" >
<!-- generate an output xsl:stylesheet node -->
<xsl:stylesheet></xsl:stylesheet>
</xsl:template>
</xsl:stylesheet>


Oops! The XSLT processor cribs (and with good reason too)! It doesn't know which xsl:template is for the current stylesheet vs which is intended to be output to the result document.There are a couple of approaches around this. One is to use xsl:element like this

    <xml:namespace prefix = xsl />
    <xsl:element name="xsl:template">
    </xsl:element>
  

But this results in enormously wordy documents. Thankfully there's a neater way out. You use something called . Basically, what it does is that it allows you to use a dummy namespace in your xslt. You set up the dummy namespace (let's say genxsl) to map to a real namespace in the result document (xsl). Then you basically use the dummy namespace in your XSLT. However, when generating output, the processor will replace all references to the dummy namespace in the result document with references to the real namespace. For ex.


<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:gen="http://www.w3.org/1999/XSL/Transform/2">
    <xsl:namespace-alias stylesheet-prefix="gen" result-prefix="xsl"/>
    <xsl:template match="xsl:stylesheet">
        <gen:stylesheet>
            <xsl:for-each select="@*">
                <xsl:attribute name="{name(.)}">
                    <xsl:value-of select="."/>
                </xsl:attribute>
            </xsl:for-each>
            <xsl:apply-templates></xsl:apply-templates>
            <xsl:if test="not(xsl:template[@name='pseudo-xpath-to-current-node'])" &gt;
                <xsl:text>
                    &amp;#10;&amp;#10;
                </xsl:text> <xsl:copy-of select="document('')/xsl:stylesheet/xsl:template[ @name='pseudo-xpath-to-current-node']"/>
                <xsl:text>
                    &amp;#10;&amp;#10;
                </xsl:text>
            </xsl:if>
        </gen:stylesheet>
    </xsl:template>
</xsl:stylesheet>



Note the usage of xsl:namespace-alias and the code for generating an xsl:stylesheet element in the result document.

I've included my efforts here - along with a simple books.xml, a books.xsl which generates a table and finally an instrument.xsl that instruments books.xsl to generate an instrumented version. Transforming books.xml with the instrumented xslt generates output that annotated with custom nodes that highlight which template got called when.

After I was mostly done with the code, I came across an article in IBM developerWorks which discusses the same topic. Rather than cover the same material again, you can find the article here. Stuff that's different is that I generate custom nodes (which I thought would be useful to view in XML IDE which allow a hierarchical display). I've also shamelessly borrowed the code to generate the Xpath of the node (part of what you see in the snippet).


I'm on a high. I've been stuck with this problem o...

I'm on a high. I've been stuck with this problem of trying to understand a HUGE xslt that operates on an even bigger XML. I sorely needed something that will let me trace through the xslt execution to understand the flow.

Tried a couple of IDEs - Stylus Studio (free edition) and Marrowsoft XSelerator. Stylus studio did a graceful exit, Xselerator went purple in the face and died a gruesome death :-(

Hmm... so after sometime I was wondering if I could annotate the XSL output with information on the templates matched it would atleast help partway. I was thinking of perl/C#/regular expressions and then suddenly the penny dropped "for each xsl:template node, include a comment with the template match/mode" - Hang on!!! looks like that sounds like a job for XSLT....

Anyway, there are a couple of quirks - the first one you hit will be when you try to output a template like this
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="xsl:stylesheet" >

	<!-- generate an output xsl:stylesheet node -->

		<xsl:stylesheet>

		</xsl:stylesheet>

	</xsl:template>

</xsl:stylesheet>

Oops! The XSLT processor cribs (and with good reason too)! It doesn't know which xsl:template is for the current stylesheet vs which is intended to be output to the result document.There are a couple of approaches around this. One is to use xsl:element like this
    <xml:namespace prefix = xsl />

	<xsl:element name="xsl:template"></xsl:element>

But this results in enormously wordy documents. Thankfully there's a neater way out. You use something called . Basically, what it does is that it allows you to use a dummy namespace in your xslt. You set up the dummy namespace (let's say genxsl) to map to a real namespace in the result document (xsl). Then you basically use the dummy namespace in your XSLT. However, when generating output, the processor will replace all references to the dummy namespace in the result document with references to the real namespace. For ex.
<xsl:stylesheet version="1.0"

	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

	xmlns:gen="http://www.w3.org/1999/XSL/Transform/2">

<xsl:namespace-alias stylesheet-prefix="gen" result-prefix="xsl"/>

<xsl:template match="xsl:stylesheet">

	<gen:stylesheet>

		<xsl:for-each select="@*">

		<xsl:attribute name="{name(.)}">

		<xsl:value-of select="."/>

		</xsl:attribute>

	</xsl:for-each>

	<xsl:apply-templates></xsl:apply-templates>

	<xsl:if test="not(xsl:template[@name='pseudo-xpath-to-current-node'])" >

	<xsl:text></xsl:text>

	<xsl:copy-of select="document('')/xsl:stylesheet/xsl:template[ @name='pseudo-xpath-to-current-node']"/>

	<xsl:text></xsl:text>

	</xsl:if>

</gen:stylesheet>

</xsl:template>

Note the usage of xsl:namespace-alias and the code for generating an xsl:stylesheet element in the result document.

I've included my efforts here - along with a simple books.xml, a books.xsl which generates a table and finally an instrument.xsl that instruments books.xsl to generate an instrumented version. Transforming books.xml with the instrumented xslt generates output that annotated with custom nodes that highlight which template got called when.

After I was mostly done with the code, I came across an article in IBM developerWorks which discusses the same topic. Rather than cover the same material again, you can find the article here. Stuff that's different is that I generate custom nodes (which I thought would be useful to view in XML IDE which allow a hierarchical display). I've also shamelessly borrowed the code to generate the Xpath of the node (part of what you see in the snippet).

Thursday, April 14, 2005


I love the premise of Test Driven Development - I've even used it a few times in the line of duty ;-) (I do have to admit, I've been naughty and left out the step of  seeing the test case fail a few times )...Anyway, I end up working on web applications more often than not and while you can use TDD for your class libraries, a web app is a totally different animal. The fact that you can use TDD for class libraries makes the whole thing even more frustrating - you have a bit that works for sure (the class libraries with their tests) and then you hit this piece (aspx) on which you dont have the same level of confidence.


I've been working around it making sure that pages generate nice logs, so that during development, whenever I find a sticky piece, I put in an additional log statement. This thing works but at best is a poor cousin to automated testing ala nunit.


Enter NunitAsp - it promises to do for web applications what nunit does to class libraries - pretty stiff goal indeed! I've looked at this piece about an year ago for a similar project but had to decide against its use after going through the feature list. As a result, though I understand the aims, I havent got my hands into it. These days' I'm planning to do a dive-deep into it - just to make a more diligent evaluation if it'll actually work.


I love the premise of Test Driven Development -...


I love the premise of Test Driven Development - I've even used it a few times in the line of duty ;-) (I do have to admit, I've been naughty and left out the step of  seeing the test case fail a few times )...Anyway, I end up working on web applications more often than not and while you can use TDD for your class libraries, a web app is a totally different animal. The fact that you can use TDD for class libraries makes the whole thing even more frustrating - you have a bit that works for sure (the class libraries with their tests) and then you hit this piece (aspx) on which you dont have the same level of confidence.


I've been working around it making sure that pages generate nice logs, so that during development, whenever I find a sticky piece, I put in an additional log statement. This thing works but at best is a poor cousin to automated testing ala nunit.


Enter NunitAsp - it promises to do for web applications what nunit does to class libraries - pretty stiff goal indeed! I've looked at this piece about an year ago for a similar project but had to decide against its use after going through the feature list. As a result, though I understand the aims, I havent got my hands into it. These days' I'm planning to do a dive-deep into it - just to make a more diligent evaluation if it'll actually work.


Monday, April 11, 2005


For the most part, I find ASP.NET far more easier to use than Java. But the ONE BIG THING where I've found ASP.NET sorely lacking is in the support for page templates.


Page templates, if you need to brush up, allow you to define common layout and contents for a web site. Furthermore, once defined, its easy to change the layout and or move your default items around the place.


Basically, what you need is to be able to define a template page with the different areas (header, left pane, main content, footer etc). So the template page controls what is shown where. In addition, you also define the default content for all these areas.


Now each page in the application just overrides the content for the main area (assuming that the defaults are fine for the rest of it). WOW!!!


Java's had this quite some time - Jakarta Struts has something called Tiles which does exactly this.


For .NET, as I mentioned, the need's going to be fulfilled with v2.0 of ASP.NET. Meanwhile, if you feel the idea's great and there's no point in waiting for v2.0, release, do take a look at MasterPage as www.asp.net control gallery. Do note that since the asp.net team has released this control, there's a good chance that most of the features will end up in asp.net 2.0.


There are a few shortcomings of the control though - you'll get a hang of them if you read the posts. Paul Wilson has a version which overcomes these - and best of all, he releases the control with source :). You can find it here.


For the most part, I find ASP.NET far more easi...


For the most part, I find ASP.NET far more easier to use than Java. But the ONE BIG THING where I've found ASP.NET sorely lacking is in the support for page templates.


Page templates, if you need to brush up, allow you to define common layout and contents for a web site. Furthermore, once defined, its easy to change the layout and or move your default items around the place.


Basically, what you need is to be able to define a template page with the different areas (header, left pane, main content, footer etc). So the template page controls what is shown where. In addition, you also define the default content for all these areas.


Now each page in the application just overrides the content for the main area (assuming that the defaults are fine for the rest of it). WOW!!!


Java's had this quite some time - Jakarta Struts has something called Tiles which does exactly this.


For .NET, as I mentioned, the need's going to be fulfilled with v2.0 of ASP.NET. Meanwhile, if you feel the idea's great and there's no point in waiting for v2.0, release, do take a look at MasterPage as www.asp.net control gallery. Do note that since the asp.net team has released this control, there's a good chance that most of the features will end up in asp.net 2.0.


There are a few shortcomings of the control though - you'll get a hang of them if you read the posts. Paul Wilson has a version which overcomes these - and best of all, he releases the control with source :). You can find it here.


Friday, April 08, 2005


The ASP.NET validation summary is great for displaying all the errors in the page. It would be nice though to be able to use a validation summary to display errors that occur on the server side.


A typical scenario is when you have a page that does a search and then displays the results of the search. For the case where no results are found, it would be nice to be able to display the results in our custom validation summary control. It takes away the need to handle the display of the error message in a validation summary control.


This is what you can do about it - implement the IValidator class




public
class CustomErrorMessage:IValidator
    {
        privatestring message;
        public CustomErrorMessage()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region IValidator Members

        publicvoid Validate()
        {
            // TODO: Add CustomErrorMessage.Validate implementation
        }

        publicbool IsValid
        {
            get
            {
                returnfalse;
            }
            set
            {
                
            }
        }

        publicstring ErrorMessage
        {
            get
            {
                
                return message;
            }
            set
            {
                this.message = value;
            }
        }

        #endregion
    }


And here's code to use the validator at runtime in response to a server side error.



CustomErrorMessage msg = new CustomErrorMessage();
msg.ErrorMessage = "No rows found";
ServerErrors.Visible =true;
ValidationSummary1.Visible = false;
Page.Validators.Add(msg);

Page.Validate();



The ASP.NET validation summary is great for displaying all the errors in the page. It would be nice though to be able to use a validation summary to display errors that occur on the server side.


A typical scenario is when you have a page that does a search and then displays the results of the search. For the case where no results are found, it would be nice to be able to display the results in our custom validation summary control. It takes away the need to handle the display of the error message in a validation summary control.


This is what you can do about it - implement the IValidator class




public
class CustomErrorMessage:IValidator
    {
        privatestring message;
        public CustomErrorMessage()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region IValidator Members

        publicvoid Validate()
        {
            // TODO: Add CustomErrorMessage.Validate implementation
        }

        publicbool IsValid
        {
            get
            {
                returnfalse;
            }
            set
            {
                
            }
        }

        publicstring ErrorMessage
        {
            get
            {
                
                return message;
            }
            set
            {
                this.message = value;
            }
        }

        #endregion
    }



Been tinkering with getting a nice paging algorithm out. To get a basic hang of the problem, do take a look at





Many ASP developers create their own search engines that return back the results to the web browser one page at a time



Do take a look at the second query given. I've modified it a little bit so that you can sort by a given field and removed a bit of the cruft (the au_lname like '%A%' bit). Here the table used is called Pager - with a column called Name.


Some of my bare bones requirements for a paging system are:


1. Should allow sorting


2. Should not impose any requirements on the table schema/ resultset.


3. Should be done on SQL Server as much as possible. Definitely not default paging that results in all rows being sent to the middle layer.


4. Ideally, should not require dynamic queries. (Though note that this conflicts with 1 & 3 as these two requirements almost make dynamic queries mandatory).


5. Should not use temp tables.


declare @pagenum int
declare @pageSize int


set rowcount  @pagesize


select * 
   from Pager P
where
    (select count(*)
    from Pager P2
    where P2.Name <= P.name) > @pagesize * @pagenum
order by
       p.name


 


The ASP.NET validation summary is great for dis...


The ASP.NET validation summary is great for displaying all the errors in the page. It would be nice though to be able to use a validation summary to display errors that occur on the server side.


A typical scenario is when you have a page that does a search and then displays the results of the search. For the case where no results are found, it would be nice to be able to display the results in our custom validation summary control. It takes away the need to handle the display of the error message in a validation summary control.


This is what you can do about it - implement the IValidator class




public
class CustomErrorMessage:IValidator
    {
        privatestring message;
        public CustomErrorMessage()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region IValidator Members

        publicvoid Validate()
        {
            // TODO: Add CustomErrorMessage.Validate implementation
        }

        publicbool IsValid
        {
            get
            {
                returnfalse;
            }
            set
            {
                
            }
        }

        publicstring ErrorMessage
        {
            get
            {
                
                return message;
            }
            set
            {
                this.message = value;
            }
        }

        #endregion
    }


And here's code to use the validator at runtime in response to a server side error.



CustomErrorMessage msg = new CustomErrorMessage();
msg.ErrorMessage = "No rows found";
ServerErrors.Visible =true;
ValidationSummary1.Visible = false;
Page.Validators.Add(msg);

Page.Validate();


The ASP.NET validation summary is great for dis...


The ASP.NET validation summary is great for displaying all the errors in the page. It would be nice though to be able to use a validation summary to display errors that occur on the server side.


A typical scenario is when you have a page that does a search and then displays the results of the search. For the case where no results are found, it would be nice to be able to display the results in our custom validation summary control. It takes away the need to handle the display of the error message in a validation summary control.


This is what you can do about it - implement the IValidator class




public
class CustomErrorMessage:IValidator
    {
        privatestring message;
        public CustomErrorMessage()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region IValidator Members

        publicvoid Validate()
        {
            // TODO: Add CustomErrorMessage.Validate implementation
        }

        publicbool IsValid
        {
            get
            {
                returnfalse;
            }
            set
            {
                
            }
        }

        publicstring ErrorMessage
        {
            get
            {
                
                return message;
            }
            set
            {
                this.message = value;
            }
        }

        #endregion
    }


Been tinkering with getting a nice paging algor...


Been tinkering with getting a nice paging algorithm out. To get a basic hang of the problem, do take a look at





Many ASP developers create their own search engines that return back the results to the web browser one page at a time



Do take a look at the second query given. I've modified it a little bit so that you can sort by a given field and removed a bit of the cruft (the au_lname like '%A%' bit). Here the table used is called Pager - with a column called Name.


Some of my bare bones requirements for a paging system are:


1. Should allow sorting


2. Should not impose any requirements on the table schema/ resultset.


3. Should be done on SQL Server as much as possible. Definitely not default paging that results in all rows being sent to the middle layer.


4. Ideally, should not require dynamic queries. (Though note that this conflicts with 1 & 3 as these two requirements almost make dynamic queries mandatory).


5. Should not use temp tables.


declare @pagenum int
declare @pageSize int


set rowcount  @pagesize


select * 
   from Pager P
where
    (select count(*)
    from Pager P2
    where P2.Name <= P.name) > @pagesize * @pagenum
order by
       p.name


Â