|
|
Ajustando o XSl variável
|
o stylesheet 1 e o stylesheet 3 mostram as maneiras diferentes de ajustar um xsl: variável, stylesheet 2 de XSLT e stylesheet 4 de XSLT de ajustar o 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>
|
|
Variáveis com o mesmo nome
|
Stylesheet pode fazer tem diversas variáveis com o mesmo nome. O stylesheet 1 de XSL demonstra como recuperar o valor da variável global que tem mesmo nome que aquele do local. O valor da variável local é limitado ao xsl: quando elemento. O descanso do molde vê conseqüentemente somente uma variável global.
|
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 do Com-Param de XSL
|
Os parâmetros para o molde podem ser passados com um xsl: elemento do com-param. Se um molde contiver o xsl: elemento do param que tem mesmo nome que aquele de um atributo conhecido do xsl: o com-param, este valor é usado. O stylesheet 1 de XSL mostra o exemplo típico. Se você quiser passar a variável, você deve ter que definir esta variável com o xsl: elemento do param. Olhar o stylesheet 2 de XSL para a aproximação errada.
|
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>
|
|
ajustando o xsl: variável,
|
A variável pode prender o fragmento da árvore do resultado. As operações que são permitidas no fragmento da árvore do resultado são o subconjunto daquelas permitidas no nó-ajustaram-se. A operação é permitida no fragmento da árvore do resultado somente se essa operação teria que ser permitida na corda, operação na corda pode envolver primeiramente converter uma corda ao número ou ao booleano. No detalhe, não é permitida para usar “/, //, e []” operadores nos fragmentos da árvore do resultado. Quando a operação permitida é executada no fragmento da árvore do resultado, está executada exatamente na maneira porque estaria em um equivalente nó-se ajustou.
|
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
|