XSLT / Diazo: create an element for each pair of elements

I am using Diazo / XSLT to host the Plone website. On the homepage, Plone gives me the following structure:

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

What I want to convert to something like this:

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

I am not very familiar with XSLT and Diazo (and used it more to rewrite some existing topics), but I tried several solutions.

The first was to do it twice. First find the "dd" tags, create a structure and after this update, after analyzing all the "dt" tags:

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

It creates the structure correctly, but I don’t know how to write the second part. The idea would be this:

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

I know this doesn't make much sense, but I hope you understand this:

  • I look at every dd tag

  • , , "h2", , "dd".

  • ( ) "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 ( , , , , )

.

+3
1

:

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

All Articles