p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Problem with converting an XML file in UNIX using XSL (http://p2p.wrox.com/showthread.php?t=75753)

vimalprasathj August 17th, 2009 12:43 PM

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.

Martin Honnen August 17th, 2009 01:00 PM

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.

mhkay August 17th, 2009 01:40 PM

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>



All times are GMT -4. The time now is 02:06 AM.

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