简体   繁体   English

用xslt 1.0排序

[英]Sorting with xslt 1.0

i have an evaluation form filled in by a lot of users. 我有很多用户填写的评估表。 i created an export in excel for this form. 我为此表格在Excel中创建了一个导出。 What i want to achieve is: the header of the excel will have all the questions and below every question the answers grouped by the user who filled in the form. 我要实现的是:excel的标题将包含所有问题,并且在每个问题下方均由填写表单的用户分组。 For this, i'm getting the questions by evaluation form and order them by position, then i'm grouping the users who filled in the form, looping through the results of the grouping so i will have the same lines for every question. 为此,我通过评估表获取问题并按职位顺序对它们进行排序,然后对填写表单的用户进行分组,遍历分组结果,以便每个问题的行都相同。 this is a part of the xml i'm generating: 这是我正在生成的xml的一部分:

<QUESTIONNAMES>
    <ITEM>
      <QUESID>468</QUESID>
      <QUESNAME><![CDATA[Name]]></QUESNAME>
    </ITEM>
    <ITEM>
      <QUESID>554</QUESID>
      <QUESNAME><![CDATA[Palce]]></QUESNAME>
    </ITEM>
</QUESTIONNAMES>
<EXPORTLINE>
      <ITEMS>
        <ITEM>
          <QUESID>468</QUESID>
          <VALUES>
            <UserID>25151</UserID>
            <VALUE><![CDATA[dommel]]></VALUE>
            <UserID>45372</UserID>
            <VALUE><![CDATA[Omnium]]></VALUE>
            <UserID>54632</UserID>
            <VALUE><![CDATA[Het Zand]]></VALUE>
            <UserID>56604</UserID>
            <VALUE><![CDATA[rijen]]></VALUE>
            <UserID>57103</UserID>
            <VALUE><![CDATA[Schanswiel]]></VALUE>
            ------  
           </VALUES>
        </ITEM>
      </ITEMS>
</EXPORTLINE>
 <EXPORTLINE>
      <ITEMS>
        <ITEM>
          <QUESID>554</QUESID>
          <VALUES>
            <UserID>22523</UserID>
            <VALUE><![CDATA[test,test]]></VALUE>
            <UserID>44308</UserID>
            <VALUE><![CDATA[Ede]]></VALUE>
            <UserID>47850</UserID>
            <VALUE><![CDATA[Drachten]]></VALUE>
            <UserID>50156</UserID>
            <VALUE><![CDATA[Dalfsen]]></VALUE>
            <UserID>50656</UserID>
            <VALUE><![CDATA[Dongen]]></VALUE>
            -----   
           </VALUES>
        </ITEM>
      </ITEMS>
</EXPORTLINE>

this is my xslt part: 这是我的xslt部分:

<xsl:template name="enquteQuestions">
 <tr> 
    <xsl:for-each select="QUESTIONNAMES/ITEM">
        <td align="left"><xsl:value-of select="QUESNAME" /></td>
    </xsl:for-each>  
 </tr>
 <tr>
    <xsl:for-each select="QUESTIONNAMES/ITEM">
        <xsl:variable name="quesid" select="QUESID" /> 
        <td align="left">   
            <xsl:for-each select="EXPORTLINE/ITEMS/ITEM[QUESID=$quesid]/VALUES/VALUE">
                 <xsl:sort select="VALUE"/>
                <xsl:value-of disable-output-escaping="yes" select="." /><br />
            </xsl:for-each>         
        </td>
    </xsl:for-each> 
</tr>

The problem is that: in the excel file, the answers are not matched, what i mean is that the answers of the second question do not match the answers of the first. 问题是:在excel文件中,答案不匹配,我的意思是第二个问题的答案与第一个问题的答案不匹配。

now i have: 我现在有:

Name           |Place
--------------------------
dommel         | test, test ----> i need to make sure that the answers of the second question match the answer of the first question.

let me know if it is not clear and if you have any suggestions to solve this. 如果不清楚,请告诉我,如果您有任何建议可以解决。

thanks in advance. 提前致谢。

In your for-each the context node is a VALUE element thus the xsl: sort should simply do select="." 在for-each中,上下文节点是VALUE元素,因此xsl:sort应该只是执行select="." .

thanks to all of you for your remakrs and answers. 感谢大家的重新规划和回答。 it was something in the structure of the xml, i was quering on the answers by question vertically and if the user did not fill something fo a question then the order is getting missed up and the answers by question are not grouped by user anymore, so you can have answer of the second question for a user but the answer of the third question for another user and not the same one. 这是xml结构中的某件事,我正在垂直查询问题的答案,如果用户没有填写问题的内容,那么该命令将被遗漏,并且问题的答案也不再由用户分组,因此您可以为用户提供第二个问题的答案,但为另一个用户而不是同一用户获得第三个问题的答案。 now i catch also the questions that the user did not fill in. this way i will have the same results and the same order for all the questiosn and the appropriate answers by user. 现在,我还捕获了用户未填写的问题。通过这种方式,我将对所有问题和用户的相应答案具有相同的结果和相同的顺序。

thanks again. 再次感谢。

This is like a CVS output when you don't know if there is missing fields (Users answers in this case). 当您不知道是否缺少字段时,这就像CVS输出(在这种情况下,用户会回答)。 So, this stylesheet: 因此,此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kUserIDByValue" match="UserID" use="."/>
    <xsl:key name="kValueByQuestion-User" match="VALUE"
             use="concat(../../QUESID,'++',preceding-sibling::UserID[1])"/>
    <xsl:variable name="vQuestions" select="/*/QUESTIONNAMES/ITEM/QUESID"/>
    <xsl:template match="text()"/>
    <xsl:template match="/">
        <table>
            <xsl:apply-templates/>
        </table>
    </xsl:template>
    <xsl:template match="QUESTIONNAMES">
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>
    <xsl:template match="QUESNAME">
        <td>
            <xsl:value-of select="."/>
        </td>
    </xsl:template>
    <xsl:template match="UserID[count(.|key('kUserIDByValue',.)[1])=1]">
        <tr>
            <xsl:apply-templates select="$vQuestions" mode="output">
                <xsl:with-param name="pUserId" select="."/>
            </xsl:apply-templates>
        </tr>
    </xsl:template>
    <xsl:template match="QUESID" mode="output">
        <xsl:param name="pUserId"/>
        <td>
            <xsl:value-of select="key('kValueByQuestion-User',
                                      concat(.,'++',$pUserId))"/>
        </td>
    </xsl:template>
</xsl:stylesheet>

Output: 输出:

<table>
    <tr>
        <td>Name</td>
        <td>Palce</td>
    </tr>
    <tr>
        <td>dommel</td>
        <td></td>
    </tr>
    <tr>
        <td>Omnium</td>
        <td></td>
    </tr>
    <tr>
        <td>Het Zand</td>
        <td></td>
    </tr>
    <tr>
        <td>rijen</td>
        <td></td>
    </tr>
    <tr>
        <td>Schanswiel</td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td>test,test</td>
    </tr>
    <tr>
        <td></td>
        <td>Ede</td>
    </tr>
    <tr>
        <td></td>
        <td>Drachten</td>
    </tr>
    <tr>
        <td></td>
        <td>Dalfsen</td>
    </tr>
    <tr>
        <td></td>
        <td>Dongen</td>
    </tr>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM