summaryrefslogtreecommitdiff
path: root/xslt/mal-rng.xsl
blob: d6d88950a1f637affd9873f1065eb9e9ab6c3317 (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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:str="http://exslt.org/strings"
    xmlns:exsl="http://exslt.org/common"
    xmlns:mal="http://projectmallard.org/1.0/"
    xmlns:cache="http://projectmallard.org/cache/1.0/"
    xmlns:rng="http://relaxng.org/ns/structure/1.0"
    xmlns="http://relaxng.org/ns/structure/1.0"
    exclude-result-prefixes="mal cache str exsl rng"
    version="1.0">

<xsl:param name="rng.strict" select="false()"/>
<xsl:param name="rng.strict.allow" select="''"/>

<xsl:template match="/*">
  <xsl:variable name="version">
    <xsl:choose>
      <xsl:when test="string(@version) != ''">
        <xsl:value-of select="@version"/>
      </xsl:when>
      <xsl:when test="/cache:cache">
        <xsl:value-of select="'cache/1.0 1.0'"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'1.0'"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="uris">
    <xsl:for-each select="str:split($version)">
      <xsl:choose>
        <xsl:when test="contains(., ':')">
          <xsl:value-of select="."/>
        </xsl:when>
        <xsl:when test="contains(., '/')">
          <xsl:variable name="ext" select="substring-before(., '/')"/>
          <xsl:variable name="ver" select="substring-after(., '/')"/>
          <xsl:text>http://projectmallard.org/</xsl:text>
          <xsl:value-of select="concat($ext, '/', $ver, '/', $ext, '-', $ver, '.rng')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>http://projectmallard.org/</xsl:text>
          <xsl:value-of select="concat(., '/mallard-', ., '.rng')"/>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text> </xsl:text>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="nss_">
    <xsl:for-each select="str:split($uris)">
      <xsl:for-each select="document(.)/*">
        <xsl:if test="@ns != ''">
          <nsName ns="{@ns}"/>
        </xsl:if>
        <xsl:for-each select="namespace::*">
          <xsl:if test=". != '' and
                        not(local-name(.) = '' and . = 'http://relaxng.org/ns/structure/1.0') and
                        not(local-name(.) = 'xml' and . = 'http://www.w3.org/XML/1998/namespace')
                        ">
            <nsName ns="{.}"/>
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
    <nsName ns=""/>
  </xsl:variable>
  <xsl:variable name="nss" select="exsl:node-set($nss_)/*[not(@ns = preceding-sibling::*/@ns)]"/>
  <grammar>
    <xsl:for-each select="str:split($uris)">
      <xsl:variable name="first" select="position() = 1"/>
      <xsl:for-each select="document(.)/rng:grammar">
        <xsl:apply-templates mode="rng.mode" select="*">
          <xsl:with-param name="first" select="$first"/>
          <xsl:with-param name="ns" select="string(@ns)"/>
          <xsl:with-param name="nss" select="$nss"/>
        </xsl:apply-templates>
      </xsl:for-each>
    </xsl:for-each>
  </grammar>
</xsl:template>

<xsl:template mode="rng.mode" match="text()">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template mode="rng.mode" match="*">
  <xsl:param name="first"/>
  <xsl:param name="ns"/>
  <xsl:param name="nss"/>
  <xsl:choose>
    <xsl:when test="$rng.strict and rng:anyName">
      <xsl:choose>
        <xsl:when test="$rng.strict.allow != ''">
          <xsl:copy>
            <choice>
              <xsl:if test="self::rng:attribute/ancestor::rng:element[1]/rng:anyName">
                <nsName ns=""/>
              </xsl:if>
              <xsl:for-each select="str:split($rng.strict.allow)">
                <nsName ns="{.}"/>
              </xsl:for-each>
              <xsl:if test="ancestor::rng:define/@name = 'mal_attr_external'">
                <nsName ns="http://www.w3.org/XML/1998/namespace"/>
              </xsl:if>
            </choice>
            <xsl:apply-templates mode="rng.mode" select="rng:anyName/following-sibling::*">
              <xsl:with-param name="first" select="$first"/>
              <xsl:with-param name="ns" select="$ns"/>
              <xsl:with-param name="nss" select="$nss"/>
            </xsl:apply-templates>
          </xsl:copy>
        </xsl:when>
        <xsl:when test="ancestor::rng:define/@name = 'mal_attr_external'">
          <xsl:copy>
            <nsName ns="http://www.w3.org/XML/1998/namespace"/>
          </xsl:copy>
        </xsl:when>
        <xsl:otherwise>
          <empty/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="nsmunge" select="self::rng:element or self::rng:attribute"/>
      <xsl:copy>
        <xsl:for-each select="@*">
          <xsl:choose>
            <xsl:when test="$nsmunge and local-name(.) = 'name' and contains(., ':')">
              <xsl:attribute name="name">
                <xsl:value-of select="substring-after(., ':')"/>
              </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
              <xsl:copy-of select="."/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
        <xsl:if test="$nsmunge and not(@ns)">
          <xsl:choose>
            <xsl:when test="contains(@name, ':')">
              <xsl:variable name="prefix" select="substring-before(@name, ':')"/>
              <xsl:attribute name="ns">
                <xsl:value-of select="namespace::*[local-name(.) = $prefix]"/>
              </xsl:attribute>
            </xsl:when>
            <xsl:when test="self::rng:element">
              <xsl:attribute name="ns">
                <xsl:value-of select="$ns"/>
              </xsl:attribute>
            </xsl:when>
          </xsl:choose>
        </xsl:if>
        <xsl:apply-templates mode="rng.mode">
          <xsl:with-param name="first" select="$first"/>
          <xsl:with-param name="ns" select="$ns"/>
          <xsl:with-param name="nss" select="$nss"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template mode="rng.mode" match="rng:start">
  <xsl:param name="first"/>
  <xsl:param name="ns"/>
  <xsl:param name="nss"/>
  <xsl:if test="$first or @combine = 'choice' or @combine = 'interleave'">
    <xsl:copy>
      <xsl:for-each select="@*">
        <xsl:copy-of select="."/>
      </xsl:for-each>
      <xsl:apply-templates mode="rng.mode">
        <xsl:with-param name="first" select="$first"/>
        <xsl:with-param name="ns" select="$ns"/>
        <xsl:with-param name="nss" select="$nss"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:if>
</xsl:template>

<xsl:template mode="rng.mode" match="rng:anyName/rng:except">
  <xsl:param name="first"/>
  <xsl:param name="ns"/>
  <xsl:param name="nss"/>
  <except>
    <xsl:copy-of select="$nss"/>
  </except>
</xsl:template>

</xsl:stylesheet>