Reputation: 392
Given the following XML code:
<section>
<p>Text...</p>
<p>More text...</p>
<special>Text with formatting</special>
<p>More text..</p>
</section>
I want to use XSLT to output this - but the <special>
tag must be with the HTML tag <pre></pre>
around it.
Can't figure out how to make it appear in the right order with all ofte p-elements inside the section. Help appreciated.
Upvotes: 0
Views: 52
Reputation: 167516
I would start with the identity transformation template
<xsl:template match="@* | node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
and then add templates for the elements needing special treatment e.g.
<xsl:template match="special">
<pre>
<xsl:call-template name="identity"/>
</pre>
</xsl:template>
Upvotes: 1
Reputation: 22617
This is how you could do it:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="section">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
<xsl:template match="special">
<xsl:element name="pre">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The crucial bit is the xsl:element that constructs the new element "pre" that you need.
In future questions, please take the time to show what you have tried so far. Instead of merely saying you could not get it to work.
Upvotes: 1