使用XSLT以两个为一组将XML元素格式化为HTML

时间:2011-03-30 14:08:11

标签: html xml xslt

我有以下XML数据集(简化),我可以从零到无穷无尽的项目(通常会有2-10个):

<item template="event_element">
   <mytitle>This is the first title</mytitle>
   <mydate>20110330T143004</mytitle>
   <mydescription>This is the first description</mytitle>
   <mylink>www.example.com</mytitle>
</item>
.
.
<item template="event_element">
   <mytitle>This is the Tenth title</mytitle>
   <mydate>20110330T143004</mytitle>
   <mydescription>This is the tenth description</mytitle>
   <mylink>www.example.com</mytitle>
</item>

我的最终结果应该是这样的,我最终会得到几组项目(将通过javascript旋转):

<div class="body">
<div class="rel" id="arrangement">

    // First set of items goes here
    <div class="item">
        <div class="itm">
            <div class="in">
                <p>
                    <a href="MY LINK" title="MY LINK DESCRIPTION">
                        <span>MY DATE</span>
                        <strong>MY FIRST TITLE</strong>
                        MY SHORT DESCRIPTION...
                    </a>
                </p>
            </div>
        </div>
        <div class="itm last">
            <div class="in">
                <p>
                    <a href="MY LINK" title="MY LINK DESCRIPTION">
                        <span>30. mar 2011</span>
                        <strong>MY SECOND TITLE</strong>
                        MY SHORT DESCRIPTION...
                    </a>
                </p>
            </div>
        </div>
        <div class="clr"></div>
    </div>

    // Second set of items goes here
    <div class="item">
        <div class="itm">
            <div class="in">
                <p>
                    <a href="MY LINK" title="MY LINK DESCRIPTION">
                        <span>MY DATE</span>
                        <strong>MY THIRD TITLE</strong>
                        MY SHORT DESCRIPTION...
                    </a>
                </p>
            </div>
        </div>
        <div class="itm last">
            <div class="in">
                <p>
                    <a href="MY LINK" title="MY LINK DESCRIPTION">
                        <span>30. mar 2011</span>
                        <strong>MY FORTH TITLE</strong>
                        MY SHORT DESCRIPTION...
                    </a>
                </p>
            </div>
        </div>
        <div class="clr"></div>
    </div>

    <div class="clr"></div>
</div>

我的问题是单步执行我的项目(甚至应该按照日期desc排序)并将它们分成两组。

就像今天一样,我被迫对这些集合进行硬编码($ eventfolder,$ totalevents变量已经预定义):

<pre><code><div class="body">
  <div class="rel" id="arrangement">
    <xsl:if test="$totalevents &gt; 0">
      <div class="item">
        <xsl:for-each select="$eventfolder/item[@template='event_element' and position() &gt; 0 and position() &lt; 3]">
          <xsl:choose>
            <xsl:when test="position() &lt; $eventsperpage">        
          <div class="itm">
            <xsl:call-template name="renderEvent" />    
              </div>
            </xsl:when>
            <xsl:otherwise>
          <div class="itm last">
            <xsl:call-template name="renderEvent" />    
              </div>
              <div class="clr"></div>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>     
      </div>
    </xsl:if>

    <xsl:if test="$totalevents &gt; 2">
      <div class="item">
        <xsl:for-each select="$eventfolder/item[@template='event_element' and position() &gt; 2 and position() &lt; 5]">
          <xsl:choose>
            <xsl:when test="position() &lt; $eventsperpage">        
          <div class="itm">
            <xsl:call-template name="renderEvent" />    
              </div>
            </xsl:when>
          <xsl:otherwise>
        <div class="itm last">
          <xsl:call-template name="renderEvent" />  
            </div>
            <div class="clr"></div>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>       
    </div>
  </xsl:if>

   <div class="clr"></div>
 </div>
</div>
</code></pre>

renderElement模板只是呈现内部事件HTML。

但是,如果我想显示比2更多的设置 - 或者甚至在每个集合中显示更多项目,那么这种方式不太实用... xslt文件会很大且不可读...

任何有关如何解决此问题的帮助,因为我无法弄清楚如何插入HTML标记,因为XSLT编译器无法看到它们被关闭 - 即。 (当test="position() = 1插入html开始标记时,以及稍后test="position() = X关闭标记时)。

提前致谢

3 个答案:

答案 0 :(得分:1)

关于:

  

任何帮助我如何解决这个问题   问题,因为我无法弄清楚如何   插入HTML标记作为XSLT   编译器看不到它们被关闭 -   即。 (当test="position() = 1插入时   html开始标记然后再开始   test="position() = X关闭标记。)

你正在考虑这个问题。 XSLT生成树,而不是标签。您可以创建元素,而不是元素的一半。

以下样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!--  the number of items to include in each group -->
    <xsl:variable name="group" select="2" />
    <xsl:template match="/">
        <xsl:apply-templates
            select="events/item[@template='event_element']
                               [(position() - 1)  mod $group = 0]" />
    </xsl:template>
    <xsl:template match="item[@template='event_element']" mode="inner">
        <div class="itm">
            <div class="in">
                <p>
                    <a href="{mylink}" title="{mydescription}">
                        <span><xsl:value-of select="mydate" /></span>
                        <strong><xsl:value-of select="mytitle" /></strong>
                        <xsl:value-of select="mydescription" />
                    </a>
                </p>
            </div>
        </div>
    </xsl:template>
    <xsl:template match="item[@template='event_element']">
        <div class="item">
            <xsl:apply-templates
                select=".|following-sibling::item[@template='event_element']
                                                 [position() &lt; $group]"
                mode="inner" />
            <div class="clr" />
        </div>
    </xsl:template>
</xsl:stylesheet>

应用于此输入:

<events>
<item template="event_element">
   <mytitle>This is the first title</mytitle>
   <mydate>20110330T143004</mydate>
   <mydescription>This is the first description</mydescription>
   <mylink>www.example.com</mylink>
</item>
<item template="event_element">
   <mytitle>This is the second title</mytitle>
   <mydate>20110330T143004</mydate>
   <mydescription>This is the tenth description</mydescription>
   <mylink>www.example.com</mylink>
</item>
<item template="event_element">
   <mytitle>This is the third title</mytitle>
   <mydate>20110330T143004</mydate>
   <mydescription>This is the tenth description</mydescription>
   <mylink>www.example.com</mylink>
</item>
<item template="event_element">
   <mytitle>This is the fourth title</mytitle>
   <mydate>20110330T143004</mydate>
   <mydescription>This is the tenth description</mydescription>
   <mylink>www.example.com</mylink>
</item>
<item template="event_element">
   <mytitle>This is the fifth title</mytitle>
   <mydate>20110330T143004</mydate>
   <mydescription>This is the tenth description</mydescription>
   <mylink>www.example.com</mylink>
</item>
</events>

产生以下输出:

<div class="item">
    <div class="itm">
        <div class="in">
            <p>
                <a href="www.example.com" title="This is the first description">
                    <span>20110330T143004</span>
                    <strong>This is the first title</strong>
                    This is the first description
                </a>
            </p>
        </div>
    </div>
    <div class="itm">
        <div class="in">
            <p>
                <a href="www.example.com" title="This is the tenth description">
                    <span>20110330T143004</span>
                    <strong>This is the second title</strong>
                    This is the tenth description
                </a>
            </p>
        </div>
    </div>
    <div class="clr" />
</div>
<div class="item">
    <div class="itm">
        <div class="in">
            <p>
                <a href="www.example.com" title="This is the tenth description">
                    <span>20110330T143004</span>
                    <strong>This is the third title</strong>
                    This is the tenth description
                </a>
            </p>
        </div>
    </div>
    <div class="itm">
        <div class="in">
            <p>
                <a href="www.example.com" title="This is the tenth description">
                    <span>20110330T143004</span>
                    <strong>This is the fourth title</strong>
                    This is the tenth description
                </a>
            </p>
        </div>
    </div>
    <div class="clr" />
</div>
<div class="item">
    <div class="itm">
        <div class="in">
            <p>
                <a href="www.example.com" title="This is the tenth description">
                    <span>20110330T143004</span>
                    <strong>This is the fifth title</strong>
                    This is the tenth description
                </a>
            </p>
        </div>
    </div>
    <div class="clr" />
</div>

通过修改group变量的值来更改要包含在每个组中的项目数。

答案 1 :(得分:1)

在没有涉及您的方法引发的更大问题的情况下,在XSLT中,一次对元素列表进行分组的方法是:

<xsl:apply-templates select="*[(position()-1) mod $n = 0]" mode="group"/>

...将组模式模板应用于当前元素的第一个,第n + 1个,第2个,第1个等子元素。组模式模板如下所示:

<xsl:template match="*" mode="group">
   <group>
      <xsl:apply-templates 
           select=".|following-sibling::*[position() &lt; $n]"/>
   </group>
</xsl:template>

这将创建一个group元素,并在该元素中,将模板应用于当前元素及其后续兄弟姐妹的n-1。

净效应是只要n是正整数,上面将创建包含源树中n个连续元素的group元素。在您的示例中,除其他更改外,您还需要创建div而不是group

答案 2 :(得分:0)

<xsl:template match="items">
<div>
<xsl:apply-template select="item"/>
</div>
</xsl:template>

<xsl:apply-templates match="item">
do stuff here
</xsl:apply-templates>
你使用类似的东西吗?