1
votes

Pre

I have this 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>

After XSLT I get HTML where subscriber will be separated into each div with img tag inside.

Question

How I can generate div equals nullDays attribute using xsl:for-each? Following code requires subscriber nodes more than nullDays, but it cannot be exist:

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

Thank you!

1
Can you show your complete XSLT code? At a quick glance this looks O.K. to me, the problem might be that your current node isn't <subscribers> at the point the above code takes effect. (Also, the desired output does usually help a lot.)Thomas W
@thomas-w If number of <subscriber> less than subscribers/@nullDays this for-each doesn't prints <div class="empty-div"></div> subscribers/@nullDays+1 timesdenisldaur
Are you saying if you have 2 subscribers in your XML, you want to output those 2 subscribers, along with 3 empty dives, to make the total to 5? Or something else? It might help if you show your expected output. Thanks!Tim C

1 Answers

1
votes

This XSLT 1.0 transformation:

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

when applied on the provided XML document:

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

produces (what I guess is) the wanted result:

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