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 8th, 2009, 02:22 AM
Registered User
 
Join Date: Aug 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default Converting subtree of an XML to another XML

Hi Guys,

I am new to XSL and it would be very grateful, if you could help me out my requirement.

I am having the following Input XML file and i need to convert it into the following XML format using XSL. I need an XSL file which converts it.

Input XML:

<?xml version="1.0" encoding="utf-8"?>
<xCRM>
<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>
<DiscussedProduct>
<ActivityId>..66nT6/</ActivityId>
<Products>
<Product>
<ProductId>ING</ProductId>
<PercentAllocated>60</PercentAllocated>
<Position>1</Position>
</Product>
</Products>
</DiscussedProduct>
</DiscussedProducts>
</xCRM>

the required OUTPUT XML file 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="0" Col="1" ValueType="60">
<gmr:Content>DYWE..66nT6/</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="0" Col="2" ValueType="60">
<gmr:Content>ING</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="0" Col="3" ValueType="60">
<gmr:Content>60</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="0" Col="4" ValueType="60">
<gmr:Content>1</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="1" Col="1" ValueType="60">
<gmr:Content>DYWE..66nT6/</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="1" Col="2" ValueType="60">
<gmr:Content>EZE</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="1" Col="3" ValueType="60">
<gmr:Content>60</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="1" Col="4" ValueType="60">
<gmr:Content>1</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="2" Col="1" ValueType="60">
<gmr:Content>DYWE../</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="2" Col="2" ValueType="60">
<gmr:Content>IG</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="2" Col="3" ValueType="60">
<gmr:Content>60</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="2" Col="4" ValueType="60">
<gmr:Content>1</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="3" Col="1" ValueType="60">
<gmr:Content>DYWE../</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="3" Col="2" ValueType="60">
<gmr:Content>EE</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="3" Col="3" ValueType="60">
<gmr:Content>60</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="3" Col="4" ValueType="60">
<gmr:Content>1</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="4" Col="1" ValueType="60">
<gmr:Content>..66nT6/</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="4" Col="2" ValueType="60">
<gmr:Content>ING</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="4" Col="3" ValueType="60">
<gmr:Content>60</gmr:Content>
</gmr:Cell>
<gmr:Cell Row="4" Col="4" ValueType="60">
<gmr:Content>1</gmr:Content>
</gmr:Cell>
</gmr:Cells>
</gmr:Row>
</gmr:Sheet>
</gmr:Sheets>
</gmr:Workbook>


Please note that the row number should increase based on number of nodes. I have tried to create an following XSL file but the row no is not get increased. please help me out on this.

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.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:gmr="http://www.gnome.org/gnumeric/v7" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes">

<xsl:strip-space elements="*"/>
<xsl:output method ="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:param name="vim2" select="0"/>
<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:param name="vim2" select="$vim2+1"/>
</xsl:apply-templates>

</xsl:template>

<xsl:template match="Product">
<xsl:param name="vim1"/>
<gmr:Cell Row="{$vim2}" Col="1" ValueType = "60">
<gmr:Content><xsl:value-of select="$vim1"/></gmr:Content>
</gmr:Cell>

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

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

<gmr:Cell Row="{$vim2}" Col="4" ValueType = "60">
<gmr:Content><xsl:value-of select="Position"/></gmr:Content>
</gmr:Cell>
</xsl:template>

</xsl:stylesheet>
 
Old August 8th, 2009, 02:24 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Use either the xpath function "position()" or the xslt instruction <xsl:number> to get your incrementing number.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
vimalprasathj (August 8th, 2009)
 
Old August 8th, 2009, 05:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Do what Sam says ;-)

When you write this:

<xsl:param name="vim2" select="$vim2+1"/>

You are defining a local variable $vim2 whose value is always one greater than the global variable $vim2. The value of the global variable never changes (it is always zero), so the value of the local variable will always be 1.

Variables in functional languages don't behave the same way as variables in the procedural languages that you are probably accustomed to.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
vimalprasathj (August 8th, 2009)
 
Old August 8th, 2009, 09:26 AM
Registered User
 
Join Date: Aug 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi sam and micheal,

First of all i would like to thank for replying my post and i have tried the following piece of code in the template "Product" and it worked out.

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

Millions of thanks for you. I was just stuck up for 2 days because of this and finally it comes to an end. Thank you very muuch once again...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficulties converting XML to XML using XSLT Reznik XSLT 7 June 3rd, 2008 05:45 AM
Converting Source Xml into Target Xml Using XSL. alapati.sasi XSLT 3 May 14th, 2007 10:54 AM
converting an xml to another xml sreeUS XSLT 4 May 2nd, 2006 01:59 PM
Converting XML to XML (making element mandatory) boondocksaint20 XSLT 8 April 28th, 2006 10:54 AM
Converting from binary to xml Morrislgn Pro VB.NET 2002/2003 0 February 2nd, 2006 12:46 PM





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