2012-05-02 17 views
3

私は、Plone WebサイトのテーマとしてDiazo/XSLTを使用しています。ホームページで、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とジアゾと本当に身近な(そしてより慣れていませんいくつかの既存のテーマを書き直す)しかし、私はいくつかの解決策を試した。

最初は2回でこれを行うことでした。まず「DT」のタグを解析することによって、それぞれの「DD」タグを探し構造を作成し、その更新後:

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

それは正しく構造を作成しますが、私は2番目の部分を記述するのか分かりません。私はそれはあまり意味がありません知っているが、私はあなたがそれのアイデアを得る願ってい

<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をコピーします。

私が考えていた2番目の解決方法は、少し汚れています(どちらも機能しません)。アイデアは、ワンステップでコンテンツを作成することです:

<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 more"リンクに "href"属性を追加するための2番目のステップ)。 よりよい解決策がありますか?

私だけジアゾを使用することを好むだろうが、それはなんとかではない場合、私は直接、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> 
関連する問題