Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 12th, 2013, 12:07 PM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default Duplication of elements with concat style names (XSLT 1)

Hi Guys,

Running some transform scripts that is runnign through a field lets call it "person" and each person has a ID of 2,3,4,5,6,7. I want some information outputting into a Element with a name of 'Data1' + the ID, so it would output data12,13,14
The next set of information needs the same style but it would be 'Data5' +(ID +2) Any information on this would be great as atm the way i have ahd to programme it is Alot of repeated code.

See below 2 codes i am using.

<xsl:for-each select="$PCQRoot/blahblah">
<xsl:choose>
<xsl:when test="@id='2'">
<data12>
<xsl:choose>
<xsl:when test="AccessToOtherVehicles = 'field1'">D</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'field2'">Y</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field3'">M</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field4'">V</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field5'">B</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field6'">C</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'field7'">N</xsl:when>
</xsl:choose>
</data12>
</xsl:when>

<xsl:when test="@id='3'">
<data13>
<xsl:choose>
<xsl:when test="AccessToOtherVehicles = 'field1'">D</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'field2'">Y</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field3'">M</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field4'">V</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field5'">B</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field6'">C</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'field7'">N</xsl:when>
</xsl:choose>
</data13>

</xsl:when>

<xsl:when test="@id='4'">
<data14>
<xsl:choose>
<xsl:when test="AccessToOtherVehicles = 'field1'">D</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'field2'">Y</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field3'">M</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field4'">V</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field5'">B</xsl:when>
<xsl:when test="AccessToOtherVehicles = ''field6'">C</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'field7'">N</xsl:when>
</xsl:choose>
</data14>

If these ID's went up to 100, this would be alot of undeed code, so I am trying to have this done in a small section as below,

<xsl:for-each select="$PCQRoot/blahblah">
<xsl:element name="'data1',@id">this is my problem
<xsl:choose>
<xsl:when test="AccessToOtherVehicles = 'NDOC'">D</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'OOC'">Y</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'OUM'">M</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'OUV'">V</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'CC'">B</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'CCSU'">C</xsl:when>
<xsl:when test="AccessToOtherVehicles = 'NAOV'">N</xsl:when>
</xsl:choose>
</xsl:element>
</xsl:for-each>
 
Old February 12th, 2013, 12:11 PM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Just to add to this, there could be anything from 2 ID's to 50 ID's and i want it to loop and keep outputting the element(with its concat name) and the data inside this element for each ID untill it hits the last one, then move onto the next section of code.
 
Old February 12th, 2013, 12:17 PM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Brief knockup of the format of xml being put into this

<pc:blahblah xmlns:pc="http://www.blahblah.co.uk/schema/thirdparty/2-8/quote/pc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Policy>
</Policy>
<Proposer id="1">
</Proposer>
<Driver id="2">
<AccessToOtherVehicles>field4</AccessToOtherVehicles>
</Driver>
<Driver id="3">
<AccessToOtherVehicles>field4</AccessToOtherVehicles>
</Driver>
</pc:blahblah>
 
Old February 12th, 2013, 01:53 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think you want
Code:
<xsl:for-each select="//Driver">
  <xsl:element name="Data1{@id}">
      <xsl:copy-of select="node()"/>
   </xsl:element>
</xsl:for-each>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 14th, 2013, 09:30 AM
Authorized User
 
Join Date: Feb 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
Default

That will work a treat. going to go into abit clearer detail now to try and ask a secon quesiton... after the code.
as it stands i have...

Code:
<xsl:choose>
						<xsl:when test="@id='2'">
						<data12>
							<xsl:choose>
								<xsl:when test="AccessToOtherVehicles = 'NDOC'">D</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'OOC'">Y</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'OUM'">M</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'OUV'">V</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'CC'">B</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'CCSU'">C</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'NAOV'">N</xsl:when>		
							</xsl:choose>
						</data12>
						</xsl:when>
						
						<xsl:when test="@id='3'">
						<data13>
							<xsl:choose>
								<xsl:when test="AccessToOtherVehicles = 'NDOC'">D</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'OOC'">Y</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'OUM'">M</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'OUV'">V</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'CC'">B</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'CCSU'">C</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'NAOV'">N</xsl:when>			
							</xsl:choose>
						</data13>
							
						</xsl:when>
						
						<xsl:when test="@id='4'">
						<data14>
							<xsl:choose>
								<xsl:when test="AccessToOtherVehicles = 'NDOC'">D</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'OOC'">Y</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'OUM'">M</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'OUV'">V</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'CC'">B</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'CCSU'">C</xsl:when>
								<xsl:when test="AccessToOtherVehicles = 'NAOV'">N</xsl:when>			
							</xsl:choose>
						</data14>
						</xsl:when>
						<xsl:when test="@id='5'"> 
							<xsl:message terminate="yes">
								Error: They permit a maximum of 3 Additional drivers
							</xsl:message>
						</xsl:when>
					</xsl:choose>
				</xsl:for-each>
I wish to turn this into something using your previous post like...
Code:
<xsl:for-each select="$PCQRoot/Driver">
					<xsl:element name="Data1{@id}">
					    <xsl:when test="AccessToOtherVehicles = 'NDOC'">D</xsl:when>
						<xsl:when test="AccessToOtherVehicles = 'OOC'">Y</xsl:when>
						<xsl:when test="AccessToOtherVehicles = 'OUM'">M</xsl:when>
						<xsl:when test="AccessToOtherVehicles = 'OUV'">V</xsl:when>
						<xsl:when test="AccessToOtherVehicles = 'CC'">B</xsl:when>
						<xsl:when test="AccessToOtherVehicles = 'CCSU'">C</xsl:when>
						<xsl:when test="AccessToOtherVehicles = 'NAOV'">N</xsl:when>	
					</xsl:element>
				</xsl:for-each>
However, the ID's are allways going to be 2,3,4. However the element varies from each template/forEach. in the above case the tags were data12,13,14. so the above worked. however later i need ID 2,3,4 to be output as Data18,19,20. and again as 54,55,56
How would i go about treating the data"XX" as a number and being able to add the ID to it?Can i simply say Data1{@id +6} or Data 16+{@id}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Cant style elements after created the theme folders ftaran BOOK: Beginning ASP.NET 4 : in C# and VB 10 November 4th, 2011 12:54 PM
unique attribute names for form elements fishmonkey XSLT 3 March 16th, 2008 07:46 PM
XSLT For Each - Field Names lahatfield XSLT 6 May 15th, 2007 06:33 AM
Different Font Style For Elements in Drop Down Box kunal_kishan HTML Code Clinic 1 July 11th, 2006 09:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.