Project

Profile

Help

How to connect?
Download (3.23 KB) Statistics
| Branch: | Revision:

he / tools / map-print.xsl @ 01d6fdb6

1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
  xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:map="http://www.w3.org/2005/xpath-functions/map"
4
  xmlns:X="IndirectXSLT" exclude-result-prefixes="xs" version="3.0">
5
  <xsl:template name="map-print">
6
    <xsl:param name="items" as="item()*"/>
7
    <xsl:for-each select="$items" xmlns="IndirectXSLT">
8
      <xsl:choose>
9
        <xsl:when test=". instance of map(*)">
10
          <map>
11
            <xsl:variable name="this" select="."/>
12
            <xsl:for-each select="map:keys($this)">
13
              <xsl:sort select="."/>
14
              <map-entry key="'{.}'">
15
                <xsl:call-template name="map-print">
16
                  <xsl:with-param name="items" select="$this(.)"/>
17
                </xsl:call-template>
18
              </map-entry>
19
            </xsl:for-each>
20
          </map>
21
        </xsl:when>
22
        <xsl:when test=". instance of element()">
23
          <xsl:copy-of select="."/>
24
        </xsl:when>
25
        <xsl:otherwise>
26
          <xsl:value-of select="."/>
27
        </xsl:otherwise>
28
      </xsl:choose>
29
    </xsl:for-each>
30
  </xsl:template>
31
  
32
  <xsl:variable name="map-joiner">,<xsl:text>
33
  </xsl:text></xsl:variable>
34
  <xsl:variable name="quot">'</xsl:variable>
35
  <xsl:variable name="quoter" select="function($s) {string-join(($quot,replace($s,$quot,$quot||$quot),$quot))}"/>
36
  <xsl:template name="map-print-XPath">
37
    <xsl:param name="items" as="item()*"/>
38
    <xsl:for-each select="$items" xmlns="IndirectXSLT">
39
      <xsl:choose>
40
        <xsl:when test=". instance of map(*)">
41
          <xsl:variable name="this" select="."/>
42
          <xsl:variable name="entries" as="xs:string*">
43
            <xsl:for-each select="map:keys($this)">
44
              <xsl:sort select="."/>
45
              <xsl:variable name="key" as="xs:string">
46
                <xsl:choose>
47
                  <xsl:when test=". instance of xs:string">
48
                    <xsl:value-of select="''''||.||''''"/>
49
                  </xsl:when>
50
                  <xsl:otherwise>
51
                    <xsl:value-of select="."/>
52
                  </xsl:otherwise>
53
                </xsl:choose>
54
              </xsl:variable>
55
              <xsl:variable name="value" as="xs:string">
56
                <xsl:variable name="parts" as="xs:string*">
57
                  <xsl:call-template name="map-print-XPath">
58
                    <xsl:with-param name="items" select="$this(.)"/>
59
                  </xsl:call-template>
60
                </xsl:variable>
61
                <xsl:value-of
62
                  select="if(count($parts) ne 1) then '('||string-join($parts,',')||')' else $parts"
63
                />
64
              </xsl:variable>
65
              <xsl:value-of select="$key,':=',$value"/>
66
            </xsl:for-each>
67
          </xsl:variable>
68
          <xsl:value-of select="'map {', string-join($entries,$map-joiner),'}'"/>
69
        </xsl:when>
70
        <xsl:when test=". instance of element()">
71
          <xsl:copy-of select="."/>
72
        </xsl:when>
73
        <xsl:when test=". instance of xs:string">
74
          <xsl:value-of select="$quoter(.)"/>
75
        </xsl:when>
76
        <xsl:otherwise>
77
          <xsl:value-of select="."/>
78
        </xsl:otherwise>
79
      </xsl:choose>
80
    </xsl:for-each>
81
  </xsl:template>
82
</xsl:stylesheet>
(15-15/15)