xslt - child element replaced with a new element name -
i'm pretty new xslt world. can me out question? have input xml, , desired output xml. need write xslt transformation. condition: if element ends "11" child element node "title" (if exist) title replaced cdtitle i.e, cd11/title cd11/cdtitle
input xml --------------- <catalog> <cd11> <title>empire burlesque</title> <artist>bob dylan</artist> <year>1985</year> </cd11> <cd22> <title>empire burlesque</title> <artist>bob dylan</artist> <year>1985</year> </cd22> <cd33> <title>empire burlesque</title> <artist>bob dylan</artist> <year>1985</year> </cd33> </catalog> output xml --------------- <catalog> <cd11> <cdtitle>empire burlesque</cdtitle> <artist>bob dylan</artist> <year>1985</year> </cd11> <cd22> <title>empire burlesque</title> <artist>bob dylan</artist> <year>1985</year> </cd22> <cd33> <title>empire burlesque</title>`enter code here` <artist>bob dylan</artist> <year>1985</year> </cd33> </catalog>
when want output similar input changes, start identity transform , add customization desire.
so start copies everything:
<xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> then add want unique in more specific match. in case state "any title parent ends '11'". write way:
<xsl:template match="title[substring(name(parent::*),string-length(name(parent::*)) - 1, 2) = '11']"> putting together:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:template match="title[substring(name(parent::*),string-length(name(parent::*)) - 1, 2) = '11']"> <cdtitle> <xsl:apply-templates /> </cdtitle> </xsl:template> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> output is:
<catalog> <cd11> <cdtitle>empire burlesque</cdtitle> <artist>bob dylan</artist> <year>1985</year> </cd11> <cd22> <title>empire burlesque</title> <artist>bob dylan</artist> <year>1985</year> </cd22> <cd33> <title>empire burlesque</title> <artist>bob dylan</artist> <year>1985</year> </cd33> </catalog
Comments
Post a Comment