 |
| 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
|
|
|
|

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

November 1st, 2006, 07:21 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

November 1st, 2006, 07:27 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 1st, 2006, 07:33 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

November 1st, 2006, 08:06 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 1st, 2006, 08:17 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 "<
November 1st, 2006, 08:19 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

November 2nd, 2006, 12:32 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you for your time and help Michael
|
|

November 2nd, 2006, 04:12 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|
 |