1

このxmlのすべてのコメントから降順で並べられた最初の4つのコメント(date_addedで並べ替え)を取得する必要があります。

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="get7Comments.xsl"?>

<products>
<product id="1">
    <comment id="1">
        <username>admin1</username>
        <text>nice</text>
        <date_added>20.06.2005</date_added>
    </comment>
    <comment id="2">
        <username>admin2</username>
        <text>too nice</text>
        <date_added>11.05.2005</date_added>
    </comment>
</product>
<product id="2">
    <comment id="1">
        <username>admin1</username>
        <text>comment1</text>
        <date_added>19.05.2005</date_added>
    </comment>
    <comment id="2">
        <username>daniel</username>
        <text>comment2</text>
        <date_added>06.05.2005</date_added>
    </comment>
    <comment id="3">
        <username>another</username>
        <text>comment3</text>
        <date_added>15.05.2005</date_added>
    </comment>
</product>
</products>

私が欲しい最後の4つのコメントの出力例:

admin1 : nice : 20.06.2005
admin1 : comment1  : 19.05.2005
another : comment3 : 15.05.2005
admin2 : too nice : 11.05.2005

アイテムのリスト-コメント-としてそれらを許可した場合は完全に機能しますが、新しいタグの下でそれらを分離した後は機能しません

<product id=""> comments </product> 

すべてを並べ替えて最初に取得することはできません。4。「product」タグがない場合の仕組み、つまり、すべてのコメントが親の「products」タグとしてあることを意味します。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match ="products">
<xsl:for-each select="product[position() &lt; 8]">

    <xsl:sort select="normalize-space(substring(date_added,7,4))" order="descending" />
    <xsl:sort select="normalize-space(substring(date_added,4,2))" order="descending" />
    <xsl:sort select="normalize-space(substring(date_added,0,2))" order="descending" />
    <xsl:variable name="commID" select="@id" />

    <a href="index.php?p=comment&amp;id={$commID}">
        <xsl:value-of select="substring(text,0,60)" />
    </a><br/>

</xsl:for-each>

 </xsl:template>
 </xsl:stylesheet>
4

1 に答える 1

1

何をしようとしているのか正確にはわかりませんが、のxsl:apply-templates代わりにを使用してみてくださいxsl:for-each

例:

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8"/>

    <xsl:template match ="products">
        <xsl:apply-templates select="product/comment">
            <xsl:sort select="concat(
                normalize-space(substring(date_added,7,4)),
                normalize-space(substring(date_added,4,2)),
                normalize-space(substring(date_added,1,2))
                )" data-type="number" order="descending" />
        </xsl:apply-templates>      
    </xsl:template>

    <xsl:template match="comment">
        <!--xsl:if used because it wasn't working on the predicate?-->
        <xsl:if test="4 >= position()">
            <a href="index.php?p=comment&amp;id={@id}">
                <xsl:value-of select="substring(text,1,60)" />
            </a><br/>
            <xsl:text>&#xA;</xsl:text>                  
        </xsl:if>
    </xsl:template>     

</xsl:stylesheet>

XML入力を使用すると、次のようになります。

<a href="index.php?p=comment&amp;id=1">nice</a><br>
<a href="index.php?p=comment&amp;id=1">comment1</a><br>
<a href="index.php?p=comment&amp;id=3">comment3</a><br>
<a href="index.php?p=comment&amp;id=2">too nice</a><br>

製品間でコメントIDが重複している場合(例のXMLのように)、hrefを変更する必要があります。

(編集:上位4つのコメントを返そうとしていることを見逃しました。修正されました。3番目のタイプミスも修正されましたsubstring()。)

于 2012-06-07T05:56:13.070 に答える