| 1 |
<?xml version='1.0' encoding='UTF-8'?> |
| 2 |
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> |
| 3 |
|
| 4 |
<!-- |
| 5 |
|
| 6 |
$Revision$ |
| 7 |
|
| 8 |
This stylesheet looks for orth, pron, tr, note and p elements from a TEI XML |
| 9 |
dictionary, which have an opening or closing brace without a matching closing |
| 10 |
or opening counterpart. |
| 11 |
|
| 12 |
It can thus identify some entries which were imported incorrectly from another data |
| 13 |
format. It is similar to the sanity check "unbalanced braces" in FreeDict-Editor, |
| 14 |
but not as comprehensive, since it does not check the correct nesting of different |
| 15 |
brace types. But this disadvantage might be of theoretic nature only. |
| 16 |
|
| 17 |
Since xsltproc and Sablotron don't support xmlns:regexp, we can't just use |
| 18 |
regular expressions here. We could try to use ECMA-Script with |
| 19 |
Sablotron, since that language includes regexp support. |
| 20 |
|
| 21 |
But on the other hand, with much effort the checking can be done with |
| 22 |
substring-before() / substring-after() in plain XSLT/XPath. |
| 23 |
|
| 24 |
Please keep in mind that all kinds of braces should be avoided in TEI XML |
| 25 |
dictionaries. Usually their use is a sign of inappropriate data encoding. |
| 26 |
|
| 27 |
The output of this stylesheet should be a valid TEI XML file. |
| 28 |
|
| 29 |
Michael Bunk, Aug 2005 |
| 30 |
|
| 31 |
--> |
| 32 |
|
| 33 |
<!-- BTW, xsltproc compiled against libxml 20510, libxslt 10032 and libexslt 721 |
| 34 |
does not honour the 'indent' attribute (or my strip/preserve-space understanding lacks |
| 35 |
something. But Sablotron 1.02 _does_ indentation! --> |
| 36 |
<xsl:output method="xml" encoding="UTF-8" indent="yes"/> |
| 37 |
|
| 38 |
<xsl:strip-space elements="body entry form gramGrp sense trans"/> |
| 39 |
|
| 40 |
<xsl:param name="verbose" select="false()"/> |
| 41 |
|
| 42 |
<!-- Generate a FreeDict standard TEI DOCTYPE declaration --> |
| 43 |
<xsl:template match="/"> |
| 44 |
<xsl:text disable-output-escaping="yes"> |
| 45 |
<!DOCTYPE TEI.2 |
| 46 |
PUBLIC "-//TEI P4//DTD Main DTD Driver File//EN" |
| 47 |
"http://www.tei-c.org/P4X/DTD/tei2.dtd" [ |
| 48 |
<!ENTITY % TEI.XML "INCLUDE" > |
| 49 |
<!ENTITY % TEI.dictionaries "INCLUDE" > |
| 50 |
<!ENTITY % TEI.linking "INCLUDE" > |
| 51 |
<!ATTLIST xptr url CDATA #IMPLIED > |
| 52 |
<!ATTLIST xref url CDATA #IMPLIED > |
| 53 |
]> |
| 54 |
</xsl:text> |
| 55 |
<xsl:apply-templates/> |
| 56 |
</xsl:template> |
| 57 |
|
| 58 |
<xsl:template match="entry"> |
| 59 |
<xsl:if test="(position() mod 50) = 0"> |
| 60 |
<xsl:message>Processed <xsl:value-of select="position()"/></xsl:message> |
| 61 |
</xsl:if> |
| 62 |
<!-- This is like a function call --> |
| 63 |
<xsl:variable name="lonely"> |
| 64 |
<xsl:call-template name="check-lonely-nodeset"> |
| 65 |
<xsl:with-param name="nodes" select=".//orth | .//pron | .//tr | .//note | .//p"/> |
| 66 |
</xsl:call-template> |
| 67 |
</xsl:variable> |
| 68 |
|
| 69 |
<xsl:if test="$verbose"> |
| 70 |
<xsl:message>check-lonely-nodeset returned <xsl:value-of select="$lonely"/>. |
| 71 |
</xsl:message> |
| 72 |
</xsl:if> |
| 73 |
|
| 74 |
<!-- If there are lonely braces, copy this entry for output --> |
| 75 |
<xsl:if test="$lonely='true'"> |
| 76 |
<entry> |
| 77 |
<xsl:apply-templates select="@* | *"/> |
| 78 |
</entry> |
| 79 |
</xsl:if> |
| 80 |
</xsl:template> |
| 81 |
|
| 82 |
<!-- This template is a function returning true() or false(), converted into |
| 83 |
a string. It handles all nodes of a nodeset sequentially. --> |
| 84 |
<xsl:template name="check-lonely-nodeset"> |
| 85 |
<xsl:param name="nodes"/> |
| 86 |
|
| 87 |
<xsl:choose> |
| 88 |
<!-- We can't count($nodes) when $nodes is an empty nodeset --> |
| 89 |
<xsl:when test="$nodes = ''">false</xsl:when> |
| 90 |
<xsl:otherwise> |
| 91 |
<xsl:variable name="n" select="count($nodes)"/> |
| 92 |
|
| 93 |
<xsl:if test="$verbose"> |
| 94 |
<xsl:message> check-lonely-nodeset with <xsl:value-of select="$n"/> nodes called.</xsl:message> |
| 95 |
</xsl:if> |
| 96 |
<xsl:choose> |
| 97 |
<!-- Recursively process more than 2 nodes --> |
| 98 |
<xsl:when test="$n>=2"> |
| 99 |
<!-- Check first node, the variable contains either the string 'true' |
| 100 |
or the string 'false' --> |
| 101 |
<xsl:variable name="node1-lonely"> |
| 102 |
<xsl:call-template name="check-lonely-node"> |
| 103 |
<xsl:with-param name="node" select="$nodes[1]"/> |
| 104 |
</xsl:call-template> |
| 105 |
</xsl:variable> |
| 106 |
<!-- Check second node --> |
| 107 |
<xsl:variable name="node2-lonely"> |
| 108 |
<xsl:call-template name="check-lonely-node"> |
| 109 |
<xsl:with-param name="node" select="$nodes[2]"/> |
| 110 |
</xsl:call-template> |
| 111 |
</xsl:variable> |
| 112 |
<!-- Turn strings into booleans and 'or' them --> |
| 113 |
<xsl:variable name="ored" select="$node1-lonely='true' or $node2-lonely='true'"/> |
| 114 |
<xsl:choose> |
| 115 |
<!-- When one of them is lonely, no need to do recursion --> |
| 116 |
<xsl:when test="$ored">true</xsl:when> |
| 117 |
<xsl:otherwise> |
| 118 |
<!-- Do recursion --> |
| 119 |
<xsl:call-template name="check-lonely-nodeset"> |
| 120 |
<xsl:with-param name="nodes" select="$nodes[position()>2]"/> |
| 121 |
</xsl:call-template> |
| 122 |
</xsl:otherwise> |
| 123 |
</xsl:choose> |
| 124 |
</xsl:when> |
| 125 |
<!-- Base case: Only one node to process --> |
| 126 |
<xsl:otherwise> |
| 127 |
<xsl:call-template name="check-lonely-node"> |
| 128 |
<xsl:with-param name="node" select="$nodes[1]"/> |
| 129 |
</xsl:call-template> |
| 130 |
</xsl:otherwise> |
| 131 |
</xsl:choose> |
| 132 |
</xsl:otherwise> |
| 133 |
</xsl:choose> |
| 134 |
</xsl:template> |
| 135 |
|
| 136 |
<!-- This template is a function taking a string in $node as argument |
| 137 |
returning 'true' if a lonely brace is seen or 'false' otherwise. Templates |
| 138 |
can only return strings (and result tree fragments), watch out! It checks |
| 139 |
the string value of $node for lonelinesses of different brace types. --> |
| 140 |
<xsl:template name="check-lonely-node"> |
| 141 |
<xsl:param name="node"/> |
| 142 |
|
| 143 |
<xsl:variable name="type1result"> |
| 144 |
<xsl:call-template name="check-lonely-node-for-bracetype"> |
| 145 |
<xsl:with-param name="node" select="$node"/> |
| 146 |
<xsl:with-param name="co" select="'('"/> |
| 147 |
<xsl:with-param name="cc" select="')'"/> |
| 148 |
</xsl:call-template> |
| 149 |
</xsl:variable> |
| 150 |
<xsl:choose> |
| 151 |
<xsl:when test="$type1result = 'true'">true</xsl:when> |
| 152 |
<xsl:otherwise> |
| 153 |
<xsl:variable name="type2result"> |
| 154 |
<xsl:call-template name="check-lonely-node-for-bracetype"> |
| 155 |
<xsl:with-param name="node" select="$node"/> |
| 156 |
<xsl:with-param name="co" select="'{'"/> |
| 157 |
<xsl:with-param name="cc" select="'}'"/> |
| 158 |
</xsl:call-template> |
| 159 |
</xsl:variable> |
| 160 |
<xsl:choose> |
| 161 |
<xsl:when test="$type2result = 'true'">true</xsl:when> |
| 162 |
<xsl:otherwise> |
| 163 |
<xsl:variable name="type3result"> |
| 164 |
<xsl:call-template name="check-lonely-node-for-bracetype"> |
| 165 |
<xsl:with-param name="node" select="$node"/> |
| 166 |
<xsl:with-param name="co" select="'['"/> |
| 167 |
<xsl:with-param name="cc" select="']'"/> |
| 168 |
</xsl:call-template> |
| 169 |
</xsl:variable> |
| 170 |
<xsl:choose> |
| 171 |
<xsl:when test="$type3result = 'true'">true</xsl:when> |
| 172 |
<xsl:otherwise> |
| 173 |
<xsl:variable name="type4result"> |
| 174 |
<xsl:call-template name="check-lonely-node-for-bracetype"> |
| 175 |
<xsl:with-param name="node" select="$node"/> |
| 176 |
<xsl:with-param name="co" select="'<'"/> |
| 177 |
<xsl:with-param name="cc" select="'>'"/> |
| 178 |
</xsl:call-template> |
| 179 |
</xsl:variable> |
| 180 |
<xsl:choose> |
| 181 |
<xsl:when test="$type4result = 'true'">true</xsl:when> |
| 182 |
<xsl:otherwise>false</xsl:otherwise> |
| 183 |
</xsl:choose> |
| 184 |
</xsl:otherwise> |
| 185 |
</xsl:choose> |
| 186 |
</xsl:otherwise> |
| 187 |
</xsl:choose> |
| 188 |
</xsl:otherwise> |
| 189 |
</xsl:choose> |
| 190 |
</xsl:template> |
| 191 |
|
| 192 |
<!-- This template is a function taking a string in $node as argument |
| 193 |
returning 'true' if a lonely brace is seen and 'false' otherwise. --> |
| 194 |
<xsl:template name="check-lonely-node-for-bracetype"> |
| 195 |
<xsl:param name="node"/> |
| 196 |
<xsl:param name="open-braces" select="0"/> |
| 197 |
<!-- The charactes that should be treated as corresponding opening/closing |
| 198 |
braces --> |
| 199 |
<xsl:param name="co" select="'('"/> |
| 200 |
<xsl:param name="cc" select="')'"/> |
| 201 |
|
| 202 |
<xsl:if test="$verbose"> |
| 203 |
<xsl:message> check-lonely-node-for-bracetype with node='<xsl:value-of select="$node"/>' and |
| 204 |
open-braces=<xsl:value-of select="$open-braces"/> char-open='<xsl:value-of select="$co"/>' called.</xsl:message> |
| 205 |
</xsl:if> |
| 206 |
<xsl:variable name="pos-opening-brace" select="string-length(substring-before($node, $co))"/> |
| 207 |
<xsl:variable name="pos-closing-brace" select="string-length(substring-before($node, $cc))"/> |
| 208 |
<xsl:choose> |
| 209 |
<!-- There are no braces --> |
| 210 |
<xsl:when test="not(contains($node, $co)) and not(contains($node, $cc))">false</xsl:when> |
| 211 |
<!-- Only a closing brace --> |
| 212 |
<xsl:when test="not(contains($node, $co))"> |
| 213 |
<xsl:choose> |
| 214 |
<!-- No corresponding opening brace seen --> |
| 215 |
<xsl:when test="$open-braces = 0">true</xsl:when> |
| 216 |
<xsl:otherwise> |
| 217 |
<!-- Recursively check remaining string --> |
| 218 |
<xsl:call-template name="check-lonely-node-for-bracetype"> |
| 219 |
<xsl:with-param name="open-braces" select="$open-braces - 1"/> |
| 220 |
<xsl:with-param name="node" select="substring-after($node, $cc)"/> |
| 221 |
<xsl:with-param name="co" select="$co"/> |
| 222 |
<xsl:with-param name="cc" select="$cc"/> |
| 223 |
</xsl:call-template> |
| 224 |
</xsl:otherwise> |
| 225 |
</xsl:choose> |
| 226 |
</xsl:when> |
| 227 |
<!-- Only an opening brace --> |
| 228 |
<xsl:when test="not(contains($node, $cc))">true</xsl:when> |
| 229 |
<!-- Both appear, closing brace comes first --> |
| 230 |
<xsl:when test="$pos-opening-brace > $pos-closing-brace"> |
| 231 |
<xsl:choose> |
| 232 |
<!-- No corresponding opening brace seen --> |
| 233 |
<xsl:when test="$open-braces = 0">true</xsl:when> |
| 234 |
<xsl:otherwise> |
| 235 |
<!-- Recursively check remaining string --> |
| 236 |
<xsl:call-template name="check-lonely-node-for-bracetype"> |
| 237 |
<xsl:with-param name="open-braces" select="$open-braces - 1"/> |
| 238 |
<xsl:with-param name="node" select="substring-after($node, $cc)"/> |
| 239 |
<xsl:with-param name="co" select="$co"/> |
| 240 |
<xsl:with-param name="cc" select="$cc"/> |
| 241 |
</xsl:call-template> |
| 242 |
</xsl:otherwise> |
| 243 |
</xsl:choose> |
| 244 |
</xsl:when> |
| 245 |
<!-- Both appear, opening brace comes first --> |
| 246 |
<xsl:otherwise> |
| 247 |
<!-- Recursively check remaining string --> |
| 248 |
<xsl:call-template name="check-lonely-node-for-bracetype"> |
| 249 |
<xsl:with-param name="open-braces" select="$open-braces + 1"/> |
| 250 |
<xsl:with-param name="node" select="substring-after($node, $co)"/> |
| 251 |
<xsl:with-param name="co" select="$co"/> |
| 252 |
<xsl:with-param name="cc" select="$cc"/> |
| 253 |
</xsl:call-template> |
| 254 |
</xsl:otherwise> |
| 255 |
</xsl:choose> |
| 256 |
</xsl:template> |
| 257 |
|
| 258 |
<!-- If no other template matches, copy the encountered attributes and elements --> |
| 259 |
<xsl:template match='@* | node()'> |
| 260 |
<xsl:copy><xsl:apply-templates select='@* | node()'/></xsl:copy> |
| 261 |
</xsl:template> |
| 262 |
|
| 263 |
</xsl:stylesheet> |
| 264 |
|