RE: [xsl] Problem writing XSLT

Subject: RE: [xsl] Problem writing XSLT
From: "Jason" <estudio@xxxxxxxxx>
Date: Fri, 13 Jun 2003 13:57:20 +0800
Pls try this
------------------------------------------------------------------------
-------------------------------------
XML file:
<!-- edited with XMLSPY v5 rel. 2 U (http://www.xmlspy.com) by Jason
(GRT) -->
<recordset>
	<trace>
		<LEVEL>FLOW</LEVEL>
		<TIME>2003-05-06 09:57:59:9567238</TIME>
		<TICKS>631878118799567238</TICKS>
		<COMPONENT>NS</COMPONENT>
		<SUBCOMPONENT>NS_SVC</SUBCOMPONENT>
		<ENTRY type="ProcEntry">
			<Datum name="Method">Init</Datum>
		</ENTRY>
	</trace>
	<trace>
		<LEVEL>FLOW</LEVEL>
		<TIME>2003-05-06 09:58:00:2830701</TIME>
		<TICKS>631878118802830701</TICKS>
		<COMPONENT>NS</COMPONENT>
		<SUBCOMPONENT>NS_SVC</SUBCOMPONENT>
		<ENTRY type="ProcEntry">
			<Datum name="Method">InitMgmt</Datum>
		</ENTRY>
	</trace>
	<trace>
		<LEVEL>FULL</LEVEL>
		<TIME>2003-05-06 09:58:00:2830701</TIME>
		<TICKS>631878118802830701</TICKS>
		<COMPONENT>NS</COMPONENT>
		<SUBCOMPONENT>NS_SVC</SUBCOMPONENT>
		<ENTRY type="ProcEntry">
			<Datum name="Adapter">SA</Datum>
			<Datum name="Method">ServiceabilityAgent</Datum>
		</ENTRY>
	</trace>
</recordset>
------------------------------------------------------------------------
-------------------------------------
XSLT file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
	<xsl:template match="/">
		<query>
			<xsl:for-each select="//trace">
				<xsl:variable name="Columns">
					<xsl:call-template
name="getValues">
						<xsl:with-param name="V"
select="."/>
						<xsl:with-param name="T"
select="'Columns'"/>
					</xsl:call-template>
				</xsl:variable>
				<xsl:variable name="Values">
					<xsl:call-template
name="getValues">
						<xsl:with-param name="V"
select="."/>
						<xsl:with-param name="T"
select="'Values'"/>
					</xsl:call-template>
				</xsl:variable>
				<data>
	insert into table
	(<xsl:value-of select="$Columns"/>)
	values
	(<xsl:value-of select="$Values"/>)
	</data>
			</xsl:for-each>
		</query>
	</xsl:template>
	<xsl:template name="getValues">
		<xsl:param name="V"/>
		<xsl:param name="T"/>
		<xsl:variable name="Values">
			<xsl:call-template name="getValueString">
				<xsl:with-param name="V" select="$V"/>
				<xsl:with-param name="T" select="$T"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:choose>
			<xsl:when
test="normalize-space(substring(normalize-space(string($Values)),string-
length(normalize-space(string($Values))),1))=','">
				<xsl:value-of
select="substring(normalize-space(string($Values)),1,string-length(norma
lize-space(string($Values)))-1)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of
select="normalize-space(substring(normalize-space(string($Values)),strin
g-length(normalize-space(string($Values))),1))"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="getValueString">
		<xsl:param name="V"/>
		<xsl:param name="T"/>
		<xsl:for-each select="$V/*[name(.)!='ENTRY']">
			<xsl:choose>
				<xsl:when test="$T='Columns'">
					<xsl:value-of
select="name(.)"/>,
			</xsl:when>
				<xsl:otherwise>
			'<xsl:value-of select="."/>',
			</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
		<xsl:for-each select="$V/*[name(.)='ENTRY']">
			<xsl:for-each select="Datum">
				<xsl:choose>
					<xsl:when test="$T='Columns'">
						<xsl:value-of
select="name(.)"/>,
			</xsl:when>
					<xsl:otherwise>
			'<xsl:value-of select="."/>',
			</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
------------------------------------------------------------------------
-------------------------------------

it is running, but some place need be optimized...,and you must think
about the type of the Fields: int, char...etc

Jason@GRT

-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Mukul
Sent: 2003年6月13日 10:02
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Problem writing XSLT

Hello,

I have an XML file in the form below. 
 
 <trace> 
 <LEVEL>FLOW</LEVEL> 
 <TIME>2003-05-06 09:57:59:9567238</TIME> 
 <TICKS>631878118799567238</TICKS> 
 <COMPONENT>NS</COMPONENT> 
 <SUBCOMPONENT>NS_SVC</SUBCOMPONENT> 
 <ENTRY type="ProcEntry"> 
 <Datum name="Method">Init</Datum> 
 </ENTRY> 
 </trace> 

 <trace> 
 <LEVEL>FLOW</LEVEL> 
 <TIME>2003-05-06 09:58:00:2830701</TIME> 
 <TICKS>631878118802830701</TICKS> 
 <COMPONENT>NS</COMPONENT> 
 <SUBCOMPONENT>NS_SVC</SUBCOMPONENT> 
 <ENTRY type="ProcEntry"> 
 <Datum name="Method">InitMgmt</Datum> 
 </ENTRY> 
 </trace> 

 <trace> 
 <LEVEL>FULL</LEVEL> 
 <TIME>2003-05-06 09:58:00:2830701</TIME> 
 <TICKS>631878118802830701</TICKS> 
 <COMPONENT>NS</COMPONENT> 
 <SUBCOMPONENT>NS_SVC</SUBCOMPONENT> 
 <ENTRY type="ProcEntry"> 
 <Datum name="Adapter">SA</Datum> 
 <Datum name="Method">ServiceabilityAgent</Datum> 
 </ENTRY>
 </trace> 
 
I need to make an XSL file in 
such a way so that resulted o/p will be in the
following given below. 

 
 <Query>
 <Data> insert into 
table(LEVEL,TIME,TICKS,COMPONENT,SUBCOMPONENT,Method)
 values('FLOW',2003-05-06

09:58:00:2830701,631878118799567238,NS,SVC,Init)</Data>
 <Data> insert into

table(LEVEL,TIME,TICKS,COMPONENTS,SUBCOMPONENTS,Adapter,Method)
 values('FLOW',2003-05-06
09:58:00:2830701,631878118799567238,NS,SVC,SA,ServiceabilityAgent)</Data
>
 </Query>
  
Pls note --
1 - each <trace> tag will generate a SQL in the
corresponding <data> tag 
2 - the value of columns of the SQL will come from
<LEVEL>, <TIME>, <TICKS>, <COMPONENT>, <SUBCOMPONENT>
and attribute values of <Datum> tag for e.g. Adapter
and Method. The table name is a constant.
3 - values of table columns will be corresponding
values of these tags
 
I'll appreciate a response to the above problem..

Regards,
Mukul



__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread