Search

Dark theme | Light theme

November 21, 2008

Copy template for XSLT in NetBeans

Cocoon has a great way to split up big XSL stylesheet into multiple little stylesheets. With the Cocoon pipeline we can go from one stylesheet to the other. This results in better maintainable stylesheets. Because one stylesheet is executed after another in a pipeline we want to pass around the XML. For example one stylesheet will handle a product element from an order XML and we want to give the rest of the order XML to the next stylesheet. The code to do this is easy:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

Because we need this code so much it is easy to make a code template for it in NetBeans. We go to Tools | Options | Editor | Code Templates and select XML as the language. We click on the New button to create a new code template. NetBeans shows a dialog window with an input field for the abbreviation we want to use. We type ct and click the OK button. We now see our abbreviation is added and we can type our code in the Expanded text tab. We type

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>
We can add a short description in the Description tab. Let's type Copy template for XSLT. Finally at the bottom we see Expand Template on. Here we can define which character NetBeans will react to after we type ct in an XSL stylesheet to expand the template code. Let's leave it to the Tab character.

Now when we return to the NetBeans code editor and we are working on a XSL stylesheet, we can type ct, press the Tab key and we get the complete code inserted into our stylesheet.