Re: [xml] Reading only the text contents of an element



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(), '&#10;' )"/>
  </xsl:template>

  <xsl:template match="*">
    <xsl:value-of select="concat( '# Element ', name(), ':&#10;' )"/>
    <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]