-
Notifications
You must be signed in to change notification settings - Fork 0
/
expandPrefixes.xsl
81 lines (75 loc) · 2.6 KB
/
expandPrefixes.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
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs h"
xmlns:h="http://www.w3.org/1999/xhtml" xmlns:t="http://www.tei-c.org/ns/1.0"
xmlns="http://www.tei-c.org/ns/1.0" version="2.0">
<!-- expand prefixes on @ref attributes or ref/@targets
using mappings from <prefixDef> elements
read from the file ../encodingDesc.xml
-->
<xsl:template match="@ref">
<xsl:attribute name="ref">
<xsl:for-each select="tokenize(., '\s+')">
<xsl:choose>
<xsl:when test="starts-with(., 'http')">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="t:expandLink(.)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:attribute>
</xsl:template>
<xsl:template match="t:ref/@target">
<xsl:attribute name="target">
<xsl:for-each select="tokenize(., '\s+')">
<xsl:choose>
<xsl:when test="starts-with(., 'http')">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="t:expandLink(.)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:attribute>
</xsl:template>
<xsl:template match="* | @* | processing-instruction()">
<xsl:copy>
<xsl:apply-templates select="* | @* | processing-instruction() | comment() | text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
<!-- could normalize() here -->
</xsl:template>
<xsl:function name="t:expandLink" as="xs:string">
<xsl:param name="target"/>
<xsl:analyze-string select="$target" regex="(\S+):(\S+)">
<xsl:matching-substring>
<xsl:variable name="prefix" select="regex-group(1)"/>
<xsl:variable name="value" select="regex-group(2)"/>
<xsl:choose>
<xsl:when test="document('encodingDesc.xml')//t:prefixDef[@ident = $prefix]">
<xsl:for-each select="document('encodingDesc.xml')//t:prefixDef[@ident = $prefix]">
<xsl:sequence select="replace($value, @matchPattern, @replacementPattern)"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:message><xsl:value-of select="$prefix"/> not found in encodingDesc.xml</xsl:message>
<xsl:sequence select="regex-group(0)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:function>
</xsl:stylesheet>