I need to change some of the element names in the original XML. I am trying to do this with XSLT, but can\'t get it to work.
Here is a sample of XML:
&l
The following template achieves the result you're looking for:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<!-- Copy all elements and attributes from the source to the target -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- Transform the given elements to divs with class names matching the source element names -->
<xsl:template match="itemtitle|poll|pcredit">
<div class="{local-name(.)}">
<xsl:value-of select="."/>
</div>
</xsl:template>
<!-- Transform the section element to a div of class 'issuehead' -->
<xsl:template match="section">
<div class="issuehead">
<xsl:value-of select="."/>
</div>
</xsl:template>
</xsl:stylesheet>
If you'd like more well formed markup modify the xsl:output tag as follows:
<xsl:output method="xml"
version="1.0"
encoding="UTF-8"
indent="yes"
omit-xml-declaration="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
/>
For anything like this, start with an identity transform:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node( ) | @*">
<xsl:copy>
<xsl:apply-templates select="@* | node( )"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
This will just copy every node. But then you add additional templates to do what you need:
<xsl:template match="section">
<div class="issuehead">
<xsl:apply-templates select="@* | node( )"/>
</div>
</xsl:template>
This pattern should get you what you want.