每隔3个XSLT插入一个类

时间:2012-09-07 00:06:20

标签: xml xslt

我想我只是打开一个新问题,也可以帮助那些寻求解决方案的人。我想从每个第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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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运算符也适用于其他场景