|
|
Regolando il XSl variabile
|
lo stylesheet 1 e lo stylesheet 3 mostra i sensi differenti di regolazione del xsl: variabile, stylesheet 2 di XSLT e stylesheet 4 di XSLT di regolazione del xsl: param.
|
XML Source
<source>
<chapter>Chapter A</chapter>
<chapter>Chapter B</chapter>
<chapter>Chapter C</chapter>
<chapter>Chapter D</chapter>
</source>
|
Output 1
<TABLE>
<TR>
<TD>Chapter A (1/4)</TD>
</TR>
<TR>
<TD>Chapter B (2/4)</TD>
</TR>
<TR>
<TD>Chapter C (3/4)</TD>
</TR>
<TR>
<TD>Chapter D (4/4)</TD>
</TR>
</TABLE>
|
|
XSL stylesheet 1
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:variable name="totalChapters">
<xsl:value-of select="count(//chapter)"/>
</xsl:variable>
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//chapter">
<TR>
<TD>
<xsl:value-of select="."/>
<xsl:text> (</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$totalChapters"/>
<xsl:text>)</xsl:text>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
|
|
XML Source
<source>
<chapter>Chapter A</chapter>
<chapter>Chapter B</chapter>
<chapter>Chapter C</chapter>
<chapter>Chapter D</chapter>
</source>
|
Output 2
<TABLE>
<TR>
<TD>Chapter A (1/4)</TD>
</TR>
<TR>
<TD>Chapter B (2/4)</TD>
</TR>
<TR>
<TD>Chapter C (3/4)</TD>
</TR>
<TR>
<TD>Chapter D (4/4)</TD>
</TR>
</TABLE>
|
|
XSL stylesheet 2
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:param name="totalChapters">
<xsl:value-of select="count(//chapter)"/>
</xsl:param>
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//chapter">
<TR>
<TD>
<xsl:value-of select="."/>
<xsl:text> (</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$totalChapters"/>
<xsl:text>)</xsl:text>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
|
|
XML Source
<source>
<chapter>Chapter A</chapter>
<chapter>Chapter B</chapter>
<chapter>Chapter C</chapter>
<chapter>Chapter D</chapter>
</source>
|
Output 3
<TABLE>
<TR>
<TD>Chapter A (1/4)</TD>
</TR>
<TR>
<TD>Chapter B (2/4)</TD>
</TR>
<TR>
<TD>Chapter C (3/4)</TD>
</TR>
<TR>
<TD>Chapter D (4/4)</TD>
</TR>
</TABLE>
|
|
XSL stylesheet 3
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:variable name="totalChapters" select="count(//chapter)"/>
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//chapter">
<TR>
<TD>
<xsl:value-of select="."/>
<xsl:text> (</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$totalChapters"/>
<xsl:text>)</xsl:text>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
|
|
XML Source
<source>
<chapter>Chapter A</chapter>
<chapter>Chapter B</chapter>
<chapter>Chapter C</chapter>
<chapter>Chapter D</chapter>
</source>
|
OutPut 4
<TABLE>
<TR>
<TD>Chapter A (1/4)</TD>
</TR>
<TR>
<TD>Chapter B (2/4)</TD>
</TR>
<TR>
<TD>Chapter C (3/4)</TD>
</TR>
<TR>
<TD>Chapter D (4/4)</TD>
</TR>
</TABLE>
|
|
XSL stylesheet 4
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:param name="totalChapters" select="count(//chapter)"/>
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//chapter">
<TR>
<TD>
<xsl:value-of select="."/>
<xsl:text> (</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$totalChapters"/>
<xsl:text>)</xsl:text>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
|
|
Variabili con lo stesso nome
|
Stylesheet pu� fare ha parecchie variabili con lo stesso nome. Lo stylesheet 1 di XSL dimostra come recuperare il valore della variabile globale che ha stesso nome di quello di quello locale. Il valore della variabile locale � limitato al xsl: quando elemento. Il resto della mascherina quindi vede soltanto una variabile globale.
|
XML Source
<source>
<chapter>Chapter A</chapter>
<chapter>Chapter B</chapter>
<chapter>Chapter C</chapter>
<chapter>Chapter D</chapter>
</source>
|
Output
<TABLE>
<TR>
<TD>First chapter : Chapter A</TD>
</TR>
<TR>
<TD>Chapter : Chapter B</TD>
</TR>
<TR>
<TD>Chapter : Chapter C</TD>
</TR>
<TR>
<TD>Last chapter : Chapter D</TD>
</TR>
</TABLE>
|
|
XSL stylesheet
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:variable name="text">Chapter</xsl:variable>
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//chapter">
<TR>
<TD>
<xsl:variable name="text">
<xsl:choose>
<xsl:when test="position() = 1">First chapter</xsl:when>
<xsl:when test="position()=last()">Last chapter</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$text"/>
<xsl:text> : </xsl:text>
<xsl:value-of select="."/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
|
|
Elemento di Con-Param di XSL
|
I parametri per la mascherina possono essere passati con un xsl: elemento di con-param. Se una mascherina contiene il xsl: elemento di param che ha stesso nome di quello di un attributo nome di xsl: il con-param, questo valore � usato. Lo stylesheet 1 di XSL mostra l'esempio tipico. Se desiderate passare la variabile, dovreste dovere definire questa variabile con il xsl: elemento di param. Guardare lo stylesheet 2 di XSL per il metodo errato.
|
XML Source
<source>
<number>1</number>
<number>3</number>
<number>4</number>
<number>17</number>
<number>8</number>
</source>
|
Output
<TABLE>
<TR>
<TH>1 (odd)</TH>
</TR>
<TR>
<TH>3 (odd)</TH>
</TR>
<TR>
<TH>4 (even)</TH>
</TR>
<TR>
<TH>17 (odd)</TH>
</TR>
<TR>
<TH>8 (even)</TH>
</TR>
</TABLE>
|
|
XSL stylesheet 1
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//number">
<TR>
<TH>
<xsl:choose>
<xsl:when test="text() mod 2">
<xsl:apply-templates select=".">
<xsl:with-param name="type">odd</xsl:with-param>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="."/>
</xsl:otherwise>
</xsl:choose>
</TH>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
<xsl:template match="number">
<xsl:param name="type">even</xsl:param>
<xsl:value-of select="."/>
<xsl:text> (</xsl:text>
<xsl:value-of select="$type"/>
<xsl:text>)</xsl:text>
</xsl:template>
</xsl:stylesheet>
|
XSL stylesheet 2
XSLT stylesheet
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//number">
<TR>
<TH>
<xsl:choose>
<xsl:when test="text() mod 2">
<xsl:apply-templates select=".">
<xsl:with-param name="type">odd</xsl:with-param>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="."/>
</xsl:otherwise>
</xsl:choose>
</TH>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
<xsl:template match="number">
<xsl:variable name="type">even</xsl:variable>
<xsl:value-of select="."/>
<xsl:text> (</xsl:text>
<xsl:value-of select="$type"/>
<xsl:text>)</xsl:text>
</xsl:template>
</xsl:stylesheet>
|
|
regolazione del xsl: variabile,
|
La variabile pu� tenere il frammento dell'albero di risultato. I funzionamenti che sono consentiti sul frammento dell'albero di risultato sono il sottoinsieme di quelli consentiti sul nodo-si sono regolati. Il funzionamento � consentito sul frammento dell'albero di risultato soltanto se quel funzionamento dovesse essere consentito sulla stringa, funzionamento su stringa pu� coinvolgere in primo luogo convertire una stringa in numero o in booleano. In particolare, non � consentito per usare �/, // e []� operatori sui frammenti dell'albero di risultato. Quando il funzionamento consentito � realizzato sul frammento dell'albero di risultato, � effettuato esattamente nel senso poich� sarebbe su un equivalente nodo-si � regolato.
|
XML Source
<source>
<TABLE border="1">
<TR>
<TD>AAA</TD>
<TD>BBB</TD>
</TR>
<TR>
<TD>aaa</TD>
<TD>bbb</TD>
</TR>
</TABLE>
<TABLE border="1">
<TR>
<TD>1111111</TD>
</TR>
<TR>
<TD>22222222</TD>
</TR>
</TABLE>
</source>
|
Output
<TABLE border="1">
<TR>
<TD>1111111</TD>
</TR>
<TR>
<TD>22222222</TD>
</TR>
</TABLE>
<TABLE border="1">
<TR>
<TD>AAA</TD>
<TD>BBB</TD>
</TR>
<TR>
<TD>aaa</TD>
<TD>bbb</TD>
</TR>
</TABLE>
<TABLE border="1">
<TR>
<TD>1111111</TD>
</TR>
<TR>
<TD>22222222</TD>
</TR>
</TABLE>
|
|
XSL stylesheet
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:variable name="A1">
<xsl:copy-of select="//TABLE[1]"/>
</xsl:variable>
<xsl:variable name="A2">
<xsl:copy-of select="//TABLE[2]"/>
</xsl:variable>
<xsl:template match="/">
<xsl:copy-of select="$A2"/>
<xsl:copy-of select="$A1"/>
<xsl:copy-of select="$A2"/>
</xsl:template>
</xsl:stylesheet>
|
|
|
|
Keywords XSL Variables, XSL Variables, xslt variables, xsl variable, xslt variable, xsl for each,
xsl value of, asp net variables, asp net xsl, xpath variable, variables in xsl,
variable in xslt, variables in xslt, variable in xsl, variables xsl, xsl variable scope,
xsl set variable, xslt global variable, xsl variable value, xsl generate, xsl set,
functions variables, convert xsl, date variables, xsl multiple, include variables,
display variables, using variables, output variables, set variables, multiple variables,
xsl call, xslt parameters, xml parameters, xsl parameters, xml variable, xml variables
|