summaryrefslogtreecommitdiffstats
path: root/xml/context-of.xsl
blob: 96b8c173329a29b71b396bd12e80562343dd4447 (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
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="goal-name" select="'id'"/>
<xsl:param name="goal-value" select="'GOAL'"/>
<xsl:param name="style" select="'rng'"/>
<xsl:param name="skip" select="0"/>

<xsl:template match="/">
    <xsl:choose>
        <xsl:when test="not(.//@*[
                            name() = $goal-name
                            and
                            . = $goal-value
                        ])">
            <xsl:message terminate="yes">NOTFOUND</xsl:message>
        </xsl:when>
        <xsl:when test="$style = 'xml'">
            <xsl:call-template name="xpath-xml-elem">
                <xsl:with-param name="terminal-elem"
                                select=".//@*[
                                            name() = $goal-name
                                            and
                                            . = $goal-value
                                        ]/.."/>
            </xsl:call-template>
        </xsl:when>
        <xsl:when test="$style = 'rng'">
            <xsl:call-template name="xpath-rng-elem">
                <xsl:with-param name="terminal-elem"
                                select=".//@*[
                                            name() = $goal-name
                                            and
                                            . = $goal-value
                                        ]/.."/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:message terminate="yes">BADSTYLE</xsl:message>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="xpath-xml-elem">
    <xsl:param name="terminal-elem"/>
    <xsl:variable name="TotalCount"
                  select="count($terminal-elem/ancestor-or-self::*)"/>
    <xsl:for-each select="$terminal-elem/ancestor-or-self::*">
        <xsl:if test="$TotalCount - position() &gt;= $skip">
            <xsl:value-of select="concat('/', name())"/>
        </xsl:if>
    </xsl:for-each>
    <xsl:value-of select="'&#xa;'"/>
</xsl:template>

<xsl:template name="xpath-rng-elem">
    <xsl:param name="terminal-elem"/>
    <xsl:variable name="TotalCount"
                  select="count($terminal-elem/ancestor-or-self::*)"/>
    <xsl:for-each select="$terminal-elem/ancestor-or-self::*">
        <xsl:if test="$TotalCount - position() &gt;= $skip">
            <xsl:choose>
                <xsl:when test="name() = 'attribute'">
                    <xsl:value-of select="concat('/@', @name)"/>
                </xsl:when>
                <xsl:when test="name() = 'define'">
                    <xsl:value-of select="concat('/&lt;', @name, '&gt;')"/>
                </xsl:when>
                <xsl:when test="name() = 'element'">
                    <xsl:value-of select="concat('/', @name)"/>
                </xsl:when>
                <xsl:when test="name() = 'grammar'">
                    <xsl:if test="$TotalCount &lt; 3">
                        <xsl:value-of select="concat('&lt;', name(), '&gt;')"/>
                    </xsl:if>
                </xsl:when>
            </xsl:choose>
        </xsl:if>
    </xsl:for-each>
    <xsl:value-of select="'&#xa;'"/>
</xsl:template>

</xsl:stylesheet>