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 | 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 November 1st, 2006, 06:55 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML final output

Hello, i am new to xsl transformation. i tried to convert small xml file to another xml file by changing element names. more or less i succeeded, but the final output looks like that:
[u]output</u>
<?xml version="1.0" encoding="UTF-8"?><new-class><new-classitem><identifier><new-name>Sales </new-name><new-code>1234</new-code></identifier></new-classitem><new-classitem><identifier><new-name> Tax</new-name><new-code>5678</new-code></identifier></new-classitem></new-class>

[u]my xsl file</u>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" />
<xsl:template match="class">
        <new-class>
            <xsl:apply-templates />
        </new-class>
</xsl:template>

<xsl:template match="classitem">
        <new-classitem>
            <xsl:apply-templates />
        </new-classitem>
</xsl:template>

<xsl:template match="name">
        <new-name>
            <xsl:apply-templates/>
        </new-name>
</xsl:template>

<xsl:template match="code">
        <new-code>
            <xsl:apply-templates/>
        </new-code>
</xsl:template>

<xsl:template match="identifier">
        <identifier>
            <xsl:apply-templates />
        </identifier>
</xsl:template>

<xsl:template match="new-name">
            <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="new-code">
            <xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>

my problem is that i dont want all elements be in one line, i want each element to start from a new line. ex:

<identifier>
    <new-name>Sales </new-name>
    <new-code>1234</new-code>
</identifier>

what am i missing here?
thank you for your time,

alex
Reply With Quote
  #2 (permalink)  
Old November 1st, 2006, 07:21 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

I suspect you are using the Microsoft XSLT processor, which (contrary at least to the spirit of the W3C specs) removes white space from your source document without being asked. If you invoke the processor from the API there's an option to prevent this. In your case the whitespace is only cosmetic, so the simplest solution is to generate new whitespace in the output using <xsl:output indent="yes"/>.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old November 1st, 2006, 07:27 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello mhkay, thank you for your reply. yes you were right indent did the job.
thank you.

another question i wanted to ask you and other members:

lets say i have xml file with many different elements. when i transforming this xml file to another xml, i am changing only 2 or 3 elemnt names and the rest i want to keep and copy to new xml as it is.

is there a way to do it quick? or i have to include in my xsl each of the elements, even if i am changing the name of it?

i hope i was clear enough here.

regards,
alex


Reply With Quote
  #4 (permalink)  
Old November 1st, 2006, 07:33 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 coding pattern for this kind of transformation. You define an identity template rule:

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

which copies elements unchanged, and then you override it with specific template rules for the elements you want to modify. E.g. to strip <b>...</b> tags from the document (but not their content), add the rule

<xsl:template match="b">
  <xsl:apply-templates/>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old November 1st, 2006, 08:06 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mhkay, yes that worked... but i encountred a problem in one of the elements:

<stack-trace><![CDATA[ ... at System.IO.__ bla bla ... ]]></stack-trace>
how can i keep the "CDATA" tag and also how can i copy attributes that come with the element being copied?

thank you,
regards,
alex

Reply With Quote
  #6 (permalink)  
Old November 1st, 2006, 08:17 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok sorry - the same logic i applied to attributes and it worked,
but i am still facing the problem:

<stack-trace><![CDATA[ ... at System.IO.__ bla bla ... ]]></stack-trace>

i want to keep the "<![CDATA"

thank you
alex

Reply With Quote
  #7 (permalink)  
Old November 1st, 2006, 08:19 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

You can't keep CDATA - it's not part of the data model, because it isn't considered to be information-bearing. It's just a keyboard shortcut, like using the CAPS LOCK key.

If you want to copy attributes, use xsl:copy-of select="@*".

I hope though you're not going to use this list as your only way of getting such information. Elsewhere on this site you will find references to some good books...

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #8 (permalink)  
Old November 2nd, 2006, 12:32 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your time and help Michael

Reply With Quote
  #9 (permalink)  
Old November 2nd, 2006, 04:12 AM
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

I thought I answered that. You can't. The CDATA is gone long before the XSLT processor swings into action, it's not regarded as significant and is not notified by the XML parser to the XSLT processor. If the CDATA section boundaries are being used to represent significant information then you need to change your XML document design - that's what elements are for.

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
how to see xml output in IE? anboss XSLT 1 August 29th, 2008 05:16 PM
missing schemalocation in output XML matrixman XSLT 7 May 4th, 2007 07:55 AM
XML Output file Neal Crystal Reports 0 October 24th, 2006 07:02 AM
Output XML Hadware .NET Framework 1.x 0 December 28th, 2005 09:55 PM
XML output prashantnair XML 6 June 9th, 2004 12:22 AM



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


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