2 Replies - 1022 Views - Last Post: 16 April 2014 - 09:30 AM Rate Topic: -----

#1 tront  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-April 14

looping over child elements in xslt

Posted 14 April 2014 - 12:10 AM

i am trying to iteratve over parents child elements and each childs elements using xslt

parent
-----child1
--------child1 child1
--------child1 child2

-----child2
------child2 child1
------child2 child2
the problem is the xml i am trying to navigate is poorly design xml so i dont really know how to get all the child elements elements.
here is my xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>

<teams>
      <team>
         <name>barcelona</name>
         <id>x</id>
         <player1>xavi</player1>
         <player2>messi</player2>
         <player3>iniesta</player3>
          ........................
      </team>
      <team>
         <name>real madrid</name>
         <id>y</id>
         <player1>cristiano ronaldo</player1>
         <player2>modric</player2>
         <player3>bale</player3>
         .............................
      </team>
....................................................
</teams>




and her is my xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/">
	<teams>	
		<xsl:for-each select="teams/team">
			<team>	
						<name><xsl:value-of select="name"/><xsl:text> </xsl:text></name>
						<id><xsl:value-of select="id"/><xsl:text> </xsl:text></id>					
				<players>				
					<xsl:for-each select="//team/*">
						<xsl:choose>
								<xsl:when test="name()='name'">
								</xsl:when>
								<xsl:when test="name()='id'">
								</xsl:when>
								<xsl:otherwise>				
											<player>
												<name><xsl:value-of select="current()"/><xsl:text> </xsl:text></name>
											</player>						
								</xsl:otherwise>
							</xsl:choose>
					</xsl:for-each>
				</players>
			</team>
		</xsl:for-each>
	</teams>
</xsl:template> 
</xsl:stylesheet>




my issue is i get all the children of both teams where i want one set of children in each team. if you run my code you will see what i mean.

the problem is [<xsl:for-each select="//team/*">[/b] because it returns all the children of team

sorry for the long read

Is This A Good Question/Topic? 0
  • +

Replies To: looping over child elements in xslt

#2 tront  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-April 14

Re: looping over child elements in xslt

Posted 14 April 2014 - 12:18 AM

this is the output i would want
<teams>
<team>
<name>barcelona</name>
<id>x</id>
<players>
<player>
<name>xavi</name>
</player>
<player>
<name>messi</name>
</player>
<player>
<name>iniesta</name>
</player>
</players>
........................
</team>
<team>
<name>real madrid</name>
<id>y</id>
<players>
<player>
<name>cristiano ronaldo</name>
</player>
<player>
<name>modric</name>
</player>
<player>
<name>bale</name>
</player>
</players>
.............................
</team>
....................................................
</teams>

but this is what i am getting.
<teams>
      <team>
         <name>barcelona</name>
         <id>x</id>
		 <players>
			<player>
				<name>xavi</name>
			</player>
			 <player>
				<name>messi</name>
			 </player>
			 <player>
				<name>iniesta</name>
			 </player>
			 <player>
				<name>cristiano ronaldo</name>
			</player>
			 <player>
				<name>modric</name>
			 </player>
			 <player>
				<name>bale</name>
			 </player>
			 
		</players>
          ........................
      </team>
      <team>
         <name>real madrid</name>
         <id>y</id>
         <players>
			<player>
				<name>xavi</name>
			</player>
			 <player>
				<name>messi</name>
			 </player>
			 <player>
				<name>iniesta</name>
			 </player>
			<player>
				<name>cristiano ronaldo</name>
			</player>
			 <player>
				<name>modric</name>
			 </player>
			 <player>
				<name>bale</name>
			 </player>
		</players>
         .............................
      </team>
....................................................
</teams>



Was This Post Helpful? 0
  • +
  • -

#3 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: looping over child elements in xslt

Posted 16 April 2014 - 09:30 AM

Quote

<xsl:for-each select="//team/*">


This selects all child elements for every team under the root which is why you are getting all the players. For your desired output, you only need the child elements of the current team of your loop. You can also loop on the specific elements you are interested in which would eliminate the need for the "choose". The loop should just be

<xsl:for-each select="players/player"><!--This loops on all players of the current team-->
	<player>
		<name><xsl:value-of select="current()" /></name>
	</player>
</xsl:for-each>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1