Wojciech Owczarczyk
Wojciech Owczarczyk

Reputation: 5735

XSLT inject HTML

I have the following XML:

<person>
      <name>John</name>
      <htmlDescription>A <strong>very</strong> <b><i>nice</i></b> person </htmlDescription>
</person>

I would somehow like to use this XML in XSLT transform to produce HTML such as

A very nice person

is this possible using XSLT?

Upvotes: 0

Views: 1083

Answers (4)

Saxoier
Saxoier

Reputation: 1287

If you transform it to HTML:

<xsl:stylesheet version='1.0'
                xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:output method='html'/>

<xsl:template match='person'>
    <td>
        <xsl:copy-of select='htmlDescription/node()'/>
    </td>
</xsl:template>

</xsl:stylesheet>

If you transform it to XHTML:

<xsl:stylesheet version='1.0'
                xmlns='http://www.w3.org/1999/xhtml'
                xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:output method='xml'/>

<xsl:template match='htmlDescription//text()'>
    <xsl:value-of select='.'/>
</xsl:template>

<xsl:template match='htmlDescription//*'>
    <xsl:element name='{local-name()}'>
        <xsl:copy-of select='@*'/>
        <xsl:apply-templates select='node()'/>
    </xsl:element>
</xsl:template>

<xsl:template match='person'>
    <td>
        <xsl:apply-templates select='htmlDescription/node()'/>
    </td>
</xsl:template>

</xsl:stylesheet>

Upvotes: 3

Patrick B&#233;dert
Patrick B&#233;dert

Reputation: 33

Yes, it is possible. This example works for me:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="person" >
    <xsl:element name="td">
        <xsl:copy-of select="htmlDescription/*"  />
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Kimtho6
Kimtho6

Reputation: 6184

<xsl:template match="person">
    <tr>
        <td>
            <xsl:value-of select="name"/>
        </td>
         <td>
            <xsl:value-of select="htmlDescription"/>
        </td> 
    </tr>  
</xsl:template>

Upvotes: 0

Ruslan
Ruslan

Reputation: 10147

<xsl:template match="person">
    <xsl:element name="td">
        <xsl:value-of select="htmlDescription"/>
    </xsl:element>   
</xsl:template>

Upvotes: 0

Related Questions