使用XSTL进行xml到xml的转换

时间:2017-09-12 09:56:21

标签: xml xslt

我有一个名为copystud.xml的输入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>

我需要这样的输出:

    <outcome>
<playername>niyut</playername> 
<total>237</total> 
<maxscore>300</maxscore>
 <nextlevel>congrats for next level</nextlevel> 
</outcome>

和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>

我无法找到球员的平均分数,然后显示他是否会进入下一级别。每场比赛的max_score为100.

我需要xml到xml转换而不是html。请指导我解决这个问题。

2 个答案:

答案 0 :(得分:1)

XSLT中的变量本地作用于声明它们的块。您需要将变量声明移动到模板的开头,然后可以在模板中的任何位置使用它们

试试这个XSLT。注意我已经将模板更改为匹配player,因此简化了代码中的XPath表达式

<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 :(得分:0)

  

我需要xml到xml转换而不是html

对于XML作为输出,您可以使用以下XSL来转换输入XML。在XSL中添加了注释以供参考。

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 &gt; 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>

XML输出

<outcome>
    <playername>niyut</playername>
    <total>245</total>
    <maxscore>300</maxscore>
    <nextLevel>congrats for next level</nextLevel>
</outcome>