This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
XSL output method="text" and indent preservation
- To: xsl-list at lists dot mulberrytech dot com
- Subject: [xsl] XSL output method="text" and indent preservation
- From: selva <selva at india dot hp dot com>
- Date: Tue, 31 Jul 2001 11:00:02 +0530
- Organization: Hewlett Packard
- References: <002801c118fd$297c77a0$d4373c3e@PCUKMKA>
- Reply-To: xsl-list at lists dot mulberrytech dot com
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>
ChildName </xsl:text><xsl:value-of
select="childelement"/><xsl:apply-templates/>
</xsl:template>
<!-- childparent -->
<xsl:template match="childparent">
<xsl:text>
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