首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSL for-each迭代

XSL for-each迭代
EN

Stack Overflow用户
提问于 2013-01-19 22:03:52
回答 1查看 820关注 0票数 0

我有两个for-each语句在迭代,我希望输出是垂直的,而不是表中的水平格式。

目前,它输出:

代码语言:javascript
复制
a b c
1 2 3

我想让它输出:

代码语言:javascript
复制
a 1
b 2
c 3

有人能帮帮忙吗?下面是XSL:

代码语言:javascript
复制
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" indent="no"/>
    <xsl:key name="names" match="Niche" use="."/>
    <xsl:key name="niche" match="row" use="Niche"/>

        <xsl:template match="/">
            <table border="1">

                <xsl:for-each select="//Niche[generate-id() = generate-id(key('names',.)[1])]">
                <xsl:sort select="."/>

                    <td>
                    <xsl:value-of select="."/>
                    </td>
                </xsl:for-each>

                <td><tr></tr></td>

                <xsl:for-each select="//row[generate-id(.)=generate-id(key('niche', Niche)[1])]">
                <xsl:sort select="Niche"/>

                    <td>
                    <xsl:value-of select="count(key('niche', Niche))"/>
                    </td>
                </xsl:for-each>

            </table>

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

示例XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl" ?>
<root>
    <row>
        <Niche>a</Niche>
    </row>
    <row>
        <Niche>b</Niche>
    </row>
    <row>
        <Niche>b</Niche>
    </row>
    <row>
        <Niche>c</Niche>
    </row>
    <row>
        <Niche>c</Niche>
    </row>
    <row>
        <Niche>c</Niche>
    </row>
</root>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-19 22:27:47

我很确定你需要做的就是:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="no"/>
  <xsl:key name="niche" match="Niche" use="."/>

  <xsl:template match="/">
    <table border="1">

      <xsl:for-each select="//Niche[generate-id(.)=generate-id(key('niche', .)[1])]">
        <xsl:sort select="."/>
        <tr>
          <td>
            <xsl:value-of select="."/>
          </td>
          <td>
            <xsl:value-of select="count(key('niche', .))"/>
          </td>
        </tr>
      </xsl:for-each>

    </table>

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

但请提供一个示例输入XML,以便我们可以验证。

在样例输入上,这将产生:

代码语言:javascript
复制
<table border="1">
  <tr>
    <td>a</td>
    <td>1</td>
  </tr>
  <tr>
    <td>b</td>
    <td>2</td>
  </tr>
  <tr>
    <td>c</td>
    <td>3</td>
  </tr>
</table>

要从最高出现计数到最低出现计数进行排序,您可以替换以下行:

代码语言:javascript
复制
<xsl:sort select="."/>

有了这个:

代码语言:javascript
复制
<xsl:sort select="count(key('niche', .))" order="descending" data-type="number"/>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14415133

复制
相关文章

相似问题

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