954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

XML Transformation using XSL

Dear members

I am new to the world of XML and XSL transformation, i hope someone can help me with this XSL transformation
for an XML. I am not geting the expected result as described below

The following is the XML that I am trying to transform to another XML format using the XSL file also describe below
canceljob.xml

<?xml version = "1.0"?>
<?xml-stylesheet type="text/xsl" href="canceljob.xsl"?>
1DAILYWORK07/01/2010CUST2010-01-07 12:07:05.505308106133222


The following is the XSL file to perform the translation
canceljob.xsl XSL File

<?xml version="1.0"?>


The following is the resulting XML file that I am expecting

2010-01-07 12:07:05.505308106DAILYWORK1308106133222

but the result i am getting is
1DAILYWORK07/01/2010CUST2010-01-07 12:07:05.505308106133222

Appreciate any assistant members can provide
Thanks
awofesof

awofesof
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This is pretty ugly. :) I realize your new to XSLT but it's obvious from the design of your stylesheet you don't quite have a grasp on what XSLT is and how it functions.

You're trying to select attributes from your source document. But all your data is in elements. You're apply-templates for no reason at all. You also have a one giant monolithic template that matches on the root that does does it all in one step.

You want to design with small nested templates and apply them to generate your output tree accordingly. I'll write up a good example in a little bit.

In the meantime, I'd suggest you learn about the basics. Even the W3C Schools walk through will teach you more than you understand now.

http://www.w3schools.com/xsl/

iceandrews
Junior Poster
185 posts since May 2010
Reputation Points: 10
Solved Threads: 30
 

Without getting too detailed, this XSLT will produce the result you're looking for. This is VERY rough and there's lots of things I might change depending on the input document characteristics, but I'll leave that to figure out for yourself.

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:CANCELJOB="CANCELJOB">

	<xsl:template match="/">
		<xsl:apply-templates select="MSG_CGI_CANCELJOB"/>
	</xsl:template>

	<xsl:template match="MSG_CGI_CANCELJOB">
		<xsl:element name="CANCELJOB">
			<xsl:attribute name="environment">
				<xsl:value-of select="'Test'"/>
			</xsl:attribute>
			<xsl:apply-templates select="CANCELJOB:CMP_CGI_CANCELJOB/CANCELJOB:CREATIONDATETIME"/>
			<xsl:apply-templates select="CANCELJOB:CMP_CGI_CANCELJOB"/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="CANCELJOB:CREATIONDATETIME">
		<xsl:element name="ApplicationArea">
			<xsl:element name="{local-name(.)}">
				<xsl:value-of select="."/>
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<xsl:template match="CANCELJOB:CMP_CGI_CANCELJOB">
		<xsl:element name="DataArea">
			<xsl:element name="Job">
				<xsl:apply-templates select="CANCELJOB:AGENCYCODE"/>
				<xsl:apply-templates select="CANCELJOB:ACTIONCODE"/>
				<xsl:apply-templates select="CANCELJOB:EXTERNALNUMBER"/>
				<xsl:apply-templates select="CANCELJOB:JOBNUMBER"/>
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<xsl:template match="CANCELJOB:AGENCYCODE">
		<xsl:element name="{local-name(.)}">
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="CANCELJOB:ACTIONCODE">
		<xsl:element name="Closure">
			<xsl:element name="{local-name(.)}">
				<xsl:value-of select="."/>
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<xsl:template match="CANCELJOB:EXTERNALNUMBER">
		<xsl:element name="ExternalNumbers">
			<xsl:element name="{local-name(.)}">
				<xsl:value-of select="."/>
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<xsl:template match="CANCELJOB:JOBNUMBER">
		<xsl:element name="{local-name(.)}">
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>


Produces this output:

<CANCELJOB xmlns:CANCELJOB="CANCELJOB" environment="Test">
	<ApplicationArea>
		<CREATIONDATETIME>2010-01-07 12:07:05.505</CREATIONDATETIME>
	</ApplicationArea>
	<DataArea>
		<Job>
			<AGENCYCODE>DAILYWORK</AGENCYCODE>
			<Closure>
				<ACTIONCODE>1</ACTIONCODE>
			</Closure>
			<ExternalNumbers>
				<EXTERNALNUMBER>308106</EXTERNALNUMBER>
			</ExternalNumbers>
			<JOBNUMBER>133222</JOBNUMBER>
		</Job>
	</DataArea>
</CANCELJOB>
iceandrews
Junior Poster
185 posts since May 2010
Reputation Points: 10
Solved Threads: 30
 

Thank you very much for your responses. I am currently going through the tutorial you recommended and coupled with your sample response, I am begining to understand how this works.

I will try your suggestions and add to it and will let you and the members knows how it turns out

Thank you

awofesof

awofesof
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Good luck!

iceandrews
Junior Poster
185 posts since May 2010
Reputation Points: 10
Solved Threads: 30
 

I ran your sample as you have suggested, but the result set that was produce is the same result as I posted. I am not getting the XML result set you are getting/suggesting.
I am wnadering if I am executing the xls file incorectly. I even run the files from the wc3 environment and get the constant data and not the XML document. Is there something we need to set to force an XML documet result set as opposed to the actual values being returned.

I have the statement
<?xml-stylesheet type="text/xsl" href="canceljob.xsl"?>
inside of the XML file to execute the xls file. Can you explain how you are executing the xls file given the XML file I submitted.

I appreciate your assistant again

Thanks

awofesof
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Well, I'm using Stylus Studio, which is a XML IDE Suite. But there are lots of IDEs out there that can run xslt. Some of them are free (Stylus Studio, Altova XML Spy, Liquid XML, EditiX, oXygen)

What processor are you using and in what kind of enviroment? You can always download the a free version of Saxon and run them from a command line though.

iceandrews
Junior Poster
185 posts since May 2010
Reputation Points: 10
Solved Threads: 30
 

Thank you, I am using the Internent Explorer by just double clicking the XML file. This is prabably why I am just getting the constant or evaluated results whitout the XML constructs aroud it.

I will download one of the sample you suggested and retry later.

Thanks and I am really appreciative of your help

awofesof
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

iceandrews,
Thank you for your suggestions, I have downloaded one of your recommended software and and your suggestions above is now copiled correctly and working fine.
Based on your code and the short lesson from W3cschool and I am beginning to understand XML, I must confess it is a different thought process compared to procedural methodology and even java coding.

I do have one more question, I have been trying to modify the code to append construct to the code but it is so far not working and I am wandering how you will do it. Here is the new XML I am trying to construct.. adding the BOLD to the generated XML.....

Thanks


>

awofesof
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Should only have to change one template.

<xsl:template match="/">
		<soapenv:Envelope xmlns:mwf="http://www.utilitysolutions.cgi.com/MWFMS-1_1" xmlns:oag="http://www.openapplications.org/oagis" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
			<soapenv:Body>
				<xsl:apply-templates select="MSG_CGI_CANCELJOB"/>
			</soapenv:Body>
		</soapenv:Envelope>
	</xsl:template>
iceandrews
Junior Poster
185 posts since May 2010
Reputation Points: 10
Solved Threads: 30
 

[Thanks very much ...This works very well.
Appreciate your help as I am getting better in understanding XSL constructions

awofesof
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: