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 June 4th, 2003, 01:20 AM
Registered User
 
Join Date: Jun 2003
Location: , , Netherlands.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to text

Hi all,

I have the following XML-document

Code:
<?xml version="1.0" encoding="utf-8" ?>
<datarows>
<datarow>1.Some text
<br/>bla bla 1<br/></datarow>
<datarow>2.Some text
<br/>bla bla 2<br/></datarow>
</datarows>
And i want to end up with the following textfile:

"1.Some text";"bla bla 1";
"2.Some text";"bla bla 2";

All text in a datarow-element must be on a single line and every br-element must be replaced by a ';'.
I can't seem to remove the newlines that are in the datarow-elements.

Here's my stylesheet. I'm sure it is a minor adjustment.

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="utf-8" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="datarow">
    <xsl:text>"</xsl:text>
    <xsl:apply-templates />
    <br/>
    <xsl:text>";</xsl:text>
  </xsl:template>

  <xsl:template match="br">
    <xsl:text>";"</xsl:text>
  </xsl:template>

</xsl:stylesheet>
Many thanks in advance,
Raoul.
 
Old June 4th, 2003, 11:13 AM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think I have accomplished what you are looking for, try this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="utf-8" />
  <xsl:strip-space elements="*"/>

<xsl:template match="datarows">
    <xsl:for-each select="datarow">
        <xsl:text>"</xsl:text>
            <xsl:apply-templates select="descendant-or-self::*"/>
        <xsl:text>;</xsl:text>

        <xsl:text>#13;</xsl:text>
    </xsl:for-each>
</xsl:template>

<xsl:template match="*">
    <xsl:value-of select="normalize-space(text())"/>
    <xsl:apply-templates select="*"/>
</xsl:template>


I had to make a few changes, such as the recursive template to iterate though each datarow and its descendants to extract the text only. I added the normalize-space function (which is maybe all you needed to do!) to get rid of leading and trailing whitespace in the source. I also output a CR character (#13) instead of the <BR/> tag you were outputting. I'm assuming you wanted a text output, not html. This is a common mistake. You can add the LF char back in, if you need it.

Hope it helps some. I'm sure that there are many other ways to tackle this, but I favor recursion
 
Old June 4th, 2003, 11:34 AM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Looks like I should have review my output before posting. I left out the bla bla.

I'll plug away at it some more
 
Old June 4th, 2003, 11:53 AM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Got it! Try this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="utf-8" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="datarows">
    <xsl:for-each select="datarow">
      <xsl:text>"</xsl:text>
      <xsl:apply-templates />
      <xsl:text>#13;</xsl:text>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="br">
    <xsl:text>";</xsl:text>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:value-of select="normalize-space(.)"/>
  </xsl:template>
</xsl:stylesheet>
 
Old June 4th, 2003, 01:00 PM
Registered User
 
Join Date: Jun 2003
Location: , California, .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to annettetruong
Default

The above solution is almost correct except for missing quotes. This solution seems to work for me.

Annette.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="utf-8" />

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

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

  <xsl:template match="br">
    <xsl:choose>
       <xsl:when test="following-sibling::*">
          <xsl:text>";"</xsl:text>
       </xsl:when>
       <xsl:otherwise>
          <xsl:text>";
</xsl:text>
       </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:value-of select="normalize-space(.)"/>
  </xsl:template>

</xsl:stylesheet>
 
Old June 4th, 2003, 02:30 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for noticing the error. I get excited when I think I have a solution. My original XSLT will now work with this modification. (not to take anything away from your effort, just to have closure on mine):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="utf-8" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="datarows">
    <xsl:for-each select="datarow">
         <xsl:apply-templates />
      <xsl:text>#13;</xsl:text>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="br">
    <xsl:text>";</xsl:text>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="normalize-space(.)"/>
  </xsl:template>
</xsl:stylesheet>
 
Old June 5th, 2003, 08:55 AM
Authorized User
Points: 251, Level: 5
Points: 251, Level: 5 Points: 251, Level: 5 Points: 251, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Canada.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default

Hi Raoul!

This will do the trick. The character #x0D is the carriage return. You can't use <br/> because you want a text output.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="utf-8"/>
    <xsl:template match="datarows">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="datarow">
        <xsl:apply-templates/>
        <xsl:text>#x0D;</xsl:text>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:text>"</xsl:text>
        <xsl:value-of select="normalize-space(.)"/>
        <xsl:text>";</xsl:text>
    </xsl:template>
</xsl:stylesheet>
 
Old June 5th, 2003, 06:24 PM
Registered User
 
Join Date: Jun 2003
Location: , California, .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to annettetruong
Default

Just a technicality...both of the above solutions produce extra semi colons.

E.g.
"1.Some text";"bla bla 1";#x0D;"2.Some text";"bla bla 2";#x0D;

Annette.
 
Old June 6th, 2003, 09:16 AM
Authorized User
Points: 251, Level: 5
Points: 251, Level: 5 Points: 251, Level: 5 Points: 251, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Canada.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default

Quote:
quote:Originally posted by annettetruong
 Just a technicality...both of the above solutions produce extra semi colons.

E.g.
"1.Some text";"bla bla 1";#x0D;"2.Some text";"bla bla 2";#x0D;

Annette.
Hi Annette! It's just that I forgot to put an ampersand before #x0D; (the carriage return).

Have a nice day dear.

RushMan

Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Markup Text to XML ROCXY XSLT 4 October 18th, 2007 05:04 AM
Text String to XML testsubject Visual Studio 2005 1 November 27th, 2006 06:26 AM
Text String to XML testsubject XML 1 November 26th, 2006 06:50 PM
Help generating text from xml billclay XSLT 1 April 21st, 2005 04:18 AM
XML to text (2) raoulvb XSLT 1 June 5th, 2003 09:41 AM





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