首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT/Diazo:为每一对元素创建元素

XSLT/Diazo:为每一对元素创建元素
EN

Stack Overflow用户
提问于 2012-05-02 10:28:10
回答 1查看 1.3K关注 0票数 3

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

代码语言:javascript
复制
<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>

我想把它变成这样的东西:

代码语言:javascript
复制
<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”标记来创建结构和更新之后:

代码语言:javascript
复制
<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>

它正确地创建了结构,但我不知道如何编写第二部分。这个想法应该是这样的:

代码语言:javascript
复制
<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.

我一直在考虑的第二个解决方案是更脏的(而且也不起作用)。这样做的目的是在一个步骤中创建内容:

代码语言:javascript
复制
<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“标记并在"Read”链接上添加"href“属性的第二步)。有更好的解决方案吗?

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

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-02 12:11:17

这个转换

代码语言:javascript
复制
<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 时(其结构与所提供的结构相同,但具有更多项):

代码语言:javascript
复制
<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>

生成想要的、正确的结果

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10411917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档