Language detail: XSLT
Coverage: 20.81%
|
number of '+' ratings |
contribution for coverage |
Unsolved challenges
- 文字列で+を表示する (Nested Flatten)
- 年賀はがきの当せん番号 (Nested Flatten)
- 箱詰めパズルの判定 (Nested Flatten)
- 関数やメソッドのソースの平均行数 (Nested Flatten)
- コレクションの実装 (Nested Flatten)
codes
複素数
(Nested
Flatten)
絶対値を求めるのに必要な平方根を求める関数が標準では用意されないので頑張って作ってみたりなど、本質でない部分に労力が... 使えるならEXSLTを使うといいかも。
下記のようなxmlにxsltを適応すると計算できます。:
<complex-list>
<add>
<complex real="3" imag="1" />
<complex real="4" imag="-1" />
</add>
<sub>
<complex real="5" imag="-9" />
<complex real="2" imag="6" />
</sub>
<mul>
<complex real="5" imag="3" />
<complex real="5" imag="8" />
</mul>
<div>
<complex real="9" imag="-7" />
<complex real="9" imag="-3" />
</div>
<abs>
<complex real="2" imag="3" />
</abs>
</complex-list>
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:my="uri:ja.doukaku.org:my-functions"
exclude-result-prefixes="my"
>
<xsl:output method="text" />
<xsl:template match="/complex-list" >
<xsl:for-each select="complex|add|sub|mul|div|abs">
<xsl:variable name="complex" as="xs:decimal*">
<xsl:sequence select="my:evaluate-node(.)" />
</xsl:variable>
<xsl:value-of select="my:complex-to-string($complex[1], $complex[2])" />
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:function name="my:complex-to-string" as="xs:string">
<xsl:param name="real" as="xs:decimal" />
<xsl:param name="imag" as="xs:decimal" />
<xsl:variable name="realstr" as="xs:string" select="xs:string($real)" />
<xsl:variable name="imagsign" as="xs:string">
<xsl:choose>
<xsl:when test="$imag gt 0">
<xsl:value-of select="'+'" />
</xsl:when>
<xsl:when test="$imag lt 0">
<xsl:value-of select="'-'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="imagstr" as="xs:string">
<xsl:choose>
<xsl:when test="fn:abs($imag)=1.0">
<xsl:value-of select="''" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="fn:abs($imag)" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$imag=0">
<xsl:value-of select="$realstr" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="fn:string-join(($realstr, $imagsign, fn:concat($imagstr, 'i')), ' ')" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="my:evaluate-node" as="xs:decimal*">
<xsl:param name="node" as="node()" />
<xsl:variable name="node-name" as="xs:string" select="fn:name($node)" />
<xsl:variable name="complex" as="xs:decimal*">
<xsl:for-each select="$node/*">
<xsl:sequence select="my:evaluate-node(.)" />
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="$node-name = 'complex'">
<xsl:sequence select="$node/@real" />
<xsl:choose>
<xsl:when test="fn:exists($node/@imag)">
<xsl:sequence select="$node/@imag" />
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="0" />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="$node-name = 'add'">
<xsl:sequence select="my:evaluate-add($complex)" />
</xsl:when>
<xsl:when test="$node-name = 'sub'">
<xsl:sequence select="my:evaluate-sub($complex)" />
</xsl:when>
<xsl:when test="$node-name = 'mul'">
<xsl:sequence select="my:evaluate-mul($complex)" />
</xsl:when>
<xsl:when test="$node-name = 'div'">
<xsl:sequence select="my:evaluate-div($complex)" />
</xsl:when>
<xsl:when test="$node-name = 'abs'">
<xsl:sequence select="my:evaluate-abs($complex)" />
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">
<xsl:text>*** unknown element name </xsl:text>
<xsl:value-of select="$node-name" />
<xsl:text> ***</xsl:text>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="my:evaluate-add" as="xs:decimal*">
<xsl:param name="values" as="xs:decimal*" />
<xsl:choose>
<xsl:when test="fn:empty($values)">
<xsl:sequence select="(0,0)" />
</xsl:when>
<xsl:when test="fn:count($values)=2">
<xsl:sequence select="$values" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="intermediate" as="xs:decimal*">
<xsl:sequence select="$values[1] + $values[3]" />
<xsl:sequence select="$values[2] + $values[4]" />
<xsl:sequence select="$values[fn:count(.) gt 4]" />
</xsl:variable>
<xsl:sequence select="my:evaluate-add($intermediate)" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="my:evaluate-sub" as="xs:decimal*">
<xsl:param name="values" as="xs:decimal*" />
<xsl:choose>
<xsl:when test="fn:empty($values)">
<xsl:sequence select="(0,0)" />
</xsl:when>
<xsl:when test="fn:count($values)=2">
<xsl:sequence select="$values" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="intermediate" as="xs:decimal*">
<xsl:sequence select="$values[1] - $values[3]" />
<xsl:sequence select="$values[2] - $values[4]" />
<xsl:sequence select="$values[fn:count(.) gt 4]" />
</xsl:variable>
<xsl:sequence select="my:evaluate-sub($intermediate)" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="my:evaluate-mul" as="xs:decimal*">
<xsl:param name="values" as="xs:decimal*" />
<xsl:choose>
<xsl:when test="fn:empty($values)">
<xsl:sequence select="(0,0)" />
</xsl:when>
<xsl:when test="fn:count($values)=2">
<xsl:sequence select="$values" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="intermediate" as="xs:decimal*">
<xsl:sequence select="$values[1] * $values[3] - $values[2] * $values[4]" />
<xsl:sequence select="$values[1] * $values[4] + $values[2] * $values[3]" />
<xsl:sequence select="$values[fn:count(.) gt 4]" />
</xsl:variable>
<xsl:sequence select="my:evaluate-mul($intermediate)" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="my:evaluate-div" as="xs:decimal*">
<xsl:param name="values" as="xs:decimal*" />
<xsl:choose>
<xsl:when test="fn:empty($values)">
<xsl:sequence select="(0,0)" />
</xsl:when>
<xsl:when test="fn:count($values)=2">
<xsl:sequence select="$values" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="intermediate" as="xs:decimal*">
<xsl:variable name="d" as="xs:decimal"
select="$values[3] * $values[3] + $values[4] * $values[4]" />
<xsl:sequence select="($values[1] * $values[3] + $values[2] * $values[4]) div $d" />
<xsl:sequence select="($values[1] * $values[4] - $values[2] * $values[3]) div $d" />
<xsl:sequence select="$values[fn:count(.) gt 4]" />
</xsl:variable>
<xsl:sequence select="my:evaluate-div($intermediate)" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="my:evaluate-abs" as="xs:decimal*">
<xsl:param name="values" as="xs:decimal*" />
<xsl:sequence select="my:sqrt($values[1] * $values[1] + $values[2] * $values[2])" />
<xsl:sequence select="0" />
</xsl:function>
<xsl:function name="my:sqrt" as="xs:decimal">
<xsl:param name="n" as="xs:decimal" />
<xsl:variable name="base" as="xs:integer*">
<xsl:variable name="nstr" as="xs:string" select="xs:string($n)" />
<xsl:for-each select="0 to
($n idiv fn:string-length(if (fn:contains($nstr, '.'))
then (fn:substring-before($nstr, '.'))
else ($nstr)))" >
<xsl:if test=". * . ge $n">
<xsl:sequence select="." />
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="my:sqrt-search($n, $base[1], 10)" />
</xsl:function>
<xsl:function name="my:sqrt-search-base" as="xs:integer" >
<xsl:param name="n" as="xs:decimal" />
<xsl:param name="m" as="xs:integer" />
<xsl:choose>
<xsl:when test="$m * $m lt $n">
<xsl:value-of select="my:sqrt-search-base($n, $m + 1)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$m" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="my:sqrt-search">
<xsl:param name="n" as="xs:decimal" />
<xsl:param name="m" as="xs:decimal" />
<xsl:param name="prec" as="xs:integer" />
<xsl:choose>
<xsl:when test="$prec = 0">
<xsl:value-of select="$m" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="my:sqrt-search($n, (($m + ($n div $m)) div 2), $prec - 1)" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
</xsl:stylesheet>
|
麻雀の和了判定
(Nested
Flatten)
受け付けるフォーマットはBタイプです。 以下のようなxml文書を入力として与えると、hand要素に"和了形"属性(true/false)を追加して出力します。:
<hands> <hand>1,1,2,3,4,5,6,6,7,7,8,9,9,9</hand> <hand>1,1,2,2,2,3,3,3,3,4,4,5,6,6</hand> <hand>1,1,2,2,4,4,5,5,6,6,7,7,9,9</hand> <hand>1,1,2,2,4,4,5,5,6,7,8,7,8,9</hand> </hands>
後、お題にはなかったのですが、七対子にも対応してみました。
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:my="uri:ja.doukaku.org:my-functions"
exclude-result-prefixes="my"
>
<xsl:output method="xml" />
<xsl:variable name="debug" as="xs:boolean" select="false()" />
<xsl:variable name="can七対子" as="xs:boolean" select="true()" />
<xsl:variable name="m" as="xs:integer" select="3" />
<xsl:variable name="n" as="xs:integer" select="4" />
<xsl:variable name="h" as="xs:integer" select="2" />
<xsl:variable name="M" as="xs:integer" select="9" />
<xsl:variable name="L" as="xs:integer" select="4" />
<xsl:template match="/hands">
<hands>
<xsl:apply-templates select="./hand" />
</hands>
</xsl:template>
<xsl:template match="hand">
<xsl:variable name="pai" as="xs:integer*">
<xsl:variable name="pai-unordered" as="xs:integer*">
<xsl:for-each select="fn:tokenize(text(), ',')">
<xsl:sequence select="xs:integer(.)" />
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="$pai-unordered">
<xsl:sort />
<xsl:sequence select="." />
</xsl:for-each>
</xsl:variable>
<xsl:if test="fn:max($pai) > $M">
<xsl:message terminate="yes">
<xsl:text>invalid max value of pai. expected </xsl:text>
<xsl:value-of select="$M" />
<xsl:text> but </xsl:text>
<xsl:value-of select="fn:max($pai)" />
</xsl:message>
</xsl:if>
<xsl:for-each select="fn:distinct-values($pai)">
<xsl:if test="fn:count(fn:index-of($pai, .)) > $L">
<xsl:message terminate="yes">
<xsl:text>invalid count per a kind, expected </xsl:text>
<xsl:value-of select="$L" />
<xsl:text> but </xsl:text>
<xsl:value-of select="fn:count(fn:index-of($pai, .))" />
<xsl:text> for </xsl:text>
<xsl:value-of select="." />
</xsl:message>
</xsl:if>
</xsl:for-each>
<xsl:element name="hand">
<xsl:attribute name="和了形">
<xsl:value-of select="my:check-和了形($pai)" />
</xsl:attribute>
<xsl:value-of select="text()" />
</xsl:element>
</xsl:template>
<xsl:function name="my:check-和了形" as="xs:boolean">
<xsl:param name="pai" as="xs:integer*" />
<xsl:variable name="result" as="xs:boolean*">
<xsl:sequence select="my:check-対子($pai,0)" />
</xsl:variable>
<xsl:value-of select="fn:count($result)>0" />
</xsl:function>
<xsl:function name="my:check-対子" as="xs:boolean*">
<xsl:param name="pai" as="xs:integer*" />
<xsl:param name="depth" as="xs:integer" />
<xsl:if test="$debug">
<xsl:message>
<xsl:text>check1: </xsl:text>
<xsl:value-of select="$pai" />
</xsl:message>
</xsl:if>
<xsl:choose>
<xsl:when test="fn:empty($pai)">
<xsl:sequence select="true()" />
</xsl:when>
<xsl:otherwise>
<xsl:if test="$depth=0 or $can七対子">
<xsl:for-each select="1 to (fn:count($pai) - ($h - 1))">
<xsl:variable name="i" as="xs:integer" select="." />
<xsl:if test="fn:count(fn:distinct-values(fn:subsequence($pai,$i, $h)))=1">
<xsl:variable name="next-pai" as="xs:integer*"
select="$pai[position() < $i or $i + ($h - 1) < position()]" />
<xsl:sequence select="my:check-対子($next-pai, $depth+1)" />
</xsl:if>
</xsl:for-each>
</xsl:if>
<xsl:if test="$depth=1">
<xsl:sequence select="my:check-刻子($pai, $depth)" />
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="my:check-刻子" as="xs:boolean*">
<xsl:param name="pai" as="xs:integer*" />
<xsl:param name="depth" as="xs:integer" />
<xsl:if test="$debug">
<xsl:message>
<xsl:text>check2: </xsl:text>
<xsl:value-of select="$pai" />
</xsl:message>
</xsl:if>
<xsl:choose>
<xsl:when test="fn:count($pai)=0">
<xsl:if test="$depth - 1!=$n">
<xsl:message terminate="yes">
<xsl:text> invalid count of 刻子/順子. expected </xsl:text>
<xsl:value-of select="$n" />
<xsl:text> but </xsl:text>
<xsl:value-of select="$depth - 1" />
</xsl:message>
</xsl:if>
<xsl:sequence select="true()" />
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="1 to (fn:count($pai) - ($m - 1))">
<xsl:variable name="i" as="xs:integer" select="." />
<xsl:if test="fn:count(fn:distinct-values(fn:subsequence($pai,$i, $m)))=1">
<xsl:variable name="next-pai" as="xs:integer*"
select="$pai[position() < $i or $i + ($m - 1) < position()]" />
<xsl:sequence select="my:check-刻子($next-pai, $depth+1)" />
</xsl:if>
</xsl:for-each>
<xsl:sequence select="my:check-順子($pai, $depth)" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="my:check-順子" as="xs:boolean*">
<xsl:param name="pai" as="xs:integer*" />
<xsl:param name="depth" as="xs:integer" />
<xsl:if test="$debug">
<xsl:message>
<xsl:text>check3: </xsl:text>
<xsl:value-of select="$pai" />
</xsl:message>
</xsl:if>
<xsl:choose>
<xsl:when test="fn:count($pai)=0">
<xsl:if test="$depth - 1 != $n">
<xsl:message terminate="yes">
<xsl:text> invalid count of 刻子/順子. expected </xsl:text>
<xsl:value-of select="$n" />
<xsl:text> but </xsl:text>
<xsl:value-of select="$depth - 1" />
</xsl:message>
</xsl:if>
<xsl:sequence select="true()" />
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="1 to (fn:count($pai) - ($m - 1))">
<xsl:variable name="i" as="xs:integer" select="." />
<xsl:variable name="indexes" as="xs:integer*">
<xsl:variable name="values" as="xs:integer*">
<xsl:sequence select="$pai[$i]" />
<xsl:for-each select="1 to ($m - 1)">
<xsl:sequence select="$pai[$i] + ." />
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="$values">
<xsl:variable name="idx" as="xs:integer*"
select="fn:index-of($pai, .)" />
<xsl:if test="fn:exists($idx)">
<xsl:sequence select="$idx[1]" />
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:if test="fn:count($indexes)=$m">
<xsl:variable name="next-pai" as="xs:integer*"
select="$pai[fn:empty(fn:index-of($indexes, position()))]" />
<xsl:sequence select="my:check-順子($next-pai, $depth+1)" />
</xsl:if>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
</xsl:stylesheet>
|
ナベアツ算
(Nested
Flatten)
「アホになる」をどう表現したものか悩みましたが...
尚、出力はファイルにリダイレクトとかしないとコンソール画面がアホになる恐れがありますので気をつけてください。
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 | <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
>
<xsl:output method="html" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="1 to 100">
<xsl:choose>
<xsl:when test="(. mod 3)=0 or fn:contains(xs:string(.), '3')">
<xsl:variable name="num" as="xs:integer" select="." />
<xsl:for-each select="fn:string-to-codepoints('アホ')">
<xsl:value-of select="fn:codepoints-to-string((.+$num))" />
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
文字列型日時ののN秒後時間取得
(Nested
Flatten)
条件2を満たしてなかった。ので、修正。
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 | <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:my="uri:ja.doukaku.org:my-functions"
exclude-result-prefixes="my"
>
<xsl:output method="text" />
<xsl:template match="/" >
<xsl:value-of select="my:DateEx('2008-09-01T10:02:38', -1)"/>
</xsl:template>
<xsl:function name="my:DateEx" as="xs:string">
<xsl:param name="dstr" as="xs:string" />
<xsl:param name="sec" as="xs:integer" />
<xsl:sequence select="xs:string(
xs:dateTime($dstr) +
xs:dayTimeDuration(
fn:concat(
(if($sec<0)then'-'else''),
'PT', xs:string(fn:abs($sec)), 'S'
)
)
)" />
</xsl:function>
</xsl:stylesheet>
|
入出力の日付文字列はISO8601形式です。
see: "XQuery 1.0 and XPath 2.0 Functions and Operators" の日付関連の説明
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 | <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:my="uri:ja.doukaku.org:my-functions"
exclude-result-prefixes="my"
>
<xsl:output method="text" />
<xsl:template match="/" >
<xsl:value-of select="my:DateEx('2008-09-01T10:02:38', 10000)"/>
</xsl:template>
<xsl:function name="my:DateEx" as="xs:string">
<xsl:param name="dstr" as="xs:string" />
<xsl:param name="sec" as="xs:integer" />
<xsl:sequence select="xs:string(
xs:dateTime($dstr) +
xs:dayTimeDuration(fn:concat('PT',xs:string($sec),'S'))
)" />
</xsl:function>
</xsl:stylesheet>
|
LL Golf Hole 8 - 横向きのピラミッドを作る
(Nested
Flatten)
for expression を使ってみる。 451bytes, 実質444bytes
1 2 3 4 5 6 7 8 | <transform version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:y="y">
<output method="text"/><param name="n"/>
<template match="/"><value-of separator="
" select="y:f(1)"/></template>
<function name="y:f"><param name="i"/>
<sequence select="(y:g($i),if($i=$n)then()else(y:f($i+1),y:g($i)))"/>
</function><function name="y:g"><param name="i"/>
<sequence select="string-join((for$x in(1 to$i)return('*')),'')"/>
</function></transform>
|
LL Golf Hole 7 - バイト数を読みやすくする
(Nested
Flatten)
一応YiBまで対応(処理系が対応してればだけど. 変換対象のバイト数はスタイルシートパラメタnで与えます。 449bytes, 実質443bytes
1 2 3 4 5 6 7 8 | <transform version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:y="y">
<output method="text"/><param name="n"/>
<variable name="u" select="('B','KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB')"/>
<template match="/"><value-of select="y:f($n,1)"/></template>
<function name="y:f"><param name="m"/><param name="i"/>
<sequence select="if($m<1024)then($m,$u[$i])else
y:f(round-half-to-even($m div 1024,2),$i+1)"/>
</function></transform>
|
除算・余剰を使わずに閏年
(Nested
Flatten)
そうか、その手が。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
>
<xsl:output method="text" />
<xsl:template match="/" >
<xsl:for-each select="1900 to 2100">
<xsl:variable name="thisYear" as="xs:date"
select="xs:date(fn:concat(xs:string(.), '-01-01'))" />
<xsl:variable name="nextYear" as="xs:date"
select="xs:date(fn:concat(xs:string(1+.), '-01-01'))" />
<xsl:if test="fn:days-from-duration($nextYear - $thisYear)=366">
<xsl:value-of select="." />
<xsl:text> 年は閏年♪
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
|
LL Golf Hole 3 - 13日の金曜日を数え上げる
(Nested
Flatten)
XSLTでは実行時時刻を取得する手段が標準で存在しないので、 お題の投稿日を「今日」ということにしてます。 一応「今日」が変わっても正しく計算できるようにして、 630 bytes.改行が7つ削れるので実質623bytes
1 2 3 4 5 6 7 8 9 10 11 12 | <transform version="2.0"
xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://www.w3.org/2005/xpath-functions"
xmlns:y="uri:ja.doukaku.org:my-functions">
<variable name="f" select="s:date('2008-08-05')"/>
<output method="text"/><template match="/">
<value-of separator=",">
<for-each select="0 to f:days-from-duration(s:date('2013-12-31')-$f)">
<variable name="d" select="$f+s:dayTimeDuration(f:concat('P',.,'D'))"/>
<sequence select="if(f:day-from-date($d)*(f:days-from-duration($d -s:date('1970-01-04'))mod 7)=65)then $d else()"/>
</for-each></value-of></template></transform>
|
LL Golf Hole 4 - 文章から単語の索引を作る
(Nested
Flatten)
XSLTでも何とかできた。
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 | <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
>
<xsl:output method="text" />
<xsl:variable name="gpl" as="xs:string"
select="unparsed-text('http://www.gnu.org/licenses/gpl.txt')" />
<xsl:variable name="mid" as="element()">
<xsl:element name="root">
<xsl:for-each select="fn:tokenize($gpl, '\n')">
<xsl:element name="line">
<xsl:attribute name="no">
<xsl:value-of select="position()" />
</xsl:attribute>
<xsl:for-each select="fn:tokenize(., '\W+')">
<xsl:element name="word">
<xsl:value-of select="fn:lower-case(.)" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:variable>
<xsl:template match="/" >
<xsl:for-each select="fn:distinct-values($mid//word/text())">
<xsl:sort data-type="text" />
<xsl:variable name="t" as="xs:string" select="." />
<xsl:value-of select="." />
<xsl:text> ... </xsl:text>
<xsl:value-of select="fn:string-join($mid//line[word=$t]/@no, ',')" />
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
|
LL Golf Hole 6 - 10進数を2進数に基数変換する
(Nested
Flatten)
短くなった。卑怯くさいけど。 #7254のコードを利用してますが、 基数だけは弄れるようにしておきました。
1 2 3 4 | <stylesheet version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform">
<import href="http://tinyurl.com/58455r"/>
<variable name="b" select="2"/>
</stylesheet>
|
正攻法で。 冒頭の 変数b の値が基数で、2~10まで対応してます。 短くとか.. 勘弁してください
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 | <x:stylesheet version="2.0"
xmlns:x="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://www.w3.org/2005/xpath-functions"
xmlns:y="uri:ja.doukaku.org:my-functions"
>
<x:variable name="b" as="s:integer" select="2"/>
<x:output method="text"/>
<x:template match="/">
<x:for-each select="0 to 256">
<x:value-of select="y:f(.)"/>
<x:text>
</x:text>
</x:for-each>
</x:template>
<x:function name="y:f" as="s:string">
<x:param name="n" as="s:integer"/>
<x:value-of select="if ($n=0) then '0' else f:string-join(f:reverse(y:g($n)),'')"/>
</x:function>
<x:function name="y:g" as="s:string*">
<x:param name="n" as="s:integer"/>
<x:if test="$n>0">
<x:sequence select="s:string($n mod $b)"/>
<x:sequence select="y:g($n idiv $b)"/>
</x:if>
</x:function>
</x:stylesheet>
|
LL Golf Hole 5 - 最上位の桁を数え上げる
(Nested
Flatten)
XPath の conditional expressions(if then else) など使ってみる。
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 | <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
>
<xsl:output method="text" />
<xsl:variable name="goal" as="xs:string" select="'300'" />
<xsl:template match="/" >
<xsl:variable name="digit" as="xs:integer" select="fn:string-length($goal)" />
<xsl:for-each select="1 to $digit">
<xsl:variable name="d" as="xs:integer" select="." />
<xsl:for-each select="(if (.=1) then 0 else 1)
to
(if (.=$digit) then xs:integer(fn:substring($goal,1,1)) else 9)" >
<xsl:value-of select="." />
<xsl:for-each select="2 to $d">
<xsl:value-of select="0" />
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
|
LL Golf Hole 1 - tinyurl.comを使ってURLを短縮する
(Nested
Flatten)
#6827 の前に考えてたものを出し忘れていました。 しかしやはりXSLTはGolfに向かない。
1 2 3 | <html x:version="2.0" xmlns:x="http://www.w3.org/1999/XSL/Transform">
<x:value-of select="unparsed-text('http://tinyurl.com/api-create.php?url=http://ll.jus.or.jp/2008/info/xgihyo')"/>
</html>
|
LL Golf Hole 2 - 文字列に含まれる単語の最初の文字を大文字にする
(Nested
Flatten)
XSLTもGolfには向きませんね。 本当は改行も全部消したかったんですが、 表示がエライことになるので止めました。 入力は↓のコード自身です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <x:transform version="2.0"
xmlns:x="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://www.w3.org/2005/xpath-functions">
<x:variable name="ll2008" as="s:string">LL future</x:variable>
<x:variable name="ll2005" as="s:string">LL day and night</x:variable>
<x:output method="text"/>
<x:template match="/">
<x:apply-templates select="/x:transform/x:variable"/>
</x:template>
<x:template match="x:variable">
<x:variable name="tokens" as="s:string*">
<x:for-each select="f:tokenize(./text(),' ')">
<x:sequence select="f:concat(f:upper-case(f:substring(.,1,1)),f:substring(.,2,f:string-length(.)-1))"/>
</x:for-each>
</x:variable>
<x:value-of select="f:concat(@name,'=')"/>
<x:value-of select="string-join($tokens,' ')"/>
<x:text>
</x:text>
</x:template>
</x:transform>
|
設定ファイルから値を取得
(Nested
Flatten)
こーゆーのが、XSLTの本来の使い方かと。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<!-- 以下のようなxml文書を渡します
<item name="りんご" cost="200" />
-->
<xsl:output method="text" encoding="sjis" />
<xsl:template match="/item" >
<xsl:call-template name="showPrice" />
</xsl:template>
<xsl:template name="showPrice">
<xsl:text>「</xsl:text>
<xsl:value-of select="@name" />
<xsl:text>」は</xsl:text>
<xsl:value-of select="floor(@cost * 1.05)" />
<xsl:text>円(税込み)です。
</xsl:text>
</xsl:template>
</xsl:stylesheet>
|
/*コメント*/を取り除く
(Nested
Flatten)
XSLT 2.0 ならregexが使えるので、
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 | <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:my="uri:ja.doukaku.org:my-functions"
exclude-result-prefixes="my"
>
<xsl:output method="text" />
<xsl:template match="/" >
<xsl:value-of select="my:remove_comment('AAA')" />
<xsl:text>
</xsl:text>
<xsl:value-of select="my:remove_comment('AAA/*BBB*/')" />
<xsl:text>
</xsl:text>
<xsl:value-of select="my:remove_comment('AAA/*BBB')" />
<xsl:text>
</xsl:text>
<xsl:value-of select="my:remove_comment('AAA/*BBB*/CCC')" />
<xsl:text>
</xsl:text>
<xsl:value-of select="my:remove_comment('AAA/*BBB/*CCC*/DDD*/EEE')" />
<xsl:text>
</xsl:text>
<xsl:value-of select="my:remove_comment('AAA/a//*BB*B**/CCC')" />
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:function name="my:remove_comment" as="xs:string">
<xsl:param name="str" as="xs:string" />
<xsl:value-of
select="fn:replace($str, '/\*.*?(\*/|$)', '')" />
</xsl:function>
</xsl:stylesheet>
|
アルファベットの繰り上がり
(Nested
Flatten)
indexが1始まりだと混乱する...
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 | <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:my="uri:ja.doukaku.org:my-functions"
exclude-result-prefixes="my"
>
<xsl:output method="text" />
<xsl:variable name="convert_table" as="xs:string+">
<xsl:for-each select="1 to 26">
<xsl:sequence
select="fn:codepoints-to-string(
(fn:string-to-codepoints('A')[1] + . - 1)
)" />
</xsl:for-each>
</xsl:variable>
<xsl:variable name="table_size" as="xs:integer"
select="fn:count($convert_table)" />
<xsl:template match="/" >
<xsl:variable name="seq" as="xs:string*">
<xsl:for-each select="1 to 100">
<xsl:sequence select="my:alphabeticalize(.)" />
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="fn:string-join($seq, ', ')" />
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:function name="my:alphabeticalize" as="xs:string">
<xsl:param name="n" as="xs:integer" />
<xsl:variable name="i" as="xs:integer" select="$n - 1" />
<xsl:variable name="n_" as="xs:string"
select="$convert_table[($i mod $table_size) + 1]" />
<xsl:choose>
<xsl:when test="$i idiv $table_size = 0">
<xsl:value-of select="$n_" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of
select="fn:concat(
my:alphabeticalize($i idiv $table_size),
$n_)" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
</xsl:stylesheet>
|
与えられた数字のケタ数
(Nested
Flatten)
next >>
負数にも対応させてみました。 出力結果: 2469 は 4 桁で、最大桁は 1000 の位です。 600 は 3 桁で、最大桁は 100 の位です。 1 は 1 桁で、最大桁は 1 の位です。 -65536 は 5 桁で、最大桁は -10000 の位です。
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 | <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
>
<xsl:output method="text" encoding="sjis" />
<xsl:template match="/" >
<xsl:call-template name="digit">
<xsl:with-param name="n" select="2469" />
</xsl:call-template>
<xsl:call-template name="digit">
<xsl:with-param name="n" select="600" />
</xsl:call-template>
<xsl:call-template name="digit">
<xsl:with-param name="n" select="1" />
</xsl:call-template>
<xsl:call-template name="digit">
<xsl:with-param name="n" select="-65536" />
</xsl:call-template>
</xsl:template>
<xsl:template name="digit">
<xsl:param name="n" as="xs:integer" />
<xsl:variable name="is_negative" as="xs:boolean"
select="$n<0" />
<xsl:variable name="n_" as="xs:string">
<xsl:choose>
<xsl:when test="$is_negative">
<xsl:value-of select="xs:string(-1 * $n)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="xs:string($n)" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$n" />
<xsl:text> は </xsl:text>
<xsl:value-of select="string-length($n_)" />
<xsl:text> 桁で、最大桁は </xsl:text>
<xsl:if test="$is_negative">
<xsl:text>-</xsl:text>
</xsl:if>
<xsl:text>1</xsl:text>
<xsl:for-each select="1 to string-length($n_)-1">
<xsl:text>0</xsl:text>
</xsl:for-each>
<xsl:text> の位です。
</xsl:text>
</xsl:template>
</xsl:stylesheet>
|




noriscape #9441() [ XSLT ] Rating0/0=0.00
XPath1.0の数値型は浮動小数点数しか存在せず、その精度は処理系によって異なるようです。倍精度くらいで実装されているようで、この数値型をそのまま利用すると(29-51行)、条件2の変換が正確に行えませんでした。
そこで、入力された16進数を上位ビットと下位ビットに分けて数値化し、10進数として表示する時に2つの数を合成する手法を用いました(52-95行)。それでもFirefoxだと16桁でヘタってしまいましたが、Xalanは26桁まで頑張ってくれました。
実行結果(実行方法:適当なXMLにこのXSLTを適用)
- Firefox 3.5.1:
0x12437308CCB6 = 20080902065334.
0x2C9C1227FC6520B = 200904012311450100.
0x12437308CCB6 = (bignum) 20080902065334.
0x2C9C1227FC6520B = (bignum) 200904012311450123.
0xCDEF89AB45670123 = (bignum) 14839230665905865403.
0x112233445566778899AABBCCDD = (bignum) 1357463230989497420518412782813.
- Xalan 1.10.0:
0x12437308CCB6 = 20080902065334.
0x2C9C1227FC6520B = 200904012311450112.
0x12437308CCB6 = (bignum) 20080902065334.
0x2C9C1227FC6520B = (bignum) 200904012311450123.
0xCDEF89AB45670123 = (bignum) 14839230665905864995.
0x112233445566778899AABBCCDD = (bignum) 1357463230989497419223659171037.
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" /> <xsl:variable name="var" select="document('')/xsl:stylesheet/xsl:template[@name='var']" /> <xsl:template match="/"> <xsl:variable name="test1" select="'0x12437308CCB6'" /> <xsl:variable name="test2" select="'0x2C9C1227FC6520B'" /> <xsl:variable name="test3" select="'0xCDEF89AB45670123'" /> <xsl:variable name="test4" select="'0x112233445566778899AABBCCDD'" /> <xsl:call-template name="hex2dec"> <xsl:with-param name="input" select="$test1" /> </xsl:call-template> <xsl:call-template name="hex2dec"> <xsl:with-param name="input" select="$test2" /> </xsl:call-template> <xsl:call-template name="bighex2dec"> <xsl:with-param name="input" select="$test1" /> </xsl:call-template> <xsl:call-template name="bighex2dec"> <xsl:with-param name="input" select="$test2" /> </xsl:call-template> <xsl:call-template name="bighex2dec"> <xsl:with-param name="input" select="$test3" /> </xsl:call-template> <xsl:call-template name="bighex2dec"> <xsl:with-param name="input" select="$test4" /> </xsl:call-template> </xsl:template> <xsl:template name="hex2dec"> <xsl:param name="input" /> <xsl:value-of select="$input" /> = <!-- --><xsl:apply-templates select="$var" mode="translate"> <xsl:with-param name="input" select="substring($input,3)" /> </xsl:apply-templates>. <!-- --></xsl:template> <xsl:template match="xsl:template" mode="translate"> <xsl:param name="input" /> <xsl:param name="output" select="0" /> <xsl:choose> <xsl:when test="$input=''"> <xsl:value-of select="$output" /> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="." mode="translate"> <xsl:with-param name="input" select="substring($input,2)" /> <xsl:with-param name="output" select="$output * 16 + hex[@name=substring($input,1,1)]" /> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="bighex2dec"> <xsl:param name="input" /> <xsl:variable name="partition" select="floor((string-length($input)-2) div 2)" /> <xsl:value-of select="$input" /> = (bignum) <!-- --><xsl:apply-templates select="$var" mode="btranslate"> <xsl:with-param name="input" select="substring($input,3,$partition)" /> <xsl:with-param name="input2" select="substring($input,3+$partition)" /> </xsl:apply-templates>. <!-- --></xsl:template> <xsl:template match="xsl:template" mode="btranslate"> <xsl:param name="input" select="''" /> <xsl:param name="input2" select="''" /> <xsl:param name="output" select="0" /> <xsl:param name="output2" select="0" /> <xsl:variable name="in1" select="substring($input,1,1)" /> <xsl:choose> <xsl:when test="$input=''"> <xsl:choose> <xsl:when test="$input2=''"> <xsl:variable name="outA" select="substring($output,1,string-length($output)-string-length($output2))" /> <xsl:variable name="outB" select="substring($output,string-length($output)-string-length($output2)+1) + $output2" /> <xsl:variable name="overflow" select="number(string-length($outB)>string-length($output2))" /> <xsl:value-of select="$outA + $overflow" /> <xsl:value-of select="substring($outB,1+$overflow)" /> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="." mode="btranslate"> <xsl:with-param name="input2" select="substring($input2,2)" /> <xsl:with-param name="output" select="$output*16" /> <xsl:with-param name="output2" select="$output2 * 16 + hex[@name=substring($input2,1,1)]" /> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="." mode="btranslate"> <xsl:with-param name="input" select="substring($input,2)" /> <xsl:with-param name="input2" select="$input2" /> <xsl:with-param name="output" select="$output * 16 + hex[@name=substring($input,1,1)]" /> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="text()" mode="translate" /> <xsl:template match="text()" mode="btranslate" /> <xsl:template match="text()" /> <xsl:template name="var"> <hex name="0">0</hex> <hex name="1">1</hex> <hex name="2">2</hex> <hex name="3">3</hex> <hex name="4">4</hex> <hex name="5">5</hex> <hex name="6">6</hex> <hex name="7">7</hex> <hex name="8">8</hex> <hex name="9">9</hex> <hex name="A">10</hex> <hex name="B">11</hex> <hex name="C">12</hex> <hex name="D">13</hex> <hex name="E">14</hex> <hex name="F">15</hex> </xsl:template> </xsl:stylesheet>Rating0/0=0.00-0+
[ reply ]