Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT output html tags from XML

Tags:

xml

xslt

I have an XML document similar to:

<tag>
   <content>adsfasdf<b>asdf</b></content>
</tag>

I would like for the XSLT to select the content element and show all of the content:

<xsl:value-of select="/tag/content"/> 

The XSLT is configured to render as HTML. Is there a way that I can get the value-of/copy-of to display the exact content without having to render it?

What I'm looking for is

asdfasdf<b>asdf</b> 

And not:

asdfasdf asdf

like image 952
monksy Avatar asked Feb 03 '11 05:02

monksy


People also ask

How do you display XML elements in an HTML table using XSLT?

Create an XSL stylesheet.xml version=”1.0″?> To bind the XML elements to a HTML table, the <for-each> XSL template must appear before each table row tag. This ensures that a new row is created for each <book> element. The <value-of> template will output the selected text of each child element into a separate table.

How can XSLT transform XML into HTML explain with example?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

Can XML generate HTML?

You can generate HTML documentation from an XML Schema file. The HTML documentation contains various information about the message model that is included in the XML Schema file. This documentation can be useful for providing a summary of the contents of your XML Schema file.

Is XML with XSLT equivalent to HTML?

With XSLT you can transform an XML document into HTML.


2 Answers

You need to escape the tag names within the content, I'd recommend something like:

<xsl:template match="content//*">
    <xsl:value-of select="concat('&lt;',name(),'&gt;')"/>
    <xsl:apply-templates/>
    <xsl:value-of select="concat('&lt;/',name(),'&gt;')"/>
</xsl:template>

which you then can call with:

<xsl:apply-templates select="/tag/content"/>
like image 73
Nick Jones Avatar answered Nov 15 '22 06:11

Nick Jones


Quick and dirty way:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="content">
        <xsl:copy-of select="text() | *"/>
    </xsl:template>
</xsl:stylesheet>

Result against your sample will be:

adsfasdf<b>asdf</b>

Another approach:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="b">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
like image 29
Flack Avatar answered Nov 15 '22 06:11

Flack