2011-03-30 68 views
2

我有以下的XML数据集(简体),在那里我可以从零到无穷的物品(一般会有2-10):使用XSLT格式化XML元素转换为HTML两个一组

<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> 

我的问题是通过我的项目步进(应该连按th排序e date 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关闭标记)。

在此先感谢

+0

您可以添加您当前的xslt,以便我们可以更好地提供建议,这听起来像您试图根据列表中的位置创建输出标签,而不是应用模板! – Treemonkey 2011-03-30 14:12:47

+0

't在那里,只有Markdown和OP在一起玩的不错:) – Wrikken 2011-03-30 14:13:53

+0

关于一个方面的说明:如果它是公开的,可能是一件好事,可以将hEvent类/标签添加到搜索引擎优化中。 – Wrikken 2011-03-30 14:15:06

回答

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> 

您使用类似这样的东西吗?

+0

我不是最好的朋友的评论或代码编辑在这里atm;) 但上述代码是在主模板中,它是用xml的根调用,并只为代码中显示的每个事件调用renderEvent模板。 – Barnebarn 2011-03-30 14:19:01

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变量的值。

+0

正确的序列分割方法,但请记住,在XSLT 1.0模式中不允许使用变量/参数引用。 – 2011-03-30 17:11:24

+0

@Alejandro - 我一直忘记这一点。更新以从'match'中删除变量ref。 – 2011-03-30 19:19:16

+0

+1正确答案。 – 2011-03-30 19:41:34

1

没有进入你的做法提出了更大的问题,在XSLT的方式族元素n个元素的列表,在一个时间是这样的:

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

...这将适用于组模式模板添加到当前元素的第一个,第n + 1个,第2n + 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,以及其他更改。

+0

正确。 'position()mod $ n = 1'会更简单。 – 2011-03-30 18:57:32

+0

我总是忘记这么做。 – 2011-03-30 21:44:33