Monday, October 18, 2010

Removing line breaks in XSLT

I needed to remove the line breaks from my transform output because they were interfering with a 3rd party transform engine. (I could not use normalize-space(.) because I needed to preserve the spacing around mixed content.)

Instead of finding what the carriage return code is (I believe its #xD). I just created the variable $linebreak with the value of an actual linebreak.

The original code:
    <xsl:template match="para/text()">
<xsl:value-of select="."/>
</xsl:template>


Updated to remove line breaks:

<xsl:template match="para/text()">
<xsl:call-template name="selectWithoutBreaks"/>
</xsl:template>


Utilizes these templates:

<xsl:template name="selectWithoutBreaks" >

<!-- NOTE: The whitespace between the xsl:text elements is important because it represents the line break -->
<xsl:variable name="linebreak">
<xsl:text>
</xsl:text>
</xsl:variable>

<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="replace" select="$linebreak" />
<xsl:with-param name="with" select="''"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


This is all possible in XSLT 1.0. If you are using XSLT 2.0 you can use the replace function instead of creating the replace-string template above.

Monday, October 4, 2010

Great tool for comparing multiple files

I had applied some "hotfixes" to some of our XSLT production code without ever building a new installer. This caused the files in production to get out of sync with what I had in my development environment and in Source safe.

I needed a way to compare multiple files at once because it would of taken forever to go one by one, and I did not want to just guess based on the modified date.

Luckily I found a free tool called KDiff3. I highly recommend this tool, it works just as expected and can potentially save you a lot of time when troubleshooting.




Download it here: http://kdiff3.sourceforge.net/

Does anyone have experience with any other compare tools? After trying KDiff3 I did not bother looking any further because the tool worked so well.