Reputation: 545
Actually as i see i can add name spaces. Because I am very close to output i expect to see. First codes:
XML:
<helptext>
<h6>General configuration options.</h6>
<h2>Changing not yet supported.</h2>
<p>this is a <b>paragraph</b><br/>this is a new line</p>
</helptext>
XSL:
<xsl:template name="transformHelptext">
<xsl:for-each select="./child::*">
<xsl:element name="ht:{local-name()}">
<xsl:choose>
<xsl:when test="count(./child::*)>0">
<xsl:call-template name="transformHelptext"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:for-each>
</xsl:template>
So far so good. There are no problems for <h6>..</h6>
and <h2>...</h2>
lines.
But third line has child node which is a <b>
. And somehow "paragraph" is the only text which is displayed, for this line. I have a mistake in choose
statement. But I cannot figure it out.
Thanks
P.S : ht namespace is defined in xsl-stylesheet tag and it is 'xmlns:ht="http://www.w3.org/1999/xhtml"'
P.S : What I try to do is, making it possible to apply html tags, styles on my specific xml nodes
Upvotes: 0
Views: 1580
Reputation: 43033
Input XML :
<?xml version="1.0" encoding="UTF-8"?>
<helptext>
<h6>General configuration options.</h6>
<h2>Changing not yet supported.</h2>
<p>this is a <b>paragraph</b><br/>this is a new line</p>
</helptext>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*|@*">
<xsl:element name="ht:{local-name()}" namespace="http://www.w3.org/1999/xhtml">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
Output XML :
<?xml version="1.0" encoding="UTF-8"?>
<ht:helptext xmlns:ht="http://www.w3.org/1999/xhtml">
<ht:h6>General configuration options.</ht:h6>
<ht:h2>Changing not yet supported.</ht:h2>
<ht:p>
this is a
<ht:b>paragraph</ht:b>
<ht:br />
this is a new line
</ht:p>
</ht:helptext>
Discussion :
As much as possible, avoid using <xsl:for-each>
as it can slow down the processor.
Upvotes: 1
Reputation: 12817
Maybe something like this instead:
<xsl:template name="transformHelptext">
<xsl:apply-templates select="@*|node()" />
</xsl:template>
<xsl:template match="*" >
<xsl:element name="ht:{local-name(.)}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:template>
<xsl:template match="@*|text()" >
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
Inside the "transformHelptext" templeate, select all attributes and nodes and apply templates to them.
The second template matches element nodes and changes the namespace. The third template match attributes and text nodes and just creates a copy.
Upvotes: 1