File: chado-expand-macros.xsl

package info (click to toggle)
libchado-perl 1.23-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,976 kB
  • ctags: 10,378
  • sloc: xml: 192,540; sql: 165,945; perl: 28,339; sh: 101; python: 73; makefile: 46
file content (44 lines) | stat: -rw-r--r-- 1,481 bytes parent folder | download | duplicates (5)
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
<?xml version="1.0" encoding="utf-8"?>

<!-- AUTHOR: Chris Mungall  cjm at fruitfly dot org  -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:output indent="yes" method="xml"/>

  <!-- index everything with an ID attribute -->
  <xsl:key name="k-macro" match="//*" use="@id"/>

  <!-- match everything -->
  <xsl:template match="/ | @* | node()">
    <xsl:choose>
      <!-- replace terminal xxx_id elements with expanded macro -->
      <xsl:when test="contains(name(.),'_id') and not(*)">
        <xsl:if test="not(key('k-macro',.))">
          <xsl:message>No such ID: <xsl:value-of select="."/></xsl:message>
        </xsl:if>
        <!-- fetch expansion term from ID index -->
        <xsl:copy>
          <xsl:variable name="macro" select="key('k-macro',.)"/>
          <!-- recursively process macros (removing id attr) -->
          <xsl:for-each select="$macro">
            <xsl:copy>
              <xsl:apply-templates select="$macro/*"/>
            </xsl:copy>
          </xsl:for-each>
        </xsl:copy>
      </xsl:when>
      <xsl:when test="count(@id)">
        <!-- remove existing macros (a macro is anything with id attr) -->
      </xsl:when>
      <xsl:otherwise>
        <!-- recursively apply this same template again -->
        <xsl:copy>
          <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>