Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 December 18th, 2009, 07:04 AM
Registered User
 
Join Date: Dec 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Avoid lines with XML tags alone

Hi,

I am writing XSL transformation to transform XML to XML. Everything works fine.

But the problem is, when I transformed the XML, some of the lines in the transformed XML have only XML tags. There is no text on that line other than XML tags.

For example:

<orderedlist numeration="arabic" inheritnum="ignore" continuation="restarts"><listitem><inst aid:cstyle="NL_NUM"> 1. </inst><para aid:pstyle="SF1_NL_FIRST">Use Appendix A.</para></listitem>
<listitem><inst aid:cstyle="NL_NUM"> 2. </inst><para aid:pstyle="SF1_NL_MID">Locate and print.</para></listitem>
<listitem><inst aid:cstyle="NL_NUM"> 5. </inst><para aid:pstyle="SF1_NL_LAST">Try answering the questions below.</para>
<itemizedlist mark="bull"><listitem><para aid:pstyle="SF1_BL_FIRST">• What overlap do you notice?</para></listitem>
<listitem><para aid:pstyle="SF1_BL_MID">• Choose one standard from INTASC.</para></listitem>
<listitem><para aid:pstyle="SF1_BL_LAST">• Can you see where studying.</para></listitem></itemizedlist>
</listitem>
</orderedlist></division></feature></section></section></section>



When we see the above XML, the last two lines are only having XML tags. I don't want to generate the XML in this way. All this tags should be merged with the line before it (or) after it.

Is it possible in XSLT.

Please provide the solution.

Thanks,
Krish
 
Old December 18th, 2009, 07:11 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Whitespace between tags like that is generally considered insignificant by most processors, so I really wouldn't worry about it unless this is having some actual effect on your processing.

You can get rid of it if you control your XSLT very carefully, but seeing as you haven't posted any of your XSLT its hard to say how.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 18th, 2009, 07:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The newline characters that you want to get rid of them can come from one of three places:

(a) they might be copied from the source document
(b) they might be copied from the stylesheet
(c) they might be generated by indent="yes" in the serialization options

(c) is unlikely in this case because it would apply elsewhere too. So you need to look at your XSLT code, decide whether it's (a) or (b), and fix your code accordingly. We can't help you with that if you don't show us your code.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 18th, 2009, 08:22 AM
Registered User
 
Join Date: Dec 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Here is my sample XML:

<?xml version="1.0" encoding="UTF-8"?><people>
<person><name>
<first>Bob</first>
<last>Toddson</last></name>
<acct-no>327598</acct-no>
<fav-color hex="#ff0000">Red</fav-color>
</person>
<person><name>
<first>Phillip</first>
<last>Cardwell</last>
</name>
<acct-no>258929</acct-no>
<fav-color hex="#d2b48c">Tan</fav-color>
</person></people>



My XSLT is:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" xml:space="default"/>
<xsl:template match="*|@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*[not(name()='aidpstyle')]|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="first">
<xsl:copy>
<xsl:attribute name="aid_cstyle">FIRST</xsl:attribute>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="last">
<xsl:copy>
<xsl:attribute name="aid_cstyle">LAST</xsl:attribute>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="acct-no">
<xsl:copy>
<xsl:attribute name="aid_cstyle">ACCT-NO</xsl:attribute>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="fav-color">
<xsl:copy>
<xsl:attribute name="aid_cstyle">FAV-COLOR</xsl:attribute>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


Output XML is:
<?xml version="1.0" encoding="UTF-8"?><people>
<person><name>
<first aid_cstyle="FIRST">Bob</first>
<last aid_cstyle="LAST">Toddson</last></name>
<acct-no aid_cstyle="ACCT-NO">327598</acct-no>
<fav-color aid_cstyle="FAV-COLOR" hex="#ff0000">Red</fav-color>
</person>
<person><name>
<first aid_cstyle="FIRST">Phillip</first>
<last aid_cstyle="LAST">Cardwell</last>
</name>
<acct-no aid_cstyle="ACCT-NO">258929</acct-no>
<fav-color aid_cstyle="FAV-COLOR" hex="#d2b48c">Tan</fav-color>
</person></people>



When we look the output XML, the tag
</person>
<person><name>
is on two separate line. I dont want these tags to be on its own line. This lines should be merged with its above line <fav-color aid_cstyle="FAV-COLOR" hex="#ff0000">Red</fav-color>


Please help me to write XSLT to achive this.

Thanks,
krish
 
Old December 18th, 2009, 08:32 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The whitespace is being copied faithfully from your source document to your output document by the processor.

You can use the <xsl:strip-space elements="people"/> instruction to strip insignificant whitespace from within the <people> element. This may still not produce the exact effect you are after, in which case you will have to write a specific template for the <people> element to handle it how you want.

I still don't see what problem the whitespace is likelyto be causing you - you have lots of other whitespace elsewhere (such as after the opening <people> tag) - why is this presenting you with an issue? Because if you just don't like how it looks you really shouldn't bother.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 18th, 2009, 08:42 AM
Registered User
 
Join Date: Dec 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

The actual problem is, when I import the XML into the Indesign document all this xml tags are come in separate line.

So when I look the Indesign document it have have empty lines (line without text) between the paragrahs. I want to remove this blank lines in Indesign document.

This is the exact reason for avoiding XML tags on its own line.

Thanks,
krish
 
Old December 18th, 2009, 08:49 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well you can simply get rid of all insignificant whitespace by using

<xsl:strip-space elements="*"/>

Then you shouldn't have any problems.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 18th, 2009, 08:54 AM
Registered User
 
Join Date: Dec 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

When I followed you suggession, all the tags and text are comes in a single line.
<?xml version="1.0" encoding="UTF-8"?><people><person><name><first aid_cstyle="FIRST">Bob</first><last aid_cstyle="LAST">Toddson</last></name><acct-no aid_cstyle="ACCT-NO">327598</acct-no><fav-color aid_cstyle="FAV-COLOR" hex="#ff0000">Red</fav-color></person><person><name><first aid_cstyle="FIRST">Phillip</first><last aid_cstyle="LAST">Cardwell</last></name><acct-no aid_cstyle="ACCT-NO">258929</acct-no><fav-color aid_cstyle="FAV-COLOR" hex="#d2b48c">Tan</fav-color></person></people>

I just want to remove the line where there is no actual text and there is only XML element.

Is this possible in XSLT.

Thanks,
krish
 
Old December 18th, 2009, 09:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Yes, which is exactly what you have done.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 18th, 2009, 09:05 AM
Registered User
 
Join Date: Dec 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for you help samjudson.

Regards,
krish





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to avoid duplicate values from the xml file us balasundarrao XSLT 2 June 4th, 2014 11:56 PM
Avoid some tags in .Net web services call moncholv .NET Web Services 0 October 26th, 2009 07:57 PM
Processing ENTITY and NOTATION lines in xml mrame XSLT 2 August 1st, 2008 01:24 PM
avoid self closing tag in xml vinodcprm XSLT 2 July 29th, 2008 04:20 AM
HTML tags to automatically hang bulleted lines? cJeffreywang ASP.NET 2.0 Basics 1 February 6th, 2008 02:12 AM





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