XSL, XSLT, XPath

I have spent some time on internet, and finally come out with this excerpt for part of the task on hand.
using XSLT, and xalan as for extension.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xalan/java">
	<xsl:template
		match="el_tnx_record | lc_tnx_record | sg_tnx_record | tf_tnx_record">

		<!-- define variables here -->

		<!-- time variables -->
		<xsl:variable name="calendar"
			select="java:java.util.Calendar.getInstance()" />
		<xsl:variable name="millsTime" select="java:getTimeInMillis($calendar)" />
		<xsl:variable name="year" select="java:get($calendar,1)" />
		<xsl:variable name="month" select="java:get($calendar,2)" />
		<xsl:variable name="day" select="java:get($calendar,3)" />
		<xsl:variable name="hour" select="java:get($calendar,10)" />
		<xsl:variable name="minute" select="java:get($calendar,12)" />
		<xsl:variable name="second" select="java:get($calendar,13)" />

		<!-- hashMap variables -->
		<!-- <xsl:variable name="hashMap" select="java:java.util.HashMap.new()" 
			/> -->
		<!-- <xsl:variable name="01" select="java:java.lang.String.new(01)" /> -->
		<!-- <xsl:variable name="new" select="java:java.lang.String.new(new)" /> -->
		<!-- <xsl:variable name="void" select="java:put($hashMap, $year, $month)" 
			/> -->
		<!-- <xsl:variable name="status" select="java:get($hashMap, $01)" /> -->


		<!-- the content should be dynamic values -->
		<EAI>
			<BWH>WLGPSG03EBN EBNRS001 20080520093610 TR_LC_MODCAN_F -1
				UOB.ELK.PROD.EBNRQST03
				UOB.ELK.PROD.EBNRPLY03 CMOS9672 CMOS9672VSA
				VSASS009 XXXSS009
				UOB.XXX.PROD.YYYRQST UOB.XXX.PROD.YYYRPLY RUS
				201105202889281          
			</BWH>
			<SvcRq>
				<ChannelId>BIBnn</ChannelId>
				<SvcCode>
					PPPTR_LC_MODCAN_F</SvcCode>
				<SvcRqId>
					<xsl:value-of select="$millsTime" />
					<xsl:value-of select=" ./ref_id" />
				</SvcRqId>
				<Timestamp>
					<Year>
						<xsl:value-of select="$year" />
					</Year>
					<Month>
						<xsl:value-of select="$month" />
					</Month>
					<Day>
						<xsl:value-of select="$day" />
					</Day>
					<Hour>
						<xsl:value-of select="$hour" />
					</Hour>
					<Minute>
						<xsl:value-of select="$minute" />
					</Minute>
					<Second>
						<xsl:value-of select="$second" />
					</Second>
				</Timestamp>
				<TimeoutPeriod>60</TimeoutPeriod>
				<ErrRecoveryReversal>Y</ErrRecoveryReversal>
				<BusinessDrivenReversal>Y</BusinessDrivenReversal>
				<MQExpPeriod>55</MQExpPeriod>
			</SvcRq>
			<ReversalSvcRq>
				<PrevSvcRqId />
			</ReversalSvcRq>
			<BIBRq>
				<UserId>ABCD123</UserId>
				<CIFNo>1234567</CIFNo>
				<CompanyId>
					ABCDEFG
				</CompanyId>
			</BIBRq>
			<SubSvcRq>
				<SubSvc>
					<SubSvcRqHeader>
						<SvcCode>TR_LC_MODCAN_F</SvcCode>
						<SubSvcSeq>1</SubSvcSeq>
						<TxnRef>20013192529</TxnRef>
					</SubSvcRqHeader>
					<SubSvcRqDetail>
						<xsl:element name="{name()}">
							<!-- copy all other node, except charges, cross_references, and additional_field -->
							<xsl:copy-of
								select="@*|node()[not(self::charges)][not(self::cross_references)][not(self::additional_field)]" />

							<!-- create new element using the value of the name attribute of additional_fieldd -->
							<xsl:for-each select="additional_field">
								<xsl:element name="additional_field_{@name}">
									<xsl:copy-of select="node()" />
								</xsl:element>
							</xsl:for-each>

							<!-- append cross_reference tag with position -->
							<xsl:if test="cross_references">
								<cross_references>
									<xsl:for-each select="cross_references/cross_reference">
										<xsl:element name="cross_reference_{position()}">
											<xsl:copy-of select="@*|node()" />
										</xsl:element>
									</xsl:for-each>
								</cross_references>
							</xsl:if>

							<!-- append charges tag with position -->
							<xsl:if test="charges">
								<charges>
									<xsl:for-each select="charges/charge">
										<xsl:element name="charge_{position()}">
											<xsl:copy-of select="@*|node()" />
										</xsl:element>
									</xsl:for-each>
								</charges>
							</xsl:if>
						</xsl:element>
					</SubSvcRqDetail>
				</SubSvc>
			</SubSvcRq>
		</EAI>
	</xsl:template>
</xsl:stylesheet>

XSL, XSLT, are defined by W3C. it’s some language quite limited, as compared to java, or c, actually might not be suitable for comparison.

There are only limited keywords, functions defined. And XPath, navigation is especially commonly used with XSLT.

Advertisements

Author: lwpro2

Java J2EE professional

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s