Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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 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.
 
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
 
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
 
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
 
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.

 
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




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





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