Reputation: 1
I have a common set of tags that need to be wrapped in different wrapper elements. Sample input XML is like-
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<a>Hello there</a>
<code>FJ-123-99</code>
<isPopular>True</isPopular>
<timestamp>2019-10-17 07:57:23</timestamp>
<pop>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<about>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</about>
</pop>
<classic>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<about>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</about>
</classic>
<retro>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<about>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</about>
</retro>
</catalog>
sample output
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<a type="primary">Hello there</a>
<typeCode>FJ12399</typeCode>
<isPopular>Y</isPopular>
<timestamp>20191017:075723</timestamp>
<pop>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<about>
<organization>Columbia</organization>
<amount>10.90</amount>
<releaseTime>1985</releaseTime>
</about>
</pop>
<classic>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<about>
<organization>CBS Records</organization>
<amount>9.90</amount>
<releaseTime>1988</releaseTime>
</about>
</classic>
<retro>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<about>
<organization>USA</organization>
<amount>9.90</amount>
<releaseTime>1982</releaseTime>
</about>
</retro>
</catalog>
Here, <title>
<artist>
and <about>
are common for main wrappers like <pop>
<retro>
and <classic>
.
My question is that how do I apply these common templates to my main wrappers I am able to do all the transformations like- converting timestamp, removing hyphens, changing tag name.
Upvotes: 0
Views: 60
Reputation: 167716
Start with the identity transformation as the base template i.e.
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
That alone would copy everything recursively level by level, but if you add your own templates for the elements or nodes in general you want to transform you should get what you want, i.e. in your case copy/preserve e.g. mathBooks
, englishBook
or scienceBook
and have your templates for e.g. address
or author
kick in for the small changes they do; after your edit it appears you want to change the catalog/a
element for instance to add an attribute so start with the template shown above and add e.g.
<xsl:template match="catalog/a">
<a type="primary">
<xsl:apply-templates/>
</a>
</xsl:template>
With that approach you can add a template for any further change you need to apply to a different node or element.
Upvotes: 0