aboutsummaryrefslogtreecommitdiff
path: root/xsl/resolver.xsl
blob: 87dc543fbe2cb05f9f24022d11d8be9f2deef206 (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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:dyn="http://exslt.org/dynamic"
 extension-element-prefixes="dyn"
>
 <xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system="http://www.w3.org/TR/html40/strict.dtd" indent="yes"/>
 
 <xsl:include href="identity.xsl"/>
 
 <xsl:param name="sitemapuri"/>
 <xsl:variable name="map" select="document($sitemapuri,document(''))"/>
 <xsl:param name="active"/>
 <xsl:variable name="activenode" select="dyn:evaluate(concat('$map',$active))"/>
 
 <xsl:variable name="lang" select="/html/@xml_lang"/>
 
 <!-- bisogna fare così, perché è un errore aggiungere attributi dopo i nodi figli, quindi bisogna forzare l'ordine. -->
 <xsl:template match="*[@href|@src]">
  <xsl:copy>
   <xsl:apply-templates select="@*"/>
   <xsl:apply-templates select="*|text()"/>
  </xsl:copy>
 </xsl:template>
 
 <xsl:template match="@href|@src">
 
  <xsl:attribute name="{name()}">
 
   <xsl:choose>
    <xsl:when test="$lang and starts-with(string(),'resolve:')">
     <xsl:variable name="sub" select="substring-after(string(),':')"/>
     <xsl:choose>
      <xsl:when test="contains($sub,':')">
       <xsl:call-template name="resolve">
        <xsl:with-param name="ref"><xsl:value-of select="concat('resolve:',substring-after($sub,':'))"/></xsl:with-param>
        <xsl:with-param name="nav" select="$map/sitemap/section[@name=substring-before($sub,':')]"/>
       </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
       <xsl:call-template name="resolve">
        <xsl:with-param name="ref"><xsl:value-of select="."/></xsl:with-param>
        <xsl:with-param name="nav" select="$map/sitemap/section[@name=$lang]"/>
       </xsl:call-template>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
     <xsl:call-template name="resolve">
      <xsl:with-param name="ref"><xsl:value-of select="."/></xsl:with-param>
     </xsl:call-template>
    </xsl:otherwise>
   </xsl:choose>
 
  </xsl:attribute>
 </xsl:template>
 
 <xsl:template name="getpath">
  <xsl:param name="dest"/>
  <xsl:choose>
   <xsl:when test="count($dest|$map/sitemap)=1">
    <xsl:value-of select="$dest/@baseURL"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:call-template name="getpath"><xsl:with-param name="dest" select="$dest/parent::*"/></xsl:call-template><xsl:if test="$dest/@dest"><xsl:value-of select="concat('/',$dest/@dest)"/></xsl:if>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 
 <xsl:template name="resolve">
  <xsl:param name="ref"/>
  <xsl:param name="nav" select="$map/sitemap"/>
  <xsl:choose>
   <xsl:when test="starts-with($ref,'resolve:')">
    <xsl:variable name="rif" select="substring-after($ref,':')"/>
    <xsl:choose>
     <xsl:when test="contains($rif,'#')">
      <xsl:variable name="refname" select="substring-before($rif,'#')"/>
      <xsl:variable name="fragment" select="substring-after($ref,'#')"/>
      <xsl:choose>
       <xsl:when test="$nav//*[@name=$refname]">
        <xsl:call-template name="getpath">
         <xsl:with-param name="dest" select="$nav//*[@name=$refname]"/>
        </xsl:call-template>
        <xsl:value-of select="concat('#',$fragment)"/>
       </xsl:when>
       <xsl:when test="$map/sitemap//*[@name=$refname]">
        <xsl:call-template name="getpath">
         <xsl:with-param name="dest" select="$map/sitemap//*[@name=$refname][1]"/>
        </xsl:call-template>
        <xsl:value-of select="concat('#',$fragment)"/>
       </xsl:when>
       <xsl:otherwise>
        <xsl:text>WRONG REF:</xsl:text><xsl:value-of select="$ref"/>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:when>
     <xsl:otherwise>
      <xsl:choose>
       <xsl:when test="$nav//*[@name=$rif]">
        <xsl:call-template name="getpath">
         <xsl:with-param name="dest" select="$nav//*[@name=$rif]"/>
        </xsl:call-template>
       </xsl:when>
       <xsl:when test="$map/sitemap//*[@name=$rif]">
        <xsl:call-template name="getpath">
         <xsl:with-param name="dest" select="$map/sitemap//*[@name=$rif][1]"/>
        </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
        <xsl:text>WRONG REF:</xsl:text><xsl:value-of select="$ref"/>
       </xsl:otherwise>
      </xsl:choose>       
     </xsl:otherwise>
    </xsl:choose>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$ref"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 
</xsl:stylesheet>