How to rename root and children element XSLT -


how rename root 'channel' , children 'item' 'records' , 'record' respectively?

input

<?xml version="1.0" encoding="utf-8"?> <rss version="2.0" xmlns:atom="http://www.w3.org/2005/atom"> <channel> <item> <title>a</title> <link>/news/view/25857/a.html</link> <guid ispermalink="true">/news/view/25857/a.html</guid> <comments>/news/25857/a.html</comments> <pubdate>sat, 03 oct 2015 00:42:42 gmt</pubdate> <description><![cdata[]]></description> <category>headline,hacker,bank,cybercrime,data loss,fraud</category> </item> </channel> </rss> 

xslt

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">     <xsl:output method="xml" indent="yes"/>      <xsl:template match="@* | node()">         <xsl:copy>             <xsl:apply-templates select="@* | node()"/>         </xsl:copy>     </xsl:template>       <xsl:template match="item/comments">         <taglink>             <xsl:text></xsl:text>             <xsl:value-of select="text()" />         </taglink>     </xsl:template>      <!--delimits values if separated comma-->     <xsl:template match="item/category[contains(.,',')]">         <category>             <xsl:variable name="elementname" select="name(..)"/>              <xsl:call-template name="splitintoelements">                 <xsl:with-param name="basename" select="name(..)" />                 <xsl:with-param name="txt" select="." />                 </xsl:call-template>         </category>     </xsl:template>      <xsl:template name="splitintoelements">          <xsl:param name="basename" />         <xsl:param name="txt" />         <xsl:param name="delimiter" select="','" />         <xsl:param name="index" select="1" />          <xsl:variable name="first" select="substring-before($txt, $delimiter)" />         <xsl:variable name="remaining" select="substring-after($txt, $delimiter)" />          <xsl:element name="value">             <xsl:choose>                 <xsl:when test="$first">                     <xsl:value-of select="$first" />                 </xsl:when>                 <xsl:otherwise>                     <xsl:value-of select="$txt" />                 </xsl:otherwise>             </xsl:choose>         </xsl:element>              <xsl:if test="$remaining">             <xsl:call-template name="splitintoelements">                 <xsl:with-param name="basename" select="$basename" />                 <xsl:with-param name="txt" select="$remaining" />                 <xsl:with-param name="index" select="$index" />                 <xsl:with-param name="delimiter" select="$delimiter" />             </xsl:call-template>         </xsl:if>     </xsl:template> </xsl:stylesheet> 

output

<?xml version="1.0" encoding="utf-8"?> <rss xmlns:atom="http://www.w3.org/2005/atom" version="2.0">    <channel>       <item>          <title>a</title>          <link>/news/view/25857/a.html</link>          <guid ispermalink="true">/news/view/25857/a.html</guid>          <taglink>/news/25857/a.html</taglink>          <pubdate>sat, 03 oct 2015 00:42:42 gmt</pubdate>          <description/>          <category>             <value>headline</value>             <value>hacker</value>             <value>bank</value>             <value>cybercrime</value>             <value>data loss</value>             <value>fraud</value>          </category>       </item>    </channel> </rss> 

expected output - there many 'item' within 'channel' there expected have many 'records' within 'record'

<?xml version="1.0" encoding="utf-8"?> <rss xmlns:atom="http://www.w3.org/2005/atom" version="2.0">    <records>       <record>          <title>a</title>          <link>/news/view/25857/a.html</link>          <guid ispermalink="true">/news/view/25857/a.html</guid>          <taglink>/news/25857/a.html</taglink>          <pubdate>sat, 03 oct 2015 00:42:42 gmt</pubdate>          <description/>          <category>             <value>headline</value>             <value>hacker</value>             <value>bank</value>             <value>cybercrime</value>             <value>data loss</value>             <value>fraud</value>          </category>       </record>    </records> </rss> 

you can add couple of templates match element renamed, , spit out new element name, example :

<xsl:template match="channel">     <records>         <xsl:apply-templates/>     </records> </xsl:template>  <xsl:template match="item">     <record>         <xsl:apply-templates/>     </record> </xsl:template> 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -