aboutsummaryrefslogtreecommitdiff
path: root/compile.xsl
blob: 656ee94805fb59f46aa6748f94e47b074f3a9180 (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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 
 <xsl:template match="/sitemap">
# Makefile generato automaticamente via XSLT
 
all: Makefile <xsl:for-each select=".//main|.//page" xml:space="preserve"><xsl:call-template name="abs"><xsl:with-param name="attr" select="'dest'"/></xsl:call-template> </xsl:for-each>
 
Makefile: sitemap.xml compile.xsl
xsltproc -o Makefile compile.xsl sitemap.xml
 
<xsl:for-each select="type">
<xsl:apply-templates select="."/>
</xsl:for-each>
 
<xsl:for-each select=".//main|.//page" xml:space="preserve">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:template>
 
 <xsl:template match="main|page">
<xsl:call-template name="abs"><xsl:with-param name="attr" select="'dest'"/></xsl:call-template> : _ACTIVE_ELEM='<xsl:call-template name="xpathfor"/>'
<xsl:call-template name="abs"><xsl:with-param name="attr" select="'dest'"/></xsl:call-template> : <xsl:call-template name="abs"><xsl:with-param name="attr" select="'src'"/></xsl:call-template> $(_<xsl:value-of select="@type"/>_PREQ)
$(_<xsl:value-of select="@type"/>_CMD)
</xsl:template>
 
<!-- chiamato con ctx-node un elemento main|page, restituisce un espressione XPath che lo individua -->
 <xsl:template name="xpathfor">
  <xsl:param name="cnode" select="."/>
  <xsl:choose>
   <xsl:when test="$cnode = /sitemap">/sitemap</xsl:when>
   <xsl:otherwise><xsl:call-template name="xpathfor"><xsl:with-param name="cnode" select="$cnode/parent::node()"/></xsl:call-template>/<xsl:value-of select="name($cnode)"/>[@name="<xsl:value-of select="$cnode/@name"/>"]</xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 
<!-- chiamato con ctx-node un elemento main|page, deve restituire il percorso completo della pagina relativo alla sitemap, ottenuto concatenando gli attributi @src o @dest degli elementi padre  -->
 <xsl:template name="abs">
  <xsl:param name="cnode" select="."/>
  <xsl:param name="attr"/>
  <xsl:choose>
   <xsl:when test="$cnode = /sitemap">
    <xsl:value-of select="$cnode/attribute::*[name()=$attr]"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:variable name="prev"><xsl:call-template name="abs"><xsl:with-param name="cnode" select="$cnode/parent::node()"/><xsl:with-param name="attr" select="$attr"/></xsl:call-template></xsl:variable>
    <xsl:choose>
     <xsl:when test="$cnode/attribute::*[name()=$attr]">
      <xsl:value-of select="$prev"/>/<xsl:value-of select="$cnode/attribute::*[name()=$attr]"/>
     </xsl:when>
     <xsl:otherwise>
      <xsl:value-of select="$prev"/>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 
<!-- chiamato con ctx-node pari ad un elemento type/xslt, deve ritornare il percorso completo della sitemap relativo allo stylesheet  -->
 <xsl:template name="rel">
  <xsl:param name="href" select="@href"/>
  <xsl:choose>
   <xsl:when test="contains($href,'/')">../<xsl:call-template name="rel"><xsl:with-param name="href" select="substring-after($href,'/')"/></xsl:call-template></xsl:when>
   <xsl:otherwise>sitemap.xml</xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 
 <xsl:template name="params">
  <xsl:for-each select="param" xml:space="preserve">--stringparam <xsl:value-of select="@name"/> <xsl:value-of select="text()"/> </xsl:for-each>
  <xsl:text xml:space="preserve">--stringparam sitemapuri </xsl:text><xsl:call-template name="rel"/>
  <xsl:text xml:space="preserve"> --stringparam active $(_ACTIVE_ELEM)</xsl:text> <!-- questo sarĂ  un'espressione XPath che indica il nodo relativo alla pagina che stiamo elaborando. xsltproc interpreta i parametri con ctx-node pari alla radice del file xml da trasformare -->
 </xsl:template>
 
 <xsl:template match="type[child::xslt]" xml:space="preserve">
_<xsl:value-of select="@name"/>_PREQ = <xsl:for-each select="xslt" xml:space="preserve"><xsl:value-of select="@href"/> </xsl:for-each>
_<xsl:value-of select="@name"/>_CMD = mkdir -p `dirname $@`;<xsl:for-each select="xslt"><xsl:choose><xsl:when test="position()=1">xsltproc <xsl:call-template name="params"/> <xsl:value-of select="@href"/> $&lt;</xsl:when><xsl:otherwise>|xsltproc <xsl:call-template name="params"/> <xsl:value-of select="@href"/> -</xsl:otherwise></xsl:choose></xsl:for-each> &gt; $@
 
</xsl:template>
 
 <xsl:template match="type[child::command]">
_<xsl:value-of select="@name"/>_PREQ =
define _<xsl:value-of select="@name"/>_CMD
<xsl:for-each select="command">
<xsl:value-of select="."/>
</xsl:for-each>
endef
</xsl:template>
 
</xsl:stylesheet>