Re: [xml] Reading only the text contents of an element
- From: Michael Ludwig <milu71 gmx de>
- To: xml gnome org
- Subject: Re: [xml] Reading only the text contents of an element
- Date: Mon, 9 Aug 2010 21:12:52 +0200
nkat nkat schrieb am 05.08.2010 um 15:44 (-0700):
I am using the following code to read the element contents from an xml
file. I just need the text content:
But in the output I get empty lines for those elements which do not
have text content but have child elements in them. How do i avoid
those blank lines from being printed.
I'd say, use the right tool, which is XSLT:
\,,,/
(o o)
------oOOo-(_)-oOOo------
<?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" />
<xsl:strip-space elements="*"/>
<xsl:template match="text()">
<xsl:value-of select="concat( normalize-space(), ' ' )"/>
</xsl:template>
<xsl:template match="*">
<xsl:value-of select="concat( '# Element ', name(), ': ' )"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
-------------------------
Call it as follows:
xsltproc milu.xsl your.xml
This should give you approximately the output you want.
--
Michael Ludwig
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]