XSLT for-each循环表输出 [英] XSLT for-each loop table output

查看:92
本文介绍了XSLT for-each循环表输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用XSLT创建一个表,其中过程阶段是标题,而processNo是值.

I want to create a table with XSLT where the process stages are headers and the processNo are the values.

  <PV_Batch masterBatchNo="KH8944">
    <tempTable processStage="Blending" processNo="KF0285" />
    <tempTable processStage="Coating" processNo="KF0282" />
    <tempTable processStage="Compression" processNo="KF0283" />
    <tempTable processStage="Granulation" processNo="12345" />
    <tempTable processStage="Granulation" processhNo="KF0284" />
    <tempTable processStage="Mixing" processNo="KF0286" />
    <tempTable processStage="mixing" processNo="K12035" />
  </PV_Batch>

Blending | Coating | Compression | Granulation | Mixing
KF0285   | KF0282  | KF0283      | 12345       | KF0286
         |         |             | Kf0284      | K12035

我在重复值时遇到问题,每当我重复时,它们就超出了范围,就像这样.

I am having problems with repeating values, whenever I have repeats, they just go out of bounds, like this.

Blending | Coating | Compression | Granulation | Mixing
KF0285   | KF0282  | KF0283      | 12345       | Kf0284  | KF0268  | K12035

这是我的表xslt样式表中的代码片段.

Here is the code snippt within my table xslt stylesheet.

          <xsl:for-each select="//tempTable[generate-id() = generate-id(key('keyComponent', @processStage)[1])]">

              <th width="" align="Left" class="tableHeaderRow">
                  <xsl:value-of select="@processStage" />
                </th>

          </xsl:for-each>

          <tr>
            <xsl:for-each select="tempTable">
              <td align="Left" class="tableNormalRow">
                <xsl:value-of select="@processNo"/>
              </td>
            </xsl:for-each>
          </tr>

推荐答案

所显示的代码片段中的第二个循环必须进行与标头循环相同的分组:

The second loop in the snippet that is shown has to do the same grouping that the loop for the header does:

<xsl:for-each select="//tempTable[generate-id() = generate-id(key('keyComponent', @processStage)[1])]">
      <!-- get current processStage -->
      <xsl:variable name="processStage" select="@processStage"/>
      <td align="Left" class="tableNormalRow">
      <!-- loop over all tempTable belonging to current processStage -->
      <xsl:for-each select="//tempTable[@processStage=$processStage]">
          <p>                  
              <xsl:value-of select="@processNo"/>
          </p>
      </xsl:for-each>
      </td>
</xsl:for-each>

如果processStage有多个值,则将每个值放在同一td中的p中,因此,此表:

If there are multiple values for a processStage this will put them each in a p in the same td, so this table:

Blending | Coating | Compression | Granulation | Mixing
KF0285   | KF0282  | KF0283      | 12345       | KF0286
         |         |             | Kf0284      | K12035

将仅包含2行.

但是,如果这应该产生一个包含3行的表,则解决方案会更加复杂.

If however this should produce a table with 3 rows the solution is a bit more complicated.

XSLT 2.0解决方案:

XSLT 2.0 solution:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:output indent="yes"/>

<xsl:variable name="processStages" select="distinct-values(//tempTable/@processStage)"/>

<xsl:variable name="maxRows"
    select="max(for $i in $processStages return count(//tempTable[@processStage=$i]))"/>

<xsl:template match="PV_Batch">
    <table>
        <tr>
            <xsl:for-each-group select="tempTable" group-by="@processStage">
                <th width="" align="Left" class="tableHeaderRow">
                    <xsl:value-of select="@processStage"/>
                </th>
            </xsl:for-each-group>
        </tr>

        <xsl:variable name="tempTable" select="tempTable"/>
        <xsl:for-each select="1 to $maxRows">
            <xsl:variable name="pos" select="."/>
            <tr>
                <xsl:for-each select="$processStage">
                    <td>
                        <xsl:value-of select="$tempTable[@processStage=current()][$pos]/@processNo"/>
                    </td>
                </xsl:for-each>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>
</xsl:stylesheet>


XSLT 1.0解决方案:


XSLT 1.0 Solution:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output indent="yes"/>

<xsl:key name="keyComponent" match="tempTable" use="@processStage"/>

<xsl:template match="PV_Batch">
    <table>
        <tr>
            <xsl:for-each
                select="//tempTable[generate-id() = generate-id(key('keyComponent', @processStage)[1])]">

                <th width="" align="Left" class="tableHeaderRow">
                    <xsl:value-of select="@processStage"/>
                </th>

            </xsl:for-each>
        </tr>

        <xsl:call-template name="tr"/>
    </table>
</xsl:template>

<xsl:template name="tr">
    <xsl:param name="pos" select="1"/>
    <xsl:variable name="temp">
        <xsl:for-each
            select="//tempTable[generate-id() = generate-id(key('keyComponent', @processStage)[1])]">
            <xsl:value-of select="//tempTable[@processStage=current()/@processStage][$pos]/@processNo"/>
        </xsl:for-each>
    </xsl:variable>
    <xsl:if test="normalize-space($temp)!=''">
        <tr>
            <xsl:for-each
                select="//tempTable[generate-id() = generate-id(key('keyComponent', @processStage)[1])]">
                <td>
                    <xsl:value-of select="//tempTable[@processStage=current()/@processStage][$pos]/@processNo"/>
                </td>
            </xsl:for-each>
        </tr>
        <xsl:call-template name="tr">
            <xsl:with-param name="pos" select="$pos + 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

这篇关于XSLT for-each循环表输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆