0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your reply. But it does show just plain text unless browser is firefox.
@savruk maybe the xml headers are missing
Norman: If you go this way, then there is no need for apply templates to unexistent attributes or child nodes of attributes or text nodes. Just use xsl:copy-of select="." or even xsl:copy as content template. Do note that there could be comments and processing instructions as well.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.