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 August 17th, 2009, 12:43 PM
Registered User
 
Join Date: Aug 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default Problem with converting an XML file in UNIX using XSL

Hi All,

I Have tried converting an XML file to an another format of XML file using XSL in JAVA(windows). It worked fine. But when i tried the same thing in UNIX it didn't work as expected. Here is the following XSL which used in our JAVA code

XSL FILE;

<?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" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes" xmlns:gmr="http://www.gnome.org/gnumeric/v7">

<xsl:strip-space elements="*"/>

<xsl:output method ="xml" indent="yes" omit-xml-declaration="yes" />

<xsl:template match="/">

<gmr:Workbook xmlns:gmr="http://www.gnome.org/gnumeric/v7">

<gmr:SheetNameIndex>
<gmr:SheetName>DiscussedProducts</gmr:SheetName>
</gmr:SheetNameIndex>

<gmr:Sheets>

<gmr:Sheet>
<gmr:Name>DiscussedProducts</gmr:Name>
<gmr:MaxCol>-1</gmr:MaxCol>
<gmr:MaxRow>-1</gmr:MaxRow>


<gmr:Row>
<gmr:Cells>

<xsl:apply-templates select="/xCRM/DiscussedProducts"/>

</gmr:Cells>
</gmr:Row>
</gmr:Sheet>

</gmr:Sheets>

</gmr:Workbook>

</xsl:template>

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


<xsl:template match="DiscussedProduct">
<xsl:apply-templates select="Products">
<xsl:with-param name="vim1" select="ActivityId"/>

</xsl:apply-templates>

</xsl:template>

<xsl:template match="Product">
<xsl:param name="vim1"/>


<xsl:param name="vim2"><xsl:number level="any" format="1"/></xsl:param>


<gmr:Cell Row="{$vim2}" Col="1" ValueType = "60"><xsl:value-of select="$vim1"/> </gmr:Cell>

<gmr:Cell Row="{$vim2}" Col="2" ValueType = "60">xsl:value-of select="ProductId"/> </gmr:Cell>
</xsl:template>
</xsl:stylesheet>

JAVA code is:

import java.io.FileOutputStream;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class xml2xmltransform {

public void convert()throws Exception,
TransformerException, TransformerConfigurationException,FileNotFoundExce ption, IOException {
javax.xml.transform.TransformerFactory xmlFactory = null;
javax.xml.transform.Transformer xmlTransformer = null;
try
{
xmlFactory = javax.xml.transform.TransformerFactory.newInstance ();
xmlTransformer = xmlFactory.newTransformer(new javax.xml.transform.stream.StreamSource("D:\\XML2X LS\\XML2XML.xsl"));
xmlTransformer.transform(new StreamSource("D:\\XML2XLS\\msd_wfi_20081218_000001 33.xml"), new StreamResult(new FileOutputStream("C:\\OutputXML.xml")));
}

catch (Exception ex)
{
// logger.fatal(ex.getMessage());
System.out.println("error");
ex.printStackTrace();
}
finally
{
xmlFactory = null;
xmlTransformer = null;
}
}
public static void main(String[] args) throws FileNotFoundException,Exception {
try
{
xml2xmltransform test = new xml2xmltransform();
test.convert();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}


INPUT XML file is:

<?xml version="1.0" encoding="utf-8"?>
<xCRM>
<Header>
<Id>133</Id>
<Signature>MRKFIN_SGPFIN_TST</Signature>
<CreationDate>2008-12-18</CreationDate>
<CreationTime>07:21:48</CreationTime>
</Header>


<DiscussedProducts>
<DiscussedProduct>
<ActivityId>DYWE..66nT6/</ActivityId>
<Products>
<Product>
<ProductId>ING</ProductId>
<PercentAllocated>60</PercentAllocated>
<Position>1</Position>
</Product>
<Product>
<ProductId>EZE</ProductId>
<PercentAllocated>60</PercentAllocated>
<Position>1</Position>
</Product>

</Products>
</DiscussedProduct>
<DiscussedProduct>
<ActivityId>DYWE../</ActivityId>
<Products>
<Product>
<ProductId>IG</ProductId>
<PercentAllocated>60</PercentAllocated>
<Position>1</Position>
</Product>
<Product>
<ProductId>EE</ProductId>
<PercentAllocated>60</PercentAllocated>
<Position>1</Position>
</Product>

</Products>
</DiscussedProduct>


</DiscussedProducts>

</xCRM>

Expected Output is:

<gmr:Workbook xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes" xmlns:gmr="http://www.gnome.org/gnumeric/v7">
<gmr:SheetNameIndex>

<gmr:SheetName>DiscussedProducts</gmr:SheetName>
</gmr:SheetNameIndex>
<gmr:Sheets>

<gmr:Sheet>
<gmr:Name>DiscussedProducts</gmr:Name>
<gmr:MaxCol>-1</gmr:MaxCol>
<gmr:MaxRow>-1</gmr:MaxRow>
<gmr:Row>
<gmr:Cells>

<gmr:Cell Row="1" Col="1" ValueType="60">
DYWE..66nT6/
</gmr:Cell>
<gmr:Cell Row="1" Col="2" ValueType="60">
ING
</gmr:Cell>
<gmr:Cell Row="1" Col="3" ValueType="60">
60
</gmr:Cell>
<gmr:Cell Row="1" Col="4" ValueType="60">
1
</gmr:Cell>
<gmr:Cell Row="2" Col="1" ValueType="60">
DYWE..66nT6/
</gmr:Cell>
<gmr:Cell Row="2" Col="2" ValueType="60">
EZE
</gmr:Cell>
<gmr:Cell Row="2" Col="3" ValueType="60">
60
</gmr:Cell>
<gmr:Cell Row="2" Col="4" ValueType="60">
1
</gmr:Cell>
<gmr:Cell Row="3" Col="1" ValueType="60">
DYWE../
</gmr:Cell>
<gmr:Cell Row="3" Col="2" ValueType="60">
IG
</gmr:Cell>
<gmr:Cell Row="3" Col="3" ValueType="60">
60
</gmr:Cell>
<gmr:Cell Row="3" Col="4" ValueType="60">
1
</gmr:Cell>
<gmr:Cell Row="4" Col="1" ValueType="60">
DYWE../
</gmr:Cell>
<gmr:Cell Row="4" Col="2" ValueType="60">
EE
</gmr:Cell>
<gmr:Cell Row="4" Col="3" ValueType="60">
60
</gmr:Cell>
<gmr:Cell Row="4" Col="4" ValueType="60">
1
</gmr:Cell>

</gmr:Cells>
</gmr:Row>
</gmr:Sheet>
</gmr:Sheets>
</gmr:Workbook>


Here the problem is when i executing the following code in UNIX, i didn't get the activity Id populated as expected which was highlighted in BOLD. I suspect the problem is due to the "param"- vim1 value passed from one template to an another template.. is there any way to solve the problem.. or is there any way to pass a value from one template to an another template.. Thanks in Advance for your help.
 
Old August 17th, 2009, 01:00 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I don't think you need that parameter, you can simply use XPath to access the ancestor element (grandparent) and its Activity child element:
Code:
<gmr:Cell Row="{$vim2}" Col="1" ValueType = "60"><xsl:value-of select="parent::Products/parent::DiscussedProduct/ActivityId"/> </gmr:Cell>
If you want to use a parameter then you either need to use XSLT 2.0 and tunnel it through to the template for the grandchild or in XSLT 1.0 you need to add a template for Products that passes the parameter on explicitly. Currently you pass the parameter on in the template for the grandparent and want to receive it in a grandchild, that does not work.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old August 17th, 2009, 01:40 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I suspect the difference isn't one between Windows/Unix, but that you are using different XSLT processors in the two cases. Check which XSLT processor is in use using

Code:
<xsl:comment>
  Generated using <xsl:value-of select="system-property('xsl:vendor')"/>
</xsl:comment>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting xml to xsl leena XSLT 1 August 3rd, 2007 06:04 AM
Converting Source Xml into Target Xml Using XSL. alapati.sasi XSLT 3 May 14th, 2007 10:54 AM
Problem in Converting xml to sgml pragneshpandya XML 0 November 27th, 2006 01:52 AM
Converting excel file into XML... muskaanbajaj ASP.NET 1.0 and 1.1 Professional 3 April 3rd, 2006 12:33 PM
Converting an XML File into a HTML File ewan XSLT 2 October 4th, 2004 09:12 AM





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