2012-05-02 24 views
3

我使用Diazo/XSLT来主题Plone网站。在主页上,Plone的给了我以下结构:XSLT/Diazo:为每对元素创建元素

<dl> 
    <dt> 
    <a href="...">The news item title</a> 
    </dt> 
    <dd> 
    The content of the news item 
    </dd> 
    (and so on for the following ones) 
</dl> 

,我要变成这样的事情:

<div id="news_items"> 
    <div> 
    <h2><a href="...">The news item title</a></h2> 
    The content of the news item. 
    <a class="readmore" href="<the HREF taken from the dt/a tag)">Read more</a> 
    </div> 
    (and so on for the following items) 
</div> 

我不是很熟悉XSLT和重氮(多用来重写一些现有的主题),但我尝试了一些解决方案。

第一个是两次做这个。每个“DD”标签首先来看,造成结构和更新通过解析所有的“DT”标签后:

<copy css:theme="#news_items"> 
    <xsl:for-each css:select="#content-core dl dd"> 
    <xsl:element name="div"> 
     <xsl:element name="h2"> 
     </xsl:element> 
     <xsl:copy-of select="./*" /> 
     <xsl:element name="a"> 
     <xsl:attribute name="class">readmore</xsl:attribute> 
     Read more 
     </xsl:element> 
    </xsl:element> 
    </xsl:for-each> 
</copy> 

它正确地创建结构,但我不知道怎么写的第二部分。这个想法是这样的事情:

<xsl:for-each css:select="#content-core dl dt"> 
    <!-- Copy the link in the 'dd' tag into the h2 tag, based on the position --> 
    <copy css:theme="#news_item div:nth-child(position()) h2" css:select="a" /> 

    <!-- Copy the a tag's href in the 'Read more' tag --> 
    <copy css:theme="#news_item div:nth-child(position()) a.readmore"> 
    <xsl:attribute name="class"> 
     <xsl:value-of select="@class" /> 
    </xsl:attribute> 
    </copy> 
</xsl:for-each> 

我知道它没有那么多道理,但我希望你得到它的想法:

  1. 我看着每一个“DD”标签

  2. 我发现,根据循环中的位置,在上一步创建的'h2'标签,我复制“dd”标签内的链接。

  3. 我发现(基于位置再次)“a.readmore”标记并复制href。

我一直在想的第二种解决方案有点肮脏(也不管用)。这个想法是在一个步骤中创建的内容:

<xsl:for-each css:select="#content-core dl > *"> 
    <xsl:if test="name = dt"> 
    <!-- That the dt tag, we start the 'div' tag and add the h2 tag --> 
    </xsl:if> 

    <xsl:if test="name = dt"> 
    <!-- That the dt tag, we copy the content and close the 'div' tag --> 
    </xsl:if> 
</xsl:foreach> 

但我真的不喜欢的解决方案(我不明白如何创建“阅读更多”链接)。

您认为第一个解决方案可以完成吗? (尤其是第二步填充“h2”标签并在“阅读更多”链接上添加“href”属性)。 有没有更好的解决方案?

我宁愿只用重氮,但如果它是不可行的,我可以直接覆盖在Plone的观点(这将是简单的,我认为,至少我知道该怎么做)

谢谢您帮帮我。

回答

2

该转化

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

<xsl:template match="/*"> 
    <div id="news_items"> 
     <xsl:apply-templates select="dt"/> 
    </div> 
</xsl:template> 

<xsl:template match="dt"> 
    <div> 
    <h2><xsl:copy-of select="a"/></h2> 
    <xsl:apply-templates select="following-sibling::dd[1]"/> 
    <a class="readmore" href="{a/@href}">Read more</a> 
    </div> 
</xsl:template> 
</xsl:stylesheet> 

当在下面的XML文档施加(具有相同的结构,所提供的一个,但具有多个项目):

<dl> 
    <dt> 
     <a href="someUrl1">The news item1 title</a> 
    </dt> 
    <dd> 
     The content of the news item1 
    </dd> 
    <dt> 
     <a href="someUrl2">The news item2 title</a> 
    </dt> 
    <dd> 
     The content of the news item2 
</dd> 
</dl> 

产生想要的,正确的结果

<div id="news_items"> 
    <div> 
     <h2> 
     <a href="someUrl1">The news item1 title</a> 
     </h2> 
     The content of the news item1 
    <a class="readmore" href="someUrl1">Read more</a> 
    </div> 
    <div> 
     <h2> 
     <a href="someUrl2">The news item2 title</a> 
     </h2> 
     The content of the news item2 
<a class="readmore" href="someUrl2">Read more</a> 
    </div> 
</div>