[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

XSL output method="text" and indent preservation

Subject: XSL output method="text" and indent preservation
From: selva <selva@xxxxxxxxxxxx>
Date: Tue, 31 Jul 2001 11:00:02 +0530
xsl output method text
Hi,
I am trying to convert an XML document to a text document. While converting,
I would like to preserve the indentation from the xml source file. I am
using Xalan(1.2.2). The code snippet to convert the XML to TEXT is as
follows.

XMLTextConverter.java
--------------------------------
import java.io.*;
import org.xml.sax.SAXException;
import org.apache.xalan.xslt.XSLTProcessorFactory;
import org.apache.xalan.xslt.XSLTInputSource;
import org.apache.xalan.xslt.XSLTResultTarget;
import org.apache.xalan.xslt.XSLTProcessor;

public class XMLTextConverter {
 public static void main(String[] args) {
    try {
        XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
        processor.process(new XSLTInputSource("myxml.xml"),  new
XSLTInputSource("myxsl.xsl"),  new XSLTResultTarget("mytext.txt"));
    } catch (org.xml.sax.SAXException ex) {
        System.out.println(ex);
    } catch (Exception ex) {
        System.out.println(ex);
    }
 }
}

myxml.xml
----------------
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <myelement>
        <childelement> childvalue </childelement>
        <childparent>
             <grandchild> grandchildvalue </grandchild>
        </childparent>
    </myelement>
</root>

myxsl.xsl:
---------------
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text" indent="yes"/>

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

<!-- myelement -->
<xsl:template match="myelement">
    <xsl:text>&#x0A;ChildName </xsl:text><xsl:value-of
select="childelement"/><xsl:apply-templates/>
</xsl:template>

<!-- childparent -->
<xsl:template match="childparent">
    <xsl:text>&#x0A;GrandChildName </xsl:text><xsl:value-of
select="grandchild"/>
</xsl:template>

<!-- text() -->
<xsl:template match="text()"/>

</xsl:stylesheet>

Expected output: (Preserving the indentation from the xml source file
-----------------------
    ChildName  childvalue
        GrandChildName  grandchildvalue

Actual output:
-------------------
ChildName  childvalue
GrandChildName  grandchildvalue


How can the indentation from the source xml file can be preserved in this
case. Any pointers please?

Thanks & Regards,
Selva


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.