Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 13th, 2005, 11:04 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
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.

Reply With Quote
  #2 (permalink)  
Old June 13th, 2005, 12:10 PM
Authorized User
Points: 251, Level: 5
Points: 251, Level: 5 Points: 251, Level: 5 Points: 251, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Canada.
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.
Reply With Quote
  #3 (permalink)  
Old June 13th, 2005, 01:12 PM
Registered User
 
Join Date: Jun 2005
Location: , , .
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.

Reply With Quote
  #4 (permalink)  
Old June 17th, 2005, 04:26 PM
Registered User
 
Join Date: Jun 2005
Location: , , .
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>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 05:01 PM
XML to XML through an XSLT (VB.NET) dimondwoof XSLT 1 June 25th, 2003 12:07 PM



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


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