3

我正在使用 Diazo/XSLT 为 Plone 网站设置主题。在主页上,Plone 给了我以下结构:

<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 和 Diazo 不是很熟悉(并且更习惯于重写一些现有主题),但我尝试了一些解决方案。

第一个是分两次执行此操作。首先查找每个“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”属性的第二步)。有没有更好的解决方案?

我宁愿只使用 Diazo,但如果它不可行,我可以直接覆盖 Plone 中的视图(我认为这会更简单,至少我知道该怎么做)

谢谢你的帮助。

4

1 回答 1

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>
于 2012-05-02T12:11:17.947 回答