Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 21st, 2011, 05:18 PM
Authorized User
Points: 310, Level: 6
Points: 310, Level: 6 Points: 310, Level: 6 Points: 310, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating Dynamic Elements with Values from Other Element values

I am looking for generic template where it uses an element value into a tag and another element value as a field value.

here is source

Code:
<attributeList count="71">
<attribute>
<name>name1</name>
<string>Value1</string>
</attribute>
<attribute>
<name>name2</name>
<integer>value2</integer>
</attribute>
<attribute>
<name>name3</name>
<boolean>value3</boolean>
</attribute>
<attribute>
<name>name4</name>
<unknown>value4</unknown>
</attribute>
<attribute>
<name>name5</name>
<datetime>value5</datetime>
</attribute>
</attributeList>
output

Code:
<name1>value1<name1>
<name2>value2<name2>
<name3>value3<name3>
<name4>value4<name4>
<name5>value5<name5>

Does this code works? please advice, I have some bugs and testing around at this time
Code:
<xsl:element name="{../attributeList/attribute/name}">
	<xsl:value-of select="../attributeList/attribute/string | ../attribute/integer | ../attribute/boolean | ../attribute/unknown | ../attribute/datetime"/>
	</xsl:element>
Reply With Quote
  #2 (permalink)  
Old February 21st, 2011, 07:34 PM
mhkay's Avatar
Wrox Author
Points: 18,277, Level: 58
Points: 18,277, Level: 58 Points: 18,277, Level: 58 Points: 18,277, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,933
Thanks: 0
Thanked 282 Times in 277 Posts
Default

It's not possible to say what your code does without knowing its context - but I can't think of any context in which it would work, in fact, the code seems to betray a lack of understanding of the importance of context.

This is a very simple bit of XSLT, it just needs

Code:
<xsl:template match="attribute">
  <xsl:element name="{name}">
     <xsl:value-of select="*[2]"/>
  </xsl:element>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old February 22nd, 2011, 12:35 PM
Authorized User
Points: 310, Level: 6
Points: 310, Level: 6 Points: 310, Level: 6 Points: 310, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default the Source with Context

I have previous posting which includes original source of XML structure

XSL Loop with counter and concat

Here is the context

/ematrix/creationProperties/businessObject/attributeList/attribute

here is the part of source below as input

Code:
<attributeList count="32">
<attribute>
<name>Complaint Originator</name>
<string>super13</string>
</attribute>
<attribute>
<name>MPG Complaint Receipt Date</name>
<datetime>2010-08-20T05:00:00Z</datetime>
</attribute>
<attribute>
<name>MPG Complaint Reopen Count</name>
<integer>0</integer>
</attribute>
<attribute>
<name>MPG Complaint TPM Notification Date</name>
<unknown></unknown>
</attribute>
<attribute>
<name>MPG MDC Medical</name>
<boolean>TRUE</boolean>
</attribute>
Here is the output as required
Code:
<Complaint Originator>super13</Complaint Originator>
<MPG Complaint Receipt Date>2010-08-20T05:00:00Z</MPG Complaint Receipt Date>
<MPG Complaint Reopen Count>0</MPG Complaint Reopen Count>
<MPG Complaint TPM Notification Date></MPG Complaint TPM Notification Date>
<MPG MDC Medical>TRUE</MPG MDC Medical>
Hope i am clear this time
Reply With Quote
  #4 (permalink)  
Old February 22nd, 2011, 01:07 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,133
Thanks: 1
Thanked 189 Times in 188 Posts
Default

That 'output' is not valid XML. You cannot have a space in an XML element name.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #5 (permalink)  
Old February 22nd, 2011, 01:13 PM
Authorized User
Points: 310, Level: 6
Points: 310, Level: 6 Points: 310, Level: 6 Points: 310, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just removed the spaces as well to make valid xml.

<ComplaintOriginator>super13</ComplaintOriginator>
<MPGComplaintReceiptDate>2010-08-20T05:00:00Z</MPGComplaintReceiptDate>
<MPGComplaintReopenCount>0</MPGComplaintReopenCount>
<MPGComplaintTPMNotificationDate></MPGComplaintTPMNotificationDate>
<MPGMDCMedical>TRUE</MPGMDCMedical>
Reply With Quote
  #6 (permalink)  
Old February 22nd, 2011, 01:18 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,133
Thanks: 1
Thanked 189 Times in 188 Posts
Default

So was there an actual question - because I'm afraid I don't see one.

You can use the translate() function with Michael's answer above to solve your problem. If this isn't working then you need to let us know why.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #7 (permalink)  
Old February 22nd, 2011, 01:25 PM
Authorized User
Points: 310, Level: 6
Points: 310, Level: 6 Points: 310, Level: 6 Points: 310, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am not very clear on translate from the above post. I tried the code, it's hanging in the middle
Reply With Quote
  #8 (permalink)  
Old February 22nd, 2011, 02:02 PM
Authorized User
Points: 310, Level: 6
Points: 310, Level: 6 Points: 310, Level: 6 Points: 310, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is my initial code, work in progress

Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<xsl:template match="/ematrix/businessObject">
		<xsl:element name="{translate(objectType,' ','')}">

			<Type>
				<xsl:value-of select="objectType"/>
			</Type>
			<Name>
				<xsl:value-of select="objectName"/>
			</Name>
			<revision>
				<xsl:value-of select="objectRevision"/>
			</revision>
			<vault>
				<xsl:value-of select="vaultRef"/>
			</vault>
			<owner>
				<xsl:value-of select="owner/userRef"/>
			</owner>
			<description>
				<xsl:value-of select="description"/>
			</description>
			<originated>
				<xsl:value-of select="creationInfo/datetime"/>
			</originated>
			<modified>
				<xsl:value-of select="modificationInfo/datetime"/>
			</modified>
			<policy>
				<xsl:value-of select="policyRef"/>
			</policy>

			<xsl:for-each select="attributeList/attribute">
			<xsl:call-template name="attribute"/>
			</xsl:for-each>
		</xsl:element>
	</xsl:template>
	<xsl:template name="attribute">
		<xsl:element name="{name()}">
			<xsl:value-of select="*[2]"/>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>
Output not as desired

Code:
<?xml version='1.0' ?>
V6R2009x.HF29(13.0.0.0)2011-02-17T02:41:20ZexporteMatriXML version V6R2008-1.0<ADDComplaint><Type xmlns:xsd="http://www.w3.org/2001/XMLSchema">ADD Complaint</Type><Name xmlns:xsd="http://www.w3.org/2001/XMLSchema">Ticket Eight 00001</Name><revision xmlns:xsd="http://www.w3.org/2001/XMLSchema">1</revision><vault xmlns:xsd="http://www.w3.org/2001/XMLSchema">ADD</vault><owner xmlns:xsd="http://www.w3.org/2001/XMLSchema">ADDsuper3</owner><description xmlns:xsd="http://www.w3.org/2001/XMLSchema"/><originated xmlns:xsd="http://www.w3.org/2001/XMLSchema">2011-02-12T11:13:06Z</originated><modified xmlns:xsd="http://www.w3.org/2001/XMLSchema">2011-02-12T11:13:46Z</modified><policy xmlns:xsd="http://www.w3.org/2001/XMLSchema">MPG Complaint</policy><attribute>ADDsuper3</attribute><attribute/><attribute>4 Low</attribute><attribute>Yes</attribute><attribute/><attribute/><attribute/><attribute/><attribute>US</attribute><attribute/><attribute>2010-08-20T05:00:00Z</attribute>
Making progress, please let me know if you have any inputs
Reply With Quote
  #9 (permalink)  
Old February 22nd, 2011, 03:03 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,133
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It should be <xsl:element name="{name}"> and not <xsl:element name="{name()}">.

The former picks the text of the child element called 'name' (i.e. <name>) whereas the latter outputs the name of the current element (i.e. <attribute>).

Use the translate function exactly as you have above, i.e. translate(name,' ','').

If you get rid of the xsd namespace declaration as well that will clean up your output.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #10 (permalink)  
Old February 22nd, 2011, 03:43 PM
Authorized User
Points: 310, Level: 6
Points: 310, Level: 6 Points: 310, Level: 6 Points: 310, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is my updated code.

The attributeList/attribute is working. The next level set is not working. Can someone take a look at it, the for each takes place but not finding the values at each context.

Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<xsl:template match="/ematrix/businessObject">
		<xsl:element name="{translate(objectType,' ','')}">
			<Type>
				<xsl:value-of select="objectType"/>
			</Type>
			<Name>
				<xsl:value-of select="objectName"/>
			</Name>
			<revision>
				<xsl:value-of select="objectRevision"/>
			</revision>
			<vault>
				<xsl:value-of select="vaultRef"/>
			</vault>
			<owner>
				<xsl:value-of select="owner/userRef"/>
			</owner>
			<description>
				<xsl:value-of select="description"/>
			</description>
			<originated>
				<xsl:value-of select="creationInfo/datetime"/>
			</originated>
			<modified>
				<xsl:value-of select="modificationInfo/datetime"/>
			</modified>
			<policy>
				<xsl:value-of select="policyRef"/>
			</policy>

			<xsl:for-each select="attributeList/attribute">
				<xsl:call-template name="attribute"/>
			</xsl:for-each>
			<states>
				<xsl:for-each select="stateList/state">

					<xsl:element name="state">
						<xsl:element name="name">
							<xsl:value-of select="name"/>
						</xsl:element>
						<xsl:element name="current">
							<xsl:value-of select="stateStatus"/>
						</xsl:element>
						<xsl:element name="actual">
							<xsl:value-of select="actualInfo/datetime"/>
						</xsl:element>
						<xsl:element name="start">
							<xsl:value-of select="startDate/datetime"/>
						</xsl:element>
						<xsl:element name="end">
							<xsl:value-of select="endDate/datetime"/>
						</xsl:element>
						<xsl:element name="duration">
							<xsl:value-of select="stateDuration"/>
						</xsl:element>
					</xsl:element>
				</xsl:for-each>
			</states>
		</xsl:element>
	</xsl:template>
	<xsl:template name="attribute">
		<xsl:element name="{translate(name,' ','')}">
			<xsl:value-of select="*[2]"/>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Swapping values between Element Name and documentation chilly XSLT 4 February 8th, 2011 01:09 PM
Insert multiple elements based on values of others fixit XSLT 14 September 25th, 2009 06:36 AM
convert list of ';' separated values --> elements Condor76 XSLT 1 June 11th, 2007 03:56 PM
HTML table with missing values (elements) go4java XSLT 6 July 10th, 2006 04:31 AM
xml element values vakorde XSLT 3 May 2nd, 2006 05:36 AM



All times are GMT -4. The time now is 10:55 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.