XSLT è Turing-complete, vedere ad esempio here o here, quindi è possibile farlo. Ma ho usato XSLT solo una o due volte e non posso dare alcuna soluzione.
UPDATE
Ho appena letto di nuovo un tutorial e ha trovato una soluzione che utilizza il seguente fatto. bitset(x, n)
restituisce true, se il n
bit di x
è impostato, false altrimenti.
bitset(x, n) := floor(x/2^n) mod 2 == 1
Di seguito XSLT
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1" style="text-align:center;">
<tr bgcolor="#9acd32">
<th>Number</th>
<th>Bit 3</th>
<th>Bit 2</th>
<th>Bit 1</th>
<th>Bit 0</th>
</tr>
<xsl:for-each select="numbers/number">
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:choose>
<xsl:when test="floor(. div 8) mod 2 = 1">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</td>
<td>
<xsl:choose>
<xsl:when test="floor(. div 4) mod 2 = 1">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</td>
<td>
<xsl:choose>
<xsl:when test="floor(. div 2) mod 2 = 1">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</td>
<td>
<xsl:choose>
<xsl:when test="floor(. div 1) mod 2 = 1">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
si trasformerà questo XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<numbers>
<number>0</number>
<number>1</number>
<number>2</number>
<number>3</number>
<number>4</number>
<number>5</number>
<number>6</number>
<number>7</number>
<number>8</number>
<number>9</number>
<number>10</number>
<number>11</number>
<number>12</number>
<number>13</number>
<number>14</number>
<number>15</number>
</numbers>
in un documento HTML con una tabella che mostra i bit dei numeri.
Number | Bit 3 | Bit 2 | Bit 1 | Bit 0
---------------------------------------
0 | 0 | 0 | 0 | 0
1 | 0 | 0 | 0 | 1
2 | 0 | 0 | 1 | 0
3 | 0 | 0 | 1 | 1
4 | 0 | 1 | 0 | 0
5 | 0 | 1 | 0 | 1
6 | 0 | 1 | 1 | 0
7 | 0 | 1 | 1 | 1
8 | 1 | 0 | 0 | 0
9 | 1 | 0 | 0 | 1
10 | 1 | 0 | 1 | 0
11 | 1 | 0 | 1 | 1
12 | 1 | 1 | 0 | 0
13 | 1 | 1 | 0 | 1
14 | 1 | 1 | 1 | 0
15 | 1 | 1 | 1 | 1
Questo è né elegante né bello in alcun modo e ci sono probabilmente soluzione molto più semplice, ma funziona. E dato che è il mio primo contatto con XSLT, sono abbastanza soddisfatto.
fonte
2009-07-09 20:05:55
Bella ricerca; dovevo semplicemente cambiare il modello con nome per accettare il numero come parametro e funzionava come un campione per qualsiasi valore arbitrario. – pdwetz