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 June 13th, 2005, 11:04 AM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT for complicated xml to xml transf. required

I want to transform 'source.xml' to 'target.xml'. can any body help me in writing the Style sheet.

source.xml:-
============

<TD5>
  <separator>*</separator>
  <value>123</value>
  <separator>*</separator>
  <value>45</value>
  <separator>*</separator>
  <separator>*</separator>
  <separator>*</separator>
  <value>ALL WEIGHTS PEPS-RIDGF</value>
</TD5>

target.xml:-
============
<TD5>
  <value>123</value>
  <value>45</value>
  <value/>
  <value/>
  <value>ALL WEIGHTS PEPS-RIDGF</value>
</TD5>

Logic:- The source.xml has <td5> document element and two child elements, namely <separator> and <value>. Each <separator> element is "optionally" followed by a <value> element.

The logic to be implemented is, if the <separator> element is followed by a <value> element then that <value> element should go into target.xml and If <separator> element is NOT FOLLOWED by a <value> element then for every two continuous <separator> elements one new blank <value> element (which is <value/>) should go into the target.xml.

In the given source.xml there are three continuous <separator> elements and hence in the target.xml they are replaced by 2 <value/> elements.

 
Old June 13th, 2005, 12:10 PM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default

Yo!

Kinda easy... I don't think you tried to hard...

Here's your solution mate:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="TD5">
  <xsl:copy>
   <xsl:apply-templates select="separator"/>
  </xsl:copy>
 </xsl:template>
 <xsl:template match="separator">
  <xsl:element name="value">
   <xsl:if test="following-sibling::node()[1][name()='value']">
    <xsl:value-of select="following-sibling::node()[1]"/>
   </xsl:if>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

Next time, try to solve your problem a little before getting free code. This forum is here to help people, not to provide cheap labor.

Hasta la vista!

Rushman

Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
 
Old June 13th, 2005, 01:12 PM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thak you rushman. To be honest with you I tried for over a week (I just strated using XSLT), may be I'm dumb? Thanks a lot.

 
Old June 17th, 2005, 04:26 PM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Rushman,

Once again thanks for your help. The free program that you gave did not work. I'm using "xalan" on Linux OS. Never the less it served the purpose of giving me the idea.

When I'm using your xslt sheet the output that i got is as follows ..

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

The xslt-sheet that I came up with is as follows. Of course , there is no big difference between your xslt and mine. I'm using * and you are using node(). What I could not understand is why doesn't your xslt which uses node() give the same output as mine that uses *? Does this have anything to do with XALAN or if everything is working as expected and your code is not complete ;)

<?xml version="1.0" ?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="TD5">
  <TD5>
    <xsl:apply-templates select="separator" />
  </TD5>
  </xsl:template>

   <xsl:template match="separator">
   <value>
    <xsl:if test="following-sibling::*[1][name()='value']">
   <xsl:value-of select="following-sibling::*[1]" />
  </xsl:if>
  </value>
 </xsl:template>
</xsl:stylesheet>






Similar Threads
Thread Thread Starter Forum Replies Last Post
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 08:18 PM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM
XML to XML through an XSLT (VB.NET) dimondwoof XSLT 1 June 25th, 2003 12:07 PM





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