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
  #1 (permalink)  
Old June 5th, 2003, 02:53 AM
Registered User
 
Join Date: Jun 2003
Location: , , Netherlands.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to text (2)

Hi all,

I have the following xml-document.

Code:
<?xml version="1.0" ?>
<datarows>
  <datarow class="a">Line 0</datarow>
  <datarow class="b">Subline 0</datarow>
  <datarow class="b">Subline 1</datarow>
  <datarow class="b">Subline 2</datarow>
  <datarow class="a">Line 1</datarow>
  <datarow class="b">Subline 10</datarow>
  <datarow class="b">Subline 11</datarow>
  <datarow class="b">Subline 12</datarow>
</datarows>
And i want to end up with a textfile like:

"Line0";"Subline 0";"Subline 1";"Subline 2"
"Line1";"Subline 10";"Subline 11";"Subline 12"

I have made the following stylesheet.

Code:
<?xml version="1.0" encoding="iso-8859-1" ?>
<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[datarow[@class='b']]">
    <xsl:apply-templates select="preceding-sibling::datarows[datarow[@class='a']]" />
    <xsl:apply-templates select="datarow" />
  </xsl:template>

  <xsl:template match="datarow">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="normalize-space(.)" />
    <xsl:choose>
      <xsl:when test="position() = last()">
        <xsl:text>"#13;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>";</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
First, I can't figure out how to get two lines in the outputdocument.
Second, i want to ask you if there is a way 'to speed up the stylesheet'.
When i have a document that is 10Mb, the time to transform the document can be very long.
The main issue here is, that time the parser encounters a datarow-element with class 'b' it has to search back in the tree to find the previous datarow-element 'a'.
If you have a very big document, you can imagine what happens.
Is it a good idea to work with a xsl:variable or maybe someone has a different approach?

Thanks in advance, Raoul.
  #2 (permalink)  
Old June 5th, 2003, 09:41 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

Chack this out, it does the trick. Regarding large documents, you should probably turn to SAX to parse your documents and generate events to handle the transformation instead of XSLT.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="datarow">
        <xsl:variable name="class" select="@class"/>
        <xsl:if test="$class='a'">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>"</xsl:text>
            <xsl:if test="boolean(following-sibling::node())">
                <xsl:text>;</xsl:text>
            </xsl:if>
            <xsl:call-template name="subclass"/>
            <xsl:text>#x0d;</xsl:text>
        </xsl:if>
    </xsl:template>
    <xsl:template name="subclass">
        <xsl:variable name="parent" select="."/>
        <xsl:for-each select="following-sibling::*[@class='b' and preceding-sibling::*[@class='a'][1]=$parent]">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>"</xsl:text>
            <xsl:if test="following-sibling::datarow[1]/@class != 'a'">
                <xsl:text>;</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>


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 raoulvb XSLT 8 June 6th, 2003 09:16 AM





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