Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename XML tags using XSLT

Tags:

This is my XML-

<CATALOG>     <NAME>C1</NAME>     <CD>         <NAME>Empire Burlesque</NAME>         <ARTIST>Bob Dylan</ARTIST>         <COUNTRY>USA</COUNTRY>         <COMPANY>Columbia</COMPANY>         <PRICE>10.90</PRICE>         <YEAR>1985</YEAR>     </CD>     <CD>         <NAME>Hide your heart</NAME>         <ARTIST>Bonnie Tyler</ARTIST>         <COUNTRY>UK</COUNTRY>         <COMPANY>CBS Records</COMPANY>         <PRICE>9.90</PRICE>         <YEAR>1988</YEAR>     </CD> </CATALOG> 

I want to replace the NAME tag in catalog to CATALOG-NAME and the the NAME tag in CD's to CD-NAME which should make my xml look like this-

<CATALOG>     <CATALOG-NAME>C1</CATALOG-NAME>     <CD>         <CD-NAME>Empire Burlesque</CD-NAME>         <ARTIST>Bob Dylan</ARTIST>         <COUNTRY>USA</COUNTRY>         <COMPANY>Columbia</COMPANY>         <PRICE>10.90</PRICE>         <YEAR>1985</YEAR>     </CD>     <CD>         <CD-NAME>Hide your heart</CD-NAME>         <ARTIST>Bonnie Tyler</ARTIST>         <COUNTRY>UK</COUNTRY>         <COMPANY>CBS Records</COMPANY>         <PRICE>9.90</PRICE>         <YEAR>1988</YEAR>     </CD> </CATALOG> 
like image 755
Srinivas Avatar asked Aug 30 '11 16:08

Srinivas


People also ask

How XML is defined by XSLT?

An XSLT stylesheet is an XML document that consists of a combination of XHTML markup, XSLT template rules, and XPath statements that work together. XHTML markup defines the display environment that XML data is presented in.

Is XML and XSLT the same?

XSLT (eXtensible Stylesheet Language Transformations) is the recommended style sheet language for XML. XSLT is far more sophisticated than CSS. With XSLT you can add/remove elements and attributes to or from the output file.

Is XML with XSLT equivalent to HTML?

Before learning XSLT, we should first understand XSL which stands for EXtensible Stylesheet Language. It is similar to XML as CSS is to HTML.

Is XSL and XSLT the same?

XSLT is designed to be used as part of XSL. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.


1 Answers

Use the identity transform with overrides for the elements you want to rename:

<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:template match="CD/NAME">         <CD-NAME><xsl:apply-templates select="@*|node()" /></CD-NAME>     </xsl:template>     <xsl:template match="CATALOG/NAME">         <CATALOG-NAME><xsl:apply-templates select="@*|node()" /></CATALOG-NAME>     </xsl:template> </xsl:stylesheet> 
like image 168
Wayne Avatar answered Sep 16 '22 18:09

Wayne