xml - How to add <p> tags around text nodes using XSLT -
i have following xml document
<body> <h2>title</h2> text , <a href="link">link</a> here. </body>
i want transform using xslt into:
<body> <h2>title</h2> <p>some text , <a href="link">link</a> here.</p> </body>
therefore tried following xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" method="xml" cdata-section-elements="script"/> <xsl:template match="/ | node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*"/> </xsl:copy> </xsl:template> <xsl:template match="body/text()" > <p><xsl:copy/></p> </xsl:template> </xsl:stylesheet>
but doesn't seem give expected result (it works fine if text node not contain anchor element). idea of how accomplish xslt? (i have option parse xml using c# later on, initial idea use xslt)
update
to make overall requirements bit more clear, input xml (or xhtml) not fixed, can user input. thing can expect it, valid xml (xhtml) , lines might not wrapped in <p>
tag .
how about:
<xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="body"> <xsl:copy> <xsl:apply-templates select=" @*|*[not(self::a)]"/> <p><xsl:copy-of select="text()|a"/></p> </xsl:copy> </xsl:template>
Comments
Post a Comment