summaryrefslogtreecommitdiff
path: root/util.xsl
blob: 1c8bb5a8a8e08762b3b83420a38aca19258fb372 (plain)
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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


	<xsl:template match="*" mode="copy-cdata">
		<cdata>
		<xsl:apply-templates />
		</cdata>
	</xsl:template>

	<xsl:template match="*" mode="copy-html">
		<xsl:apply-templates select="*|text()" mode="verb" />
	</xsl:template>

	<!--
	     Copy nodeset as escaped XML
	     http://logit.yudichev.net/2007/11/xslt-output-xml-escaped-copy-of-source.html
	-->
	<xsl:template match="*|@*" mode="verb">
		<xsl:variable name="node-type">
			<xsl:call-template name="node-type"/>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$node-type='element'"> <!-- element -->
				<xsl:text>&lt;</xsl:text>
				<xsl:value-of select="name()"/>
				<xsl:apply-templates select="@*" mode="verb"/>
				<xsl:text>&gt;</xsl:text>
				<xsl:apply-templates mode="verb"/>
				<xsl:text>&lt;/</xsl:text>
				<xsl:value-of select="name()"/>
				<xsl:text>&gt;</xsl:text>
			</xsl:when>
			<xsl:when test="$node-type='text'"> <!-- text -->
				<xsl:value-of select="self::text()"/>
			</xsl:when>
			<xsl:when test="$node-type='attribute'"> <!--any attribute-->
				<xsl:text> </xsl:text>
				<xsl:value-of select="name()"/>
				<xsl:text>="</xsl:text>
				<xsl:value-of select="."/>
				<xsl:text>"</xsl:text>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="node-type">
		<xsl:param name="node" select="."/>
		<xsl:apply-templates mode="nodetype" select="$node"/>
	</xsl:template>
	<xsl:template mode="nodetype" match="*">element</xsl:template>
	<xsl:template mode="nodetype" match="@*">attribute</xsl:template>
	<xsl:template mode="nodetype" match="text()">text</xsl:template>

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

</xsl:stylesheet>