1

我想我只是提出了一个新问题,也可以帮助同样寻找解决方案的人。我想从每 3 个项目中添加一个类。

这是我的xml代码

<groups>
<elements>
<name>//1</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2012-03-04</PostDate>
<trackno></trackno>
</elements>

<elements>
<name>//2</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2011-03-04</PostDate>
<trackno></trackno>
</elements>

<elements>
<name>//3</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2012-07-04</PostDate>
<trackno></trackno>
</elements>


<elements>
<name>//4</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2009-01-04</PostDate>
<trackno></trackno>
</elements>



<elements>
<name>//5</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2009-02-04</PostDate>
<trackno></trackno>
</elements>


<elements>
<name>//6</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2008-06-04</PostDate>
<trackno></trackno>
</elements>

<elements>
<name>//7</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2012-01-04</PostDate>
<trackno></trackno>
</elements>


<elements>
<name>//8</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2012-05-14</PostDate>
<trackno></trackno>
</elements>



<elements>
<name>//9</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2012-05-28</PostDate>
<trackno></trackno>
</elements>



<elements>
<name>//10</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2011-02-25</PostDate>
<trackno></trackno>
</elements>



<elements>
<name>//11</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2011-12-20</PostDate>
<trackno></trackno>
</elements>



<elements>
<name>//12</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2011-11-07</PostDate>
<trackno></trackno>
</elements>




<elements>
<name>//13</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2011-07-09</PostDate>
<trackno></trackno>
</elements>


<elements>
<name>//14</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music></Music>
</Hobbbies>
<Height></Height>
<PostDate>2005-10-19</PostDate>
<trackno></trackno>
</elements>
</groups>

这是我的 XSLT

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



    <xsl:template match="/">

      <html>
      <body>
        <h2>Try to display only 9 or 12</h2>
          <xsl:for-each select="/groups/elements">
            <xsl:sort select="PostDate" order="descending"/>
            <xsl:if test="not(position() > 10)">
            <b><xsl:value-of select="PostDate" /></b> - 
            <font color='red'><xsl:value-of select="name" /></font>
           <p></p>
           </xsl:if>
          </xsl:for-each>

      </body>
      </html>
    </xsl:template>

    </xsl:stylesheet> 

只是想要一种简单的方法来插入课程,非常感谢

我想得到

        <div clas="item">

        2012-07-04 - //3 

        2012-05-28 - //9 

       <div class="third"> 2012-05-14 - //8 </div>

        2012-03-04 - //1 

        2012-01-04 - //7 

        <div class="third"> 2011-12-20 - //11 </div>

        2011-11-07 - //12 

        2011-07-09 - //13 

        <div class="third">2011-03-04 - //2  </div>

        2011-02-25 - //10 
      </div>
4

2 回答 2

2

这种转变

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes"/>

    <xsl:template match="/">
      <div class="item">
          <xsl:for-each select="/groups/elements">
            <xsl:sort select="PostDate" order="descending"/>
            <xsl:if test="not(position() > 10)">
                <xsl:choose>
                  <xsl:when test="position() mod 3 = 0">
                  <xsl:text>&#xA;</xsl:text>
                    <div class="third">
                      <xsl:apply-templates select="."/>
                    </div>
                  </xsl:when>
                  <xsl:otherwise>
                  <xsl:text>&#xA;</xsl:text>
                    <xsl:apply-templates select="."/>
                  </xsl:otherwise>
                </xsl:choose>
          </xsl:if>
         </xsl:for-each>
      </div>
    </xsl:template>

    <xsl:template match="elements">
      <xsl:value-of select="PostDate" />
      <xsl:text> - </xsl:text>
      <xsl:value-of select="name" />
    </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<groups>
    <elements>
        <name>//1</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-03-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//2</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-03-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//3</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-07-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//4</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2009-01-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//5</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2009-02-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//6</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2008-06-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//7</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-01-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//8</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-05-14</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//9</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-05-28</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//10</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-02-25</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//11</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-12-20</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//12</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-11-07</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//13</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-07-09</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//14</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2005-10-19</PostDate>
        <trackno></trackno>
    </elements>
</groups>

产生想要的正确结果:

<div class="item">
2012-07-04 - //3
2012-05-28 - //9
<div class="third">2012-05-14 - //8</div>
2012-03-04 - //1
2012-01-04 - //7
<div class="third">2011-12-20 - //11</div>
2011-11-07 - //12
2011-07-09 - //13
<div class="third">2011-03-04 - //2</div>
2011-02-25 - //10</div>
于 2012-09-07T02:08:07.733 回答
0

我有一段时间没有做过 xsl,所以我的 XSL 有点生疏了。

但是,这应该有效,其中三个对应于每 x 重复的次数

 <xsl:when test="position() mod 3 = 0">
     <!-- your xml render here-->
 </xsl:when>
 <xsl:otherwise><!-- your other xml here --> </xsl:otherwise>

mod 运算符也适用于其他场景

于 2012-09-07T00:26:28.360 回答