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"?>
<MSG_CGI_CANCELJOB ID = "WMIS-965931" SRC_SYSTEM_CODE = "WMIS" SRC_SYSTEM_INSTANCE = "WMIS">
<CANCELJOB:CMP_CGI_CANCELJOB xmlns:CANCELJOB = "CANCELJOB" >
<CANCELJOB:ACTIONCODE>1</CANCELJOB:ACTIONCODE>
<CANCELJOB:AGENCYCODE>DAILYWORK</CANCELJOB:AGENCYCODE>
<CANCELJOB:CANCEL_DATE>07/01/2010</CANCELJOB:CANCEL_DATE>
<CANCELJOB:CANCEL_REASON>CUST</CANCELJOB:CANCEL_REASON>
<CANCELJOB:CREATIONDATETIME>2010-01-07 12:07:05.505</CANCELJOB:CREATIONDATETIME>
<CANCELJOB:EXTERNALNUMBER>308106</CANCELJOB:EXTERNALNUMBER>
<CANCELJOB:JOBNUMBER>133222</CANCELJOB:JOBNUMBER>
</CANCELJOB:CMP_CGI_CANCELJOB>
</MSG_CGI_CANCELJOB>


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

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
<CANCELJOB environment="Test">
<ApplicationArea>
<CREATIONDATETIME><xsl:value-of select="@CREATIONDATETIME"/></CREATIONDATETIME>
</ApplicationArea>
<DataArea>
<Job>
<AGENCYCODE><xsl:value-of select="@AGENCYCODE"/></AGENCYCODE>
<Closure>
<ACTIONCODE><xsl:value-of select="@ACTIONCODE"/></ACTIONCODE>
</Closure>
<ExternalNumbers>
<EXTERNALNUMBER><xsl:value-of select="@EXTERNALNUMBER"/></EXTERNALNUMBER>
</ExternalNumbers>
<JOBNUMBER><xsl:value-of select="@JOBNUMBER"/></JOBNUMBER>
</Job>
</DataArea>
</CANCELJOB>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>


The following is the resulting XML file that I am expecting

<CANCELJOB environment="Test">
<ApplicationArea>
<CREATIONDATETIME>2010-01-07 12:07:05.505308106</CREATIONDATETIME>
</ApplicationArea>
<DataArea>
<Job>
<AGENCYCODE>DAILYWORK</AGENCYCODE>
<Closure>
<ACTIONCODE>1</ACTIONCODE>
</Closure>
<ExternalNumbers>
<EXTERNALNUMBER>308106</EXTERNALNUMBER>
</ExternalNumbers>
<JOBNUMBER>133222</JOBNUMBER>
</Job>
</DataArea>
</CANCELJOB>

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

Appreciate any assistant members can provide
Thanks
awofesof

Recommended Answers

All 10 Replies

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/

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>

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

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

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.

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

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 <soap:Envelop.....> </soap> 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

<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>

<CANCELJOB environment="Test">
<ApplicationArea>
<CREATIONDATETIME></CREATIONDATETIME>
</ApplicationArea>
<DataArea>
<Job>
<AGENCYCODE></AGENCYCODE>
<Closure>
<ACTIONCODE></ACTIONCODE>
</Closure>
<ExternalNumbers>
<EXTERNALNUMBER></EXTERNALNUMBER>
</ExternalNumbers>
<JOBNUMBER></JOBNUMBER>
</Job>
</DataArea>
</CANCELJOB>
</soapenv:Body>
</soapenv:Envelope
>

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.