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 July 28th, 2010, 03:27 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Based on the example XML you have given us the following give us the output you have specified. Hopefully this will help you with your program:

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	
	  <xsl:template match="BO">
		  <xsl:value-of select="ObjectName"/>
      <xsl:text>,</xsl:text>
		  	<xsl:for-each select="AttributeList/Attribute">
		  	    <xsl:apply-templates select="."/>
		  	    <xsl:if test="position()!=last()">,</xsl:if>
		  		</xsl:for-each>
	  </xsl:template>

	  <xsl:template match="Attribute">
		  	<xsl:value-of select="string | datetime | boolean"/>
	  </xsl:template>

</xsl:stylesheet>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 28th, 2010, 09:10 AM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the code. It might work. I still need to try that

Meanwhile i wrote the following and had some issues while looping. I am unable to retain the variables that i am passing when i am incrementing the counter. Any suggestions

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xsl">

	<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>

	<xsl:template match="/">
		<xsl:for-each select="/ematrix/businessObject">
			<xsl:variable name="bO" select="./objectType"/>
			<xsl:variable name="Comp">
				<xsl:choose>
					<xsl:when test="$bO = 'MPG Complaint'">
						<i ref="1">MPG Complaint Priority</i>
						<i ref="2">MPG Complaint Complainant Requested Reply</i>
						<i ref="3">Complaint Originator</i>
					</xsl:when>
					<xsl:when test="$bO ='MPG Complaint Contact'">
						<i ref="1">Complaint Contact Account Number</i>
					</xsl:when>
				</xsl:choose>
			</xsl:variable>

			<xsl:variable name="attrList" select="attributeList/attribute"/>			
			<xsl:variable name="varCount" select="count(attributeList/attribute/*)"/>
			
			<xsl:choose>
				<xsl:when test="$bO = 'MPG Complaint Contact'">
						<xsl:call-template name="attrout">
							<xsl:with-param name="counter" select="1"/>
							<xsl:with-param name="varCount" select="$varCount"/>
							<xsl:with-param name="Comp" select="$Comp"/>
							<xsl:with-param name="attrList" select="$attrList"/>				
						</xsl:call-template>
				</xsl:when>
			</xsl:choose>
			

		</xsl:for-each>
	</xsl:template>
				<xsl:template name="attrout">
				<xsl:param name="counter"/>
				<xsl:param name="varCount"/>
				<xsl:param name="Comp" />
				<xsl:param name="attrList" />								

				<xsl:if test="$counter &lt; $varCount">
									
							<xsl:variable name="attrValue" select="$Comp/i[@ref=$counter]"/>
							
							<xsl:value-of select="$attrList[name=$attrValue]/string | $attrList[name=$attrValue]/datetime | $attrList[name=$attrValue]/boolean"/>
							
							<xsl:call-template name="attrout">
								<xsl:with-param name="counter" select="$counter + 1"/>
								<!--<xsl:with-param name="varCount" select="$varCount"/>
								<xsl:with-param name="Comp" select="$Comp"/>
								<xsl:with-param name="attrList" select="$attrList"/>	-->
							</xsl:call-template>
						
				</xsl:if>
			</xsl:template>

</xsl:stylesheet>
 
Old July 28th, 2010, 09:30 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Again, you've shown us some XSLT, told us 'it doesn't work' but you haven't shown us what XML input you are using, nor what you want the output to look like.

With this level of information we really can't help more than we have already.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 28th, 2010, 09:35 AM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, i had previously posted all the source and target, I am trying to refer the same here
Original Source
XSL Loop with counter and concat

Target is writing each field out based on attributelist/attribute/name matches with the variables i am defining above as an array

the main problems is passing the variable name as constant, because this information changes for each object type.

Sample Source
-----------------------
XSL Loop with counter and concat

Output text
----------------------
First,valuestring1,valuedatetime2,valueboolean3
Second,valuestring1,valuedatetime2,valueboolean3

Last edited by chilly; July 28th, 2010 at 09:37 AM..
 
Old July 28th, 2010, 09:52 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The above XSLT refers to elements in your original source XML, but we still don't know what output you are expecting from this input.

I have already provided you with a XSLT transformation for your sample input into your sample output.

If you remove the <xsl:when> clause around the call to <xsl:call-template> and uncomment the parameters in the second call then it does output something - this might be what you want, but I really don't know.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 28th, 2010, 09:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I am unable to retain the variables that i am passing when i am incrementing the counter.

I don't know what you mean. Does this code exhibit this problem, and if so, how does it manifest itself? How does the behaviour of this code differ from what you expected or wanted it to do?

Your most recent response that you have posted the data earlier in the thread is not very helpful. Your latest XSLT code refers to i/@ref, and none of the data you have shown us has an element called i or an attribute called ref. You've also changed element names from BusinessObject to BO, and many other similar things. You're not making it easy for us to help you.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old July 28th, 2010, 11:07 AM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

samjudson
I tried removing when clause and uncommenting the params in the second call, but i am still not seeing the variables retaining the earlier values when it comes to the second iteration after passing t

Code:
<xsl:when test="$bO = 'MPG Complaint'">
						<i ref="1">MPG Complaint Priority</i>
						<i ref="2">MPG Complaint Complainant Requested Reply</i>
						<i ref="3">Complaint Originator</i>
mhkay
i defined the i ref in a variable at starting of XSLT, something like this
----------------
<xsl:when test="$bO = 'MPG Complaint'">
<i ref="1">MPG Complaint Priority</i>
<i ref="2">MPG Complaint Complainant Requested Reply</i>
<i ref="3">Complaint Originator</i>
 
Old July 28th, 2010, 11:11 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

"After passing t"

Did you finish this sentence, as it makes no sense.

The following is your XSLT with the items I mentioned changed. If this doesn't output what you want then you MUST tell us exactly what you are hoping to see as you still have not told us:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xsl">

	<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>

	<xsl:template match="/">
		<xsl:for-each select="/ematrix/businessObject">
			<xsl:variable name="bO" select="./objectType"/>
			<xsl:variable name="Comp">
				<xsl:choose>
					<xsl:when test="$bO = 'MPG Complaint'">
						<i ref="1">MPG Complaint Priority</i>
						<i ref="2">MPG Complaint Complainant Requested Reply</i>
						<i ref="3">Complaint Originator</i>
					</xsl:when>
					<xsl:when test="$bO ='MPG Complaint Contact'">
						<i ref="1">Complaint Contact Account Number</i>
					</xsl:when>
				</xsl:choose>
			</xsl:variable>

			<xsl:variable name="attrList" select="attributeList/attribute"/>			
			<xsl:variable name="varCount" select="count(attributeList/attribute/*)"/>
			
						<xsl:call-template name="attrout">
							<xsl:with-param name="counter" select="1"/>
							<xsl:with-param name="varCount" select="$varCount"/>
							<xsl:with-param name="Comp" select="$Comp"/>
							<xsl:with-param name="attrList" select="$attrList"/>				
						</xsl:call-template>
			

		</xsl:for-each>
	</xsl:template>
				<xsl:template name="attrout">
				<xsl:param name="counter"/>
				<xsl:param name="varCount"/>
				<xsl:param name="Comp" />
				<xsl:param name="attrList" />								

				<xsl:if test="$counter &lt; $varCount">
									
							<xsl:variable name="attrValue" select="$Comp/i[@ref=$counter]"/>
							
							<xsl:value-of select="$attrList[name=$attrValue]/string | $attrList[name=$attrValue]/datetime | $attrList[name=$attrValue]/boolean"/>
							
							<xsl:call-template name="attrout">
								<xsl:with-param name="counter" select="$counter + 1"/>
								<xsl:with-param name="varCount" select="$varCount"/>
								<xsl:with-param name="Comp" select="$Comp"/>
								<xsl:with-param name="attrList" select="$attrList"/>
							</xsl:call-template>
						
				</xsl:if>
			</xsl:template>

</xsl:stylesheet>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 28th, 2010, 11:27 AM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It works. But not sure why it didn't work earlier. It looks same.

Thanks to samjudson and mhkay. Appreciate your help.

If you guys ever push me this far, i wouldn't have made it to where i am right now.

I really appreciate your patience and help.

Last edited by chilly; July 28th, 2010 at 11:36 AM..
 
Old July 28th, 2010, 11:34 AM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I made this code little more efficient in order to skip the template if the $Comp value is empty

Code:
<xsl:if test="$Comp != ''">
<xsl:call-template name="attrout">
        <xsl:with-param name="counter" select="1"/>
	<xsl:with-param name="varCount" select="$varCount"/>
	<xsl:with-param name="Comp" select="$Comp"/>
	<xsl:with-param name="attrList" select="$attrList"/>				
</xsl:call-template>
</xsl:if>





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to loop through a HashMap in XSL? sunilnk XSLT 6 April 10th, 2017 04:09 AM
help: how can I break from xsl for-each loop ? back2grave XSLT 5 May 31st, 2012 12:03 PM
xsl:for-each loop Tomi XSLT 3 September 7th, 2006 05:35 AM
Loop in XSL NEO1976 XSLT 31 September 5th, 2006 03:39 AM
xsl template concat problem nambati XSLT 5 January 31st, 2005 03:43 PM





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