conversão de xml para xml com o XSTL
Tenho um ficheiro xml de entrada chamado copystud.xml:
<!-- New XML document created with EditiX XML Editor
(http://www.editix.com) at Thu Aug 31 20:55:30 IST 2017 -->
<?xml-stylesheet type="text/xsl" href="copystudent.xsl"?>
<player>
<name>niyut</name>
<score>
<game1>95</game1>
<game2>70</game2>
<game3>80</game3>
</score>
</player>
preciso da saída assim:
<outcome>
<playername>niyut</playername>
<total>237</total>
<maxscore>300</maxscore>
<nextlevel>congrats for next level</nextlevel>
</outcome>
e Ficheiro xsl copystudent.xsl:
<?xml version="1.0" encoding="UTF-8" ?>
<!-- New XSLT document created with EditiX XML Editor (http://www.editix.com) at Fri Sep 01 11:48:27 IST 2017 -->
<xsl:stylesheet version="3.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:xdt="http://www.w3.org/2005/xpath-datatypes"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs xdt err○ fn">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
Evaluating players
<table>
<tr>
<td>totalscore</td>
<td>
<xsl:variable name ="tot_score" select="sum(.//game1 | .//game2 | .//game3)" />
<xsl:value-of select="$tot_score" />
</td>
<td>maxscore</td>
<td>
<xsl:variable name="subj_count" select="count(/player/score/*)"/>
<xsl:value-of select="count(/player/score/*)*100"/>
</td>
<td>evalv</td>
<td>
<xsl:value-of select="$tot_score div $subj_count" />
</td>
</tr>
</table>
</body>
</html>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
não consigo encontrar a pontuação média dos jogadores e depois mostrar se ele vai para o próximo nível ou não. O valor máximo para cada jogo é de 100.
preciso de xml para transformação xml e não html. Por favor, Guia-me para resolver isto.
0
2 answers
As variáveis no XSLT são delimitadas localmente ao bloco em que são declaradas. Você precisa mover as declarações variáveis para o início do modelo, e então elas podem ser usadas em qualquer lugar dentro do modelo
Tenta este XSLT. Nota I alterou o modelo para corresponder aplayer
, visto que simplifica então as expressões XPath no código
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/player">
<xsl:variable name ="tot_score" select="sum(score/*)" />
<xsl:variable name="subj_count" select="count(score/*)"/>
<html>
<body>
Evaluating players
<table>
<tr>
<td>totalscore</td>
<td>
<xsl:value-of select="$tot_score" />
</td>
<td>maxscore</td>
<td>
<xsl:value-of select="$subj_count * 100"/>
</td>
<td>evalv</td>
<td>
<xsl:value-of select="format-number($tot_score div $subj_count, '0.##')" />
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
1
Author: Tim C, 2017-09-12 10:13:30
preciso de xml para transformação xml e não html
Para o XML como saída, pode usar o XSL abaixo para transformar o XML de entrada. Comentários foram adicionados no XSL para referência.
XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- specify output -->
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<!-- points to the player node in the input XML -->
<xsl:template match="player">
<!-- variable for storing the total score of the player. Sums all the values from child nodes of score -->
<xsl:variable name="totalScore" select="sum(score/*)" />
<!-- variable for storing the game count. Counts the child nodes of score -->
<xsl:variable name="gameCnt" select="count(score/*)" />
<!-- variable for storing the average score of the player. Format the output to 2 decimals -->
<xsl:variable name="avgScore" select="format-number($totalScore div $gameCnt, '0.##')" />
<outcome>
<playername>
<xsl:value-of select="name" />
</playername>
<total>
<xsl:value-of select="$totalScore" />
</total>
<maxscore>
<xsl:value-of select="$gameCnt * 100" />
</maxscore>
<nextLevel>
<!-- condition for providing message -->
<xsl:choose>
<!-- condition checks average score greater than 50 -->
<xsl:when test="$avgScore > 50">
<xsl:value-of select="'congrats for next level'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'keep trying'" />
</xsl:otherwise>
</xsl:choose>
</nextLevel>
</outcome>
</xsl:template>
</xsl:stylesheet>
Saída XML
<outcome>
<playername>niyut</playername>
<total>245</total>
<maxscore>300</maxscore>
<nextLevel>congrats for next level</nextLevel>
</outcome>
0
Author: Aniket V, 2017-09-12 10:47:53