forked from konrad/JATS-to-Mediawiki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialize.xsl
executable file
·121 lines (102 loc) · 4.45 KB
/
serialize.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
<!--
XSLT 1.0 serializer for XML.
Adapted from serializer by Hermann Stamm-Wilbrandt, as described in this thread:
http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/201008/msg00186.html
and downloaded from http://stamm-wilbrandt.de/en/xsl-list/serialize/serialize.xsl.
[CFM] JATS-To-Mediawiki notes:
- Removed stuff not used, doOutput template, namespaces, comments, pi's, etc.
- Changed mode name from "output" to "serialize".
- Change the recursion mechanism. Now, it doesn't recurse with the mode 'serialize';
instead, it applies the default (no mode) template to all the children of an element.
This way, the importing stylesheet must explicitly specify those elements it wants to
serialize.
- The mechanism for serializing text nodes is kept, but I don't think it will ever be
used. From my experiments, it looks like it's unnecessary almost all of the time,
but on the other hand, doesn't do any harm.
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*" mode="serialize">
<xsl:value-of select="concat(' ',name(),'="',.,'"')"/>
</xsl:template>
<!-- Only serialize certain table elements, not everything -->
<xsl:template match="node()" mode="serialize">
<xsl:value-of select="concat('<',name())"/>
<xsl:apply-templates select="@*" mode="serialize"/>
<xsl:choose>
<xsl:when test="*|text()">
<xsl:text>></xsl:text>
<xsl:apply-templates select="*|text()"/>
<xsl:value-of select="concat('</',name(),'>')"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>/></xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()" mode="serialize">
<xsl:call-template name="escapeLtGtAmp">
<xsl:with-param name="str" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="escapeLtGtAmp">
<xsl:param name="str"/>
<xsl:choose>
<xsl:when test="contains($str,'<') or
contains($str,'>') or
contains($str,'&')">
<xsl:variable name="lt"
select="substring-before(concat($str,'<'),'<')"/>
<xsl:variable name="gt"
select="substring-before(concat($str,'>'),'>')"/>
<xsl:variable name="amp"
select="substring-before(concat($str,'&'),'&')"/>
<xsl:choose>
<xsl:when test="string-length($gt) > string-length($amp)">
<xsl:choose>
<xsl:when test="string-length($amp) > string-length($lt)">
<xsl:value-of
select="concat(substring-before($str,'<'),'&lt;')"/>
<xsl:call-template name="escapeLtGtAmp">
<xsl:with-param name="str"
select="substring-after($str,'<')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of
select="concat(substring-before($str,'&'),'&amp;')"/>
<xsl:call-template name="escapeLtGtAmp">
<xsl:with-param name="str"
select="substring-after($str,'&')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="string-length($gt) > string-length($lt)">
<xsl:value-of
select="concat(substring-before($str,'<'),'&lt;')"/>
<xsl:call-template name="escapeLtGtAmp">
<xsl:with-param name="str"
select="substring-after($str,'<')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of
select="concat(substring-before($str,'>'),'&gt;')"/>
<xsl:call-template name="escapeLtGtAmp">
<xsl:with-param name="str"
select="substring-after($str,'>')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>