Project

General

Profile

1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
3
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
4
    <xd:doc xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet">
5
        <xd:desc>
6
            <xd:p><xd:b>Created on:</xd:b> Nov 2, 2011</xd:p>
7
            <xd:p><xd:b>Author:</xd:b> gbodard</xd:p>
8
            <xd:p><xd:b>Author:</xd:b> flawrence</xd:p>
9
            <xd:p><xd:b>Author:</xd:b> rviglianti</xd:p>
10
            <xd:p></xd:p>
11
        </xd:desc>
12
    </xd:doc>
13

    
14
    <xsl:template match="comment()" mode="sqbrackets">
15
        <xsl:sequence select="."/>
16
    </xsl:template>
17
    
18
    <xsl:template match="*" mode="sqbrackets">
19
        <xsl:element name="{name()}">
20
            <xsl:copy-of select="@*"/>
21
            <xsl:apply-templates mode="sqbrackets"/>
22
        </xsl:element>
23
    </xsl:template>
24
    
25
    <xsl:template match="br" mode="sqbrackets">
26
        <xsl:sequence select="."/>
27
    </xsl:template>
28
    
29
    <xsl:template match="text()" mode="sqbrackets">
30
        <xsl:variable name="me" select="."/>
31
        <xsl:variable name="startspace" select="if (matches(substring(.,1,1),'[\n\r\s\t]')) then ' ' else ''"/>
32
        <xsl:variable name="endspace" select="if (matches(substring(.,string-length(.)),'[\n\r\s\t]')) then ' ' else ''"/>
33
        <xsl:variable name="current" select="replace(normalize-space(.), '([^\]])\](\s*)\[([^\[])', '$1$2$3')" />
34
        <xsl:variable name="strlength" select="string-length($current)"/>
35
        <xsl:variable name="firstletter" select="substring($current, 1, 1)"/>
36
        <xsl:variable name="lastletter" select="substring($current, $strlength)"/>
37
        
38
        <xsl:value-of select="$startspace"/>
39
        
40
        <xsl:choose>
41
            <xsl:when test="$firstletter = '[' or $lastletter = ']'">
42
                
43
                <xsl:variable name="previous" select="preceding::text()[1]" />
44
                <xsl:variable name="after" select="following::text()[1]" />
45
                
46
                <!-- get the first text node before the next br (if this node matches current text node then there is a br between this text node and the next text node so don't strip brackets) -->
47
                <xsl:variable name="beforenextbr" select="following::br[1]/preceding::text()[1]"/>
48
                
49
                <!-- get the first next node following the preceding br (if this node matches current text node then there is a br between this text node and the previous text node so don't strip brackets)-->
50
                <xsl:variable name="afterprevbr" select="preceding::br[1]/following::text()[1]"/>
51
                <xsl:variable name="afirstletter" select="substring(normalize-space($after), 1, 1)"/>
52
                <xsl:variable name="plastletter" select="substring(normalize-space($previous), string-length(normalize-space($previous)))"/>
53
                                
54
                <xsl:choose>
55
                    <xsl:when test="$plastletter = ']' and $afirstletter = '[' and $afterprevbr != . and $beforenextbr != .">
56
                        <xsl:value-of select="substring($current, 2, string-length($current)-2)"/>                                                
57
                    </xsl:when>
58
                    <xsl:when test="$plastletter = ']' and $afterprevbr != .">
59
                        <xsl:value-of select="substring($current, 2)"/>
60
                    </xsl:when>
61
                    <xsl:when test="$afirstletter = '[' and $beforenextbr != .">
62
                        <xsl:value-of select="substring($current, 1, string-length($current)-1)"/>
63
                    </xsl:when>
64
                    <xsl:otherwise>
65
                        <xsl:value-of select="$current"/>
66
                    </xsl:otherwise>
67
                </xsl:choose>
68
                
69
            </xsl:when>
70
            <xsl:otherwise>
71
                <xsl:value-of select="$current"/>
72
            </xsl:otherwise>
73
        </xsl:choose>
74
        <xsl:value-of select="$endspace"/>
75
        
76
    </xsl:template>
77
    
78
</xsl:stylesheet>
(36-36/95)