首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT for-每个循环表输出

XSLT for-每个循环表输出
EN

Stack Overflow用户
提问于 2014-06-20 14:19:06
回答 1查看 3.2K关注 0票数 0

我想用XSLT创建一个表,其中流程阶段是头,processNo是值。

代码语言:javascript
复制
  <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

我有问题重复的价值观,每当我有重复,他们只是越界,像这样。

代码语言:javascript
复制
Blending | Coating | Compression | Granulation | Mixing
KF0285   | KF0282  | KF0283      | 12345       | Kf0284  | KF0268  | K12035

下面是我的表xslt样式表中的代码片段。

代码语言:javascript
复制
          <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-20 16:27:25

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

代码语言:javascript
复制
<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中,所以这个表:

代码语言:javascript
复制
Blending | Coating | Compression | Granulation | Mixing
KF0285   | KF0282  | KF0283      | 12345       | KF0286
         |         |             | Kf0284      | K12035

将仅由2行组成。

但是,如果这应该生成一个3行的表,那么解决方案就会稍微复杂一些。

编辑:

XSLT2.0解决方案:

代码语言:javascript
复制
<?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>

XSLT1.0解决方案:

代码语言:javascript
复制
<?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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24329420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档