XSLT V1 For Each循环使用generate_id和key不循环

时间:2018-03-27 16:02:30

标签: xml xslt

我有一个XML文档,我正在使用XSLT进行转换(XML无法更改)。

我正在使用一组带有for-each循环的键来对数据进行分组,这对我的第一组和第二组很有效,但我的第三组不是迭代的。节点集是正确的,有3个节点可以循环,但它只是在第一次运行后停止并进入父循环的下一次迭代。

在下面的示例中,"options"模板应运行3次,因为$sec-rows变量有3个节点对应于3个xml记录。它不是。

任何帮助都会非常感激!

XSL:

<xsl:stylesheet version="1.0" 

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" />
    <xsl:key name="row-by-instance" match="DataRow" use="section_id" />
    <xsl:key name="row-by-stream" match="DataRow" use="concat(section_id, '|', question_obj)" />
    <xsl:key name="row-by-day" match="DataRow" use="concat(section_id, '|', question_obj)" />
    <xsl:template match="/QueryResults/Data">

        <xsl:variable name="columns" select="DataRow[generate-id() = generate-id(key('row-by-day',concat(Instance, '|', DayOfWeek))[1])]" />
        <style type="text/css">
            .tg  {border-collapse:collapse;border-spacing:0;page-break-after: always;}
            .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
            .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
            .tg .tg-q4ae{font-weight:bold;background-color:#ffcc67;vertical-align:top;text-align:left;}
            .tg .tg-oe6v{font-weight:bold;font-size:18px;font-family:Arial,sans-serif;background-color:#aeb1dd;vertical-align:top; text-align:left;}
            .tg .tg-yw4l{vertical-align:top; font-size:10px;}
        </style>
        <div style="margin: 2px; width: 1200px;">
            <div style="width: 1200px;  background-color: #1A5993; color: #FFFFFF;  font-size: large; font-weight: bold;">
                <table style="width: 1200px;  background-color: #1A5993; color: #FFFFFF;  font-size: large; font-weight: bold;">
                    <tr>
                        <td>
                             Child Health Template Checker - <xsl:value-of select="/QueryResults/Data[1]/DataRow[1]/title[1]"/> 
                        </td>
                    </tr>
                </table>
            </div>
            <br />
            <xsl:for-each select="DataRow[count(. | key('row-by-instance', section_id)[1]) = 1]">
            <table class="tg" width="1050px">
                    <xsl:variable name="instance-rows" select="key('row-by-instance', section_id)" />
                <tr><th colspan="4" class="tg-oe6v" style="border-right:0px;">Section: <xsl:value-of select="title"/> </th>
                    <th class="tg-oe6v"><xsl:if test="repeatflag=1">Repeating Section</xsl:if>
                </th></tr>
                <tr>
                    <td class="tg-q4ae" width="400px">Question</td>
                    <td class="tg-q4ae" width="200px">Type</td>
                    <td class="tg-q4ae" width="200px">SNOMED Code</td>
                    <td class="tg-q4ae" width="400px">SNOMED Term</td>
                    <td class="tg-q4ae" width="400px">Options</td>
                    <xsl:for-each select="$instance-rows[generate-id() = generate-id(key('row-by-stream', concat(section_id, '|', question_obj))[1])]">
                    <xsl:call-template name="questions"/>
                    </xsl:for-each>
                </tr> 
             </table>
                <br />
            </xsl:for-each>
        </div>
    </xsl:template>

    <xsl:template name="questions">
            <tr>
                <td class="tg-yw41" width="200px"><xsl:value-of select="Question"/></td>
                <td class="tg-yw41" width="200px"><xsl:value-of select="Question_Type"/></td>
                <td class="tg-yw41" width="200px"><xsl:value-of select="Question_SnomedCode"/></td>
                <td class="tg-yw41" width="200px"><xsl:value-of select="Question_Snomed_Term"/></td>
                <td>
                    <xsl:if test="Option_Text!=''">
                    <table class="tg" width="1050px">
                        <tr>
                            <td class="tg-q4ae" >Option</td>
                            <td class="tg-q4ae" >Code</td>
                            <td class="tg-q4ae" >Term</td>
                        </tr>
                        <xsl:variable name="sec-rows" select="key('row-by-day', concat(section_id, '|', question_obj))" />
                        <xsl:for-each select="$sec-rows[generate-id() = generate-id(key('row-by-day', concat(section_id, '|', question_obj)))]">
                    <xsl:call-template name="options"/>
                        </xsl:for-each>
                    </table>
                    </xsl:if>
                </td>
            </tr>
    </xsl:template>

    <xsl:template name="options">
 <tr>
     <td class="tg-yw41" width="200px"><xsl:value-of select="Option_Text"/></td>
     <td class="tg-yw41" width="200px"><xsl:value-of select="Option_SnomedCode"/></td>
     <td class="tg-yw41" width="200px"><xsl:value-of select="Option_SnomedTerm"/></td>
 </tr>   

    </xsl:template>
</xsl:stylesheet>

XML:

    <?xml version="1.0" encoding="UTF-8"?>
<QueryResults ReportName="Child Health Template Checker" ReportID="202">
    <Data RecordCount="331">
        <DataRow index="8">
            <section_id>241</section_id>
            <title>Health Promotion issues discussed</title>
            <elements_id>239</elements_id>
            <sequence_number>2</sequence_number>
            <repeatflag>0</repeatflag>
            <question_obj>49</question_obj>
            <Question>Advice and information given on social development?</Question>
            <Question_Number>1</Question_Number>
            <Question_Type>Radio</Question_Type>
            <Question_SnomedCode>320651000000100</Question_SnomedCode>
            <Question_Snomed_Term>Child health screening of social behaviour and play development</Question_Snomed_Term>
            <Option_Text>Yes</Option_Text>
            <Option_SnomedCode/>
            <Option_SnomedTerm/>
        </DataRow>
        <DataRow index="9">
            <section_id>241</section_id>
            <title>Health Promotion issues discussed</title>
            <elements_id>239</elements_id>
            <sequence_number>2</sequence_number>
            <repeatflag>0</repeatflag>
            <question_obj>49</question_obj>
            <Question>Advice and information given on social development?</Question>
            <Question_Number>1</Question_Number>
            <Question_Type>Radio</Question_Type>
            <Question_SnomedCode>320651000000100</Question_SnomedCode>
            <Question_Snomed_Term>Child health screening of social behaviour and play development</Question_Snomed_Term>
            <Option_Text>No</Option_Text>
            <Option_SnomedCode/>
            <Option_SnomedTerm/>
        </DataRow>
        <DataRow index="10">
            <section_id>241</section_id>
            <title>Health Promotion issues discussed</title>
            <elements_id>239</elements_id>
            <sequence_number>2</sequence_number>
            <repeatflag>0</repeatflag>
            <question_obj>49</question_obj>
            <Question>Advice and information given on social development?</Question>
            <Question_Number>1</Question_Number>
            <Question_Type>Radio</Question_Type>
            <Question_SnomedCode>320651000000100</Question_SnomedCode>
            <Question_Snomed_Term>Child health screening of social behaviour and play development</Question_Snomed_Term>
            <Option_Text>N/A</Option_Text>
            <Option_SnomedCode/>
            <Option_SnomedTerm/>
        </DataRow>
        </Data>
</QueryResults>

1 个答案:

答案 0 :(得分:0)

从最后一个({3}}

中删除 Muenchian分组
xsl:for-each

并输出所有三个值作为选项:

<xsl:variable name="sec-rows" select="key('row-by-day', concat(section_id, '|', question_obj))" />
<xsl:for-each select="$sec-rows">            <!-- removed key(..) -->
    <xsl:call-template name="options"/>
</xsl:for-each>

猜猜这就是你想要的。