0

Let say that i have the current xml document


<root>

<child>
<name> </name>
<lastname> </lastname>
</child>

<child>
<name> </name>
<lastname> </lastname>
</child>

<child>
<name> </name>
<lastname> </lastname>
</child>
...
</root>


I use <xsl:for-each select="root/child">

and then type
<ul>
<li> <xsl:value-of select="name"/> <xsl:value-of select="lastname"/> </li>
</ul>


The problem is that i want to place another <li>something</li> tag which have to appear
on every 3 <child> tags and separate the list that way.
So if i have 16 <child> tags that mean i have to add 5 <li> tags between them.
Please how to do that?????

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by xml_looser
0
<?xml version="1.0"?>
<root>
	<child>
		<name>1</name>
		<lastname>a</lastname>
	</child>
	<child>
		<name>2</name>
		<lastname>b</lastname>
	</child>
	<child>
		<name>3</name>
		<lastname>c</lastname>
	</child>
	<child>
		<name>4</name>
		<lastname>d</lastname>
	</child>

	<child>
		<name>5</name>
		<lastname>e</lastname>
	</child>
	<child>
		<name>6</name>
		<lastname>f</lastname>
	</child>
	<child>
		<name>7</name>
		<lastname>g</lastname>
	</child>
	<child>
		<name>8</name>
		<lastname>h</lastname>
	</child>
	<child>
		<name>9</name>
		<lastname>i</lastname>
	</child>
	<child>
		<name>10</name>
		<lastname>j</lastname>
	</child>
	<child>
		<name>11</name>
		<lastname>k</lastname>
	</child>
	<child>
		<name>12</name>
		<lastname>l</lastname>
	</child>
	<child>
		<name>13</name>
		<lastname>m</lastname>
	</child>
	<child>
		<name>14</name>
		<lastname>n</lastname>
	</child>
	<child>
		<name>15</name>
		<lastname>o</lastname>
	</child>
	<child>
		<name>16</name>
		<lastname>p</lastname>
	</child>
</root>

Template loop

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output indent="yes" method="html"/>
	<xsl:param name="var" select="5"/>
	<xsl:template match="/">
		<html>
			<body>
				<xsl:apply-templates select="root"/>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="root">
		<xsl:call-template name="child">
			<xsl:with-param name="allchild" select="child::child"/>
		</xsl:call-template>
	</xsl:template>
	<xsl:template name="child">
		<xsl:param name="allchild"/>
		<xsl:param name="ak" select="0"/>
		<xsl:if test="$ak &lt; count($allchild)">
                <!-- rekursion break -->
			<xsl:choose>
				<xsl:when test="$ak mod $var = 0">
                                <!-- number of ul child tags -->
					<ul>
						<li>
							<xsl:value-of select="concat(normalize-space($allchild[$ak +1]/name),' ',normalize-space($allchild[$ak +1]/lastname))"/>
						</li>
						<xsl:if test="($ak +1) mod $var != 0">
							<xsl:call-template name="child">
								<xsl:with-param name="ak" select="$ak +1"/>
								<xsl:with-param name="allchild" select="$allchild"/>
							</xsl:call-template>
						</xsl:if>
					</ul>
					<hr/>
					<xsl:call-template name="child">
						<xsl:with-param name="ak" select="$ak + $var"/>
						<xsl:with-param name="allchild" select="$allchild"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:when test="$ak mod $var != 0">
                                        <!-- $var = 3 loop 2 times -->
                                        <!-- $var = 5 loop 4 times -->
					<xsl:call-template name="loop">
						<xsl:with-param name="ak" select="$ak"/>
						<xsl:with-param name="allchild" select="$allchild"/>
					</xsl:call-template>
				</xsl:when>
			</xsl:choose>
		</xsl:if>
	</xsl:template>
	<xsl:template name="loop">
		<xsl:param name="ak"/>
		<xsl:param name="allchild"/>
		<xsl:param name="loop" select="1"/>
		<xsl:if test="$loop &lt; $var">
			<li>
				<xsl:value-of select="concat(normalize-space($allchild[$ak + $loop]/name),' ',normalize-space($allchild[$ak + $loop]/lastname))"/>
			</li>
			<xsl:call-template name="loop">
				<xsl:with-param name="ak" select="$ak"/>
				<xsl:with-param name="allchild" select="$allchild"/>
				<xsl:with-param name="loop" select="$loop + 1"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

$var=5

<html>
  <body>
    <ul>
      <li>1 a</li>
      <li>2 b</li>
      <li>3 c</li>
      <li>4 d</li>
      <li>5 e</li>
    </ul>
    <hr>
    <ul>
      <li>6 f</li>
      <li>7 g</li>
      <li>8 h</li>
      <li>9 i</li>
      <li>10 j</li>
    </ul>
    <hr>
    <ul>
      <li>11 k</li>
      <li>12 l</li>
      <li>13 m</li>
      <li>14 n</li>
      <li>15 o</li>
    </ul>
    <hr>
    <ul>
      <li>16 p</li>
    </ul>
    <hr>
  </body>
</html>

see picture ul with 5 elements and ul with 3 elements

Attachments list3.jpg 37.99 KB list5.jpg 40.68 KB
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.