1

プレ

私はこのXMLを持っています

<root>
    <subscribers nullDays="5">
        <subscriber>
            <ThumbURL>
                http://cs323321.userapi.com/v323321550/eea/iakdB20fx20.jpg
            </ThumbURL>
        </subscriber>
        <subscriber>
            <ThumbURL>
                http://cs323321.userapi.com/v323321550/f24/CQ-Zm0_BWnQ.jpg
            </ThumbURL>
        </subscriber>
        ...
        <subscriber>
            <ThumbURL>...</ThumbURL>
        </subscriber>
    </subscribers>
</root>

XSLT の後、サブスクライバーがimgタグを含む各divに分割されるHTML を取得します。

質問

xsl:for-eachを使用してdiv equals nullDays属性を生成するにはどうすればよいですか? 次のコードには、 nullDaysよりも多くのサブスクライバーノードが必要ですが、存在することはできません。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">    
<xsl:output encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"xml:base="http://www.w3.org/1999/xhtml" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" indent="yes"/>

<xsl:template match="root">
    <xsl:element name="html">
        <xsl:element name="head"/>
        <xsl:element name="body">

            <xsl:variable name="nullDays" select="subscribers/@nullDays"/>                

            <xsl:for-each select="subscribers/subscriber">
                <xsl:if test="(position() mod $nullDays) = 0">
                    <xsl:element name="div">
                        <xsl:attribute name="class">empty-div</xsl:attribute>
                    </xsl:element>
                </xsl:if>
            </xsl:for-each>
        </xsl:element>
    </xsl:element>        
</xsl:template>
</xsl:stylesheet>

ありがとうございました!

4

1 に答える 1

1

この XSLT 1.0 変換:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="subscribers">
     <html>
       <xsl:apply-templates select="subscriber"/>
       <xsl:call-template name="genEmpty">
         <xsl:with-param name="pTimes" select="@nullDays - count(subscriber)"/>
       </xsl:call-template>
     </html>
 </xsl:template>

 <xsl:template match="subscriber">
   <div><img src="{normalize-space(ThumbURL)}"/></div>
 </xsl:template>

 <xsl:template name="genEmpty">
  <xsl:param name="pTimes" select="0"/>

  <xsl:if test="$pTimes >= 0">
    <div class="empty-div"/>
    <xsl:call-template name="genEmpty">
     <xsl:with-param name="pTimes" select="$pTimes -1"/>
    </xsl:call-template>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

提供された XML ドキュメントに適用した場合:

<root>
    <subscribers nullDays="5">
        <subscriber>
            <ThumbURL>
                http://cs323321.userapi.com/v323321550/eea/iakdB20fx20.jpg
            </ThumbURL>
        </subscriber>
        <subscriber>
            <ThumbURL>
                http://cs323321.userapi.com/v323321550/f24/CQ-Zm0_BWnQ.jpg
            </ThumbURL>
        </subscriber>
        ...
        <subscriber>
            <ThumbURL>...</ThumbURL>
        </subscriber>
    </subscribers>
</root>

(私が推測する) 必要な結果が生成されます。

<html>
   <div><img src="http://cs323321.userapi.com/v323321550/eea/iakdB20fx20.jpg"></div>
   <div><img src="http://cs323321.userapi.com/v323321550/f24/CQ-Zm0_BWnQ.jpg"></div>
   <div><img src="..."></div>
   <div class="empty-div"></div>
   <div class="empty-div"></div>
   <div class="empty-div"></div>
</html>
于 2013-01-02T13:32:45.530 に答える