【问题标题】:XSLT V1 For Each loop with generate_id and key not loopingXSLT V1 For Each 循环与 generate_id 和键不循环
【发布时间】:2018-03-27 16:02:29
【问题描述】:

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

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

在下面的示例中,"options" 模板应该运行 3 次,因为 $sec-rows 变量有 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>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    只需从最后一个(第三个)xsl:for-each

    中删除 Muenchian Grouping
    <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>
    

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

    Yes     
    No      
    N/A
    

    我猜这就是你想要的。

    【讨论】:

    • 太棒了!花了很长时间尝试各种变化 - 我从来没有想过要删除分组。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 2012-08-06
    • 2014-03-15
    相关资源
    最近更新 更多