| 1 |
<?xml version="1.0" encoding="UTF-8"?> <!-- -*- nxml -*- --> |
| 2 |
<!-- |
| 3 |
Copyright (C) 2016 Universitat d'Alacant / Universidad de Alicante |
| 4 |
Copyright (C) 2018 Xavi Ivars <[email protected]> |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU General Public License as |
| 8 |
published by the Free Software Foundation; either version 2 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
--> |
| 19 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 20 |
<xsl:output method="xml" encoding="UTF-8" indent="yes"/> |
| 21 |
|
| 22 |
<xsl:template match="metalrx"> |
| 23 |
<xsl:apply-templates select="./rules" /> |
| 24 |
</xsl:template> |
| 25 |
|
| 26 |
<xsl:template match="seq"> |
| 27 |
<xsl:copy-of select="/metalrx/def-seqs/def-seq[@n = current()/@n]/*"/> |
| 28 |
</xsl:template> |
| 29 |
|
| 30 |
<xsl:template match="prule"> |
| 31 |
<xsl:call-template name="prule1"> |
| 32 |
<xsl:with-param name="p1" select="@p1" /> |
| 33 |
<xsl:with-param name="p2" select="@p2" /> |
| 34 |
<xsl:with-param name="p3" select="@p3" /> |
| 35 |
<xsl:with-param name="p4" select="@p4" /> |
| 36 |
</xsl:call-template> |
| 37 |
</xsl:template> |
| 38 |
|
| 39 |
<xsl:template name="prule1"> |
| 40 |
<xsl:param name="p1" /> |
| 41 |
<xsl:param name="p2" /> |
| 42 |
<xsl:param name="p3" /> |
| 43 |
<xsl:param name="p4" /> |
| 44 |
|
| 45 |
</xsl:template> |
| 46 |
|
| 47 |
<!-- Rules that don't have a repeat[@upto] element are |
| 48 |
just copied, their repeats ignored: --> |
| 49 |
<xsl:template match="rule[./repeat[@upto]]"> |
| 50 |
<xsl:apply-templates select="./repeat[@upto]"/> |
| 51 |
</xsl:template> |
| 52 |
|
| 53 |
<xsl:template match="repeat[@upto]"> |
| 54 |
<xsl:choose> |
| 55 |
<xsl:when test="not(./following-sibling::repeat[1])"> |
| 56 |
<xsl:call-template name="repeat1"> |
| 57 |
<xsl:with-param name="upto" select="@upto" /> |
| 58 |
<xsl:with-param name="from" select="@from" /> |
| 59 |
</xsl:call-template> |
| 60 |
</xsl:when> |
| 61 |
<xsl:otherwise> |
| 62 |
<ERROR-multiple-repeats><xsl:text>Several repeats per rule not supported yet</xsl:text></ERROR-multiple-repeats> |
| 63 |
</xsl:otherwise> |
| 64 |
</xsl:choose> |
| 65 |
</xsl:template> |
| 66 |
|
| 67 |
<!-- Actually output the stuff, and recurse: --> |
| 68 |
<xsl:template name="repeat1"> |
| 69 |
<xsl:param name="upto" /> |
| 70 |
<xsl:param name="from" select="1" /> |
| 71 |
<xsl:choose><xsl:when test="$upto > 0"> |
| 72 |
<rule> |
| 73 |
<xsl:copy-of select="./../@*"/> |
| 74 |
<xsl:apply-templates select="./preceding-sibling::*"/> |
| 75 |
<xsl:call-template name="repeatElts"> |
| 76 |
<xsl:with-param name="elts" select="./*" /> |
| 77 |
<xsl:with-param name="upto" select="$upto" /> |
| 78 |
<xsl:with-param name="from" select="$from" /> |
| 79 |
</xsl:call-template> |
| 80 |
<xsl:apply-templates select="./following-sibling::*"/> |
| 81 |
</rule> |
| 82 |
<xsl:variable name="next"> |
| 83 |
<repeat> |
| 84 |
<xsl:attribute name="upto"><xsl:value-of select="@upto - 1"/></xsl:attribute> |
| 85 |
<xsl:attribute name="from"><xsl:value-of select="@from"/></xsl:attribute> |
| 86 |
<xsl:copy-of select="./*"/> |
| 87 |
</repeat> |
| 88 |
</xsl:variable> |
| 89 |
<xsl:call-template name="repeat1"> |
| 90 |
<xsl:with-param name="upto" select="$upto - 1" /> |
| 91 |
<xsl:with-param name="from" select="$from" /> |
| 92 |
</xsl:call-template> |
| 93 |
</xsl:when> |
| 94 |
<xsl:when test="$upto = 0 and $from = 0"> |
| 95 |
<rule> |
| 96 |
<xsl:copy-of select="./../@*"/> |
| 97 |
<xsl:apply-templates select="./preceding-sibling::*"/> |
| 98 |
<xsl:apply-templates select="./following-sibling::*"/> |
| 99 |
</rule> |
| 100 |
</xsl:when> |
| 101 |
</xsl:choose> |
| 102 |
</xsl:template> |
| 103 |
|
| 104 |
<xsl:template match="repeat"> <!-- if no upto, you get zero repeats! --> |
| 105 |
</xsl:template> |
| 106 |
|
| 107 |
<!-- Repeat elts upto times --> |
| 108 |
<xsl:template name="repeatElts"> |
| 109 |
<xsl:param name="elts" /> |
| 110 |
<xsl:param name="upto" /> |
| 111 |
<xsl:param name="from" /> |
| 112 |
<xsl:if test="$upto > 0"> |
| 113 |
<xsl:apply-templates select="$elts" /> |
| 114 |
<xsl:call-template name="repeatElts"> |
| 115 |
<xsl:with-param name="elts" select="$elts" /> |
| 116 |
<xsl:with-param name="upto" select="$upto - 1" /> |
| 117 |
<xsl:with-param name="from" select="$from" /> |
| 118 |
</xsl:call-template> |
| 119 |
</xsl:if> |
| 120 |
</xsl:template> |
| 121 |
|
| 122 |
<!-- catch-all --> |
| 123 |
<xsl:template match="@* | node()"> |
| 124 |
<xsl:copy> |
| 125 |
<xsl:apply-templates select="@* | node()"/> |
| 126 |
</xsl:copy> |
| 127 |
</xsl:template> |
| 128 |
|
| 129 |
</xsl:stylesheet> |