Controlling whitespace/newlines in XSLT output -
i've been searching through questions see if particular one's been answered no success. if missed it, apologies.
i have need "flatten" individual xml nodes output single line. whitespace between nodes not needed, , newlines within text nodes tobe replaced special character ('\n').
an example xml:
<events> <event> <id>123</id> <type>read</type> <description> text here </description> </event> </events>
what i'd output is:
<events> <event><id>123</id><type>read</type><description>\nsome text here\n</description></event> </events>
i tried using <xsl:strip-space>
tag, , takes care of whitespace between tags, doesn't touch newlines in side element.
i tried adding following template:
<xsl:template match="text()"> <xsl:copy-of select="translate(.,'
','\n')"/> </xsl:template>
but seems remove tags , output text.
help?
your sample xml input document:
<events> <event> <id>123</id> <type>read</type> <description> text here </description> </event> </events>
given xslt 2.0 transformation:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0"> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="no" omit-xml-declaration="yes"/> <xsl:template match="*|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="text()"> <xsl:value-of select="replace(., '[ \t\r]*\n[ \t\r]*', '\\n')"/> </xsl:template> </xsl:stylesheet>
produces requested xml output document:
<events><event><id>123</id><type>read</type><description>\nsome text here\n</description></event></events>
Comments
Post a Comment