forked from OpenMath/OMSTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverb.xsl
134 lines (115 loc) · 4.29 KB
/
verb.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
>
<!--
Copyright 1999 2000 2001 David Carlisle
Use and distribution of this code are permitted under the terms of the <a
href="http://www.w3.org/Consortium/Legal/copyright-software-19980720"
>W3C Software Notice and License</a>.
-->
<xsl:template match="node()" mode="name">
<xsl:value-of select="local-name(.)"/>
</xsl:template>
<!-- non empty elements and other nodes. -->
<xsl:template mode="verb"
match="*[*]|*[text()]|*[comment()]|*[processing-instruction()]" priority="2">
<xsl:value-of select="'<'"/>
<xsl:apply-templates select="." mode="name"/>
<xsl:for-each select="namespace::*">
<xsl:choose>
<xsl:when test="name()='xml'"/>
<xsl:when test="string(../../namespace::*[name()=name(current())])=concat(.,'G')"/>
<xsl:when test="string(../../namespace::*[name()=name(current())])=."/>
<xsl:when test="name()=''"> xmlns="<xsl:value-of select="."/>"</xsl:when>
<xsl:otherwise> xmlns:<xsl:value-of select="name()"/>="<xsl:value-of select="."/>"</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates mode="verb" select="@*"/>
<xsl:text>></xsl:text>
<xsl:apply-templates mode="verb"/>
<xsl:value-of select="'</'"/>
<xsl:apply-templates select="." mode="name"/>
<xsl:value-of select="'>'"/>
</xsl:template>
<!-- empty elements -->
<xsl:template mode="verb" match="*" priority="1.5">
<xsl:value-of select="'<'"/>
<xsl:apply-templates select="." mode="name"/>
<xsl:apply-templates mode="verb" select="@*"/>
<xsl:text>/></xsl:text>
</xsl:template>
<!-- attributes
Output always surrounds attribute value by "
so we need to make sure no literal " appear in the value -->
<xsl:template mode="verb" match="@*" priority="1.5">
<xsl:if test="(../@*)[string-length(.)>50]">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="concat(' ',name(.),'=')"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="string-replace">
<xsl:with-param name="from" select="'"'"/>
<xsl:with-param name="to" select="'&quot;'"/>
<xsl:with-param name="string" select="."/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>
<!-- pis -->
<xsl:template mode="verb" match="processing-instruction()">
<xsl:text><?</xsl:text>
<xsl:value-of select="."/>
<xsl:text>?></xsl:text>
</xsl:template>
<!-- only works if parser passes on comment nodes -->
<xsl:template mode="verb" match="comment()" priority="2">
<xsl:text><!--</xsl:text>
<xsl:value-of select="."/>
<xsl:text>--></xsl:text>
</xsl:template>
<!-- text elements
need to replace & and < by entity references-->
<xsl:template mode="verb" match="text()" priority="2">
<a name="{generate-id(.)}"/>
<xsl:call-template name="string-replace">
<xsl:with-param name="to" select="'&gt;'"/>
<xsl:with-param name="from" select="'>'"/>
<xsl:with-param name="string">
<xsl:call-template name="string-replace">
<xsl:with-param name="to" select="'&lt;'"/>
<xsl:with-param name="from" select="'<'"/>
<xsl:with-param name="string">
<xsl:call-template name="string-replace">
<xsl:with-param name="to" select="'&amp;'"/>
<xsl:with-param name="from" select="'&'"/>
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<!-- end verb mode -->
<!-- replace all occurences of the character(s) `from'
by the string `to' in the string `string'.-->
<xsl:template name="string-replace" >
<xsl:param name="string"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains($string,$from)">
<xsl:value-of select="substring-before($string,$from)"/>
<xsl:value-of select="$to"/>
<xsl:call-template name="string-replace">
<xsl:with-param name="string" select="substring-after($string,$from)"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>