Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
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 Search this Thread Display Modes
  #1 (permalink)  
Old March 22nd, 2006, 01:31 PM
Registered User
 
Join Date: Mar 2006
Location: Mumbai, , India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Schema to Schema conversion

Hi All,
I am new to XSLT.
In my XSLT, I have to map one schema to another, in which some of the elements(and its child elements) in source schema converge to a particular element in target schema, while the rest of the elements are ditto the same.
Basically, I need to retain the spaces(whitespaces, tabs, newline etc.) and comments etc from the input xml file and only convert those mapping elements which are different.
could anybody tell me how to act only on the changed element and copy the common element as same.
Also, for those elements which are being transformed, I have almost one to one mapping for all attributes. In my output xml, i need to have only those attributes mapped which were present in input xml.

Please respond.
Thanks.
Reply With Quote
  #2 (permalink)  
Old March 22nd, 2006, 02:03 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's a standard design pattern for this in XSLT. Write one template that copies elements unchanged:

<xsl:template match="*">
 <xsl:copy>
  <xsl:copy-of select="@*"/>
  <xsl:apply-templates/>
 </xsl:copy>
</xsl:template>

then override it for elements you need to change. For example, to add an attribute to one particular element:

<xsl:template match="element[particular='true']">
  <xsl:copy>
  <xsl:attribute name="new">value</xsl:attribute>
  <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

You can also apply-templates to attribute nodes if you wish, though I usually do it only at the element level.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old March 22nd, 2006, 04:41 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your solution is fine.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #4 (permalink)  
Old March 22nd, 2006, 04:43 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Umm... I think my response got added to the wrong message!

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old March 23rd, 2006, 12:13 PM
Registered User
 
Join Date: Mar 2006
Location: Mumbai, , India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi All,

Thanks Michael for the reply.
As mentioned, my xslt is basically having one to one mappings from source to target schema. XSLT changes the element/attribute name one to one and the data values of element/attribute remain the same.
So, what I basically need is to retain the whitespaces/tabs/newlines in the source xml file on to target and change the element/attribute name correspondingly. I have read somewhere that :
"XML parser will normalize the attribute values. A tab or a newline is always
replaced by a single space unless it is written as a character reference such
as <<#9;>> or <<#A>> "
But, my problem is that the input xml file has a random number of whitespaces/tabs/newlines between elements/attributes and that needs to be retained in the output xml file. I tried <xsl:preserve-space element="*"/> at the start of my XSLT file, but it didn't worked.

Any expert comments please.
Any programatic solution will also do (in opensource technologies please).
Eagerly waiting for your expert response.

Reply With Quote
  #6 (permalink)  
Old March 23rd, 2006, 12:25 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your description isn't very clear, it would be useful to show a simple input and desired output, your current code, and what output it is currently giving. All cut down to a dozen lines or so, of course!

Are you using the Microsoft MSXML parser? That has a notorious "feature" that it removes whitespace by default.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Conforming to a schema Chamkaur XSLT 1 January 8th, 2007 06:03 AM
ER dia to SQL schema conversion neerajiiitb SQL Language 1 March 31st, 2006 09:12 AM
Schema help!? beerni XML 3 October 14th, 2005 08:24 AM
Schema gabrieldg74 XML 0 August 16th, 2005 05:42 PM
Print Schema mat41 SQL Server 2000 3 November 25th, 2004 07:13 PM



All times are GMT -4. The time now is 01:46 PM.


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