I am trying to write an XSLT program where i would need to perform below steps.
- add some extra static xml fields
- Append some characters to each xml tag
- Occurance should also be consiered
- If any empty tags or no tags comes in input payload then it should not create that particular tag in output
Can someone please help me out on the same. Thank you.
Input XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Header>
<Item1>
<text>data1</text>
<text2>data2</text2>
</Item1>
<ItemMAIN>
<Item2>
<text3>data3</text3>
<text4>data4</text4>
</Item2>
</ItemMAIN>
<Item3>
<text5>data5</text5>
<text6>data6</text6>
</Item3>
<Item3>
<text5>data7</text5>
<text6>data8</text6>
</Item3>
<Item3>
<text5>data9</text5>
</Item3>
<Item4>
<text5>data10</text5>
<text6/>
</Item4>
</Header>
</root>
Output XML :
<soap:TestEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:mp="http://example.com" xmlns:kt="http://exampleKT.com" >
<soap:Headdata>
<kt:name>
<mp:Item1>
<mp:text>data1</mp:text>
<mp:text2>data2</mp:text2>
</mp:Item1>
</kt:name>
</soap:Headdata>
<soap:Bodydata1>
<kt:name>
<mp:Item2>
<mp:text3>data3</mp:text3>
<mp:text4>data4</mp:text4>
</mp:Item2>
</kt:name>
</soap:Bodydata1>
<soap:Bodydata2>
<kt:name>
<mp:Item3>
<mp:text5>data5</mp:text5>
<mp:text6>data6</mp:text6>
</mp:Item3>
</kt:name>
<kt:name>
<mp:Item3>
<mp:text5>data7</mp:text5>
<mp:text6>data8</mp:text6>
</mp:Item3>
</kt:name>
<kt:name>
<mp:Item3>
<mp:text5>data9</mp:text5>
</mp:Item3>
</kt:name>
</soap:Bodydata2>
<soap:Bodydata3>
<kt:name>
<mp:Item4>
<mp:text5>data10</mp:text5>
</mp:Item4>
</kt:name>
</soap:Bodydata3>
</soap:TestEnvelope>
Tried XSLT Code : I tried like below but it gives me impression that i am trying to hardcode each field. But would like to have a code that i can use if there is any extra xml field in future should also be handled.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<soap:TestEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:mp="http://example.com" xmlns:kt="http://exampleKT.com" >
<soap:Headdata>
<xsl:for-each select="/root/Header">
<kt:name>
<mp:text>
<xsl:value-of select="Item1/text"/>
</mp:text>
<mp:text2>
<xsl:value-of select="Item1/text2"/>
</mp:text2>
</kt:name>
</soap:Headdata>
</xsl:for-each>
<xsl:for-each select="/root/Header/ItemMAIN">
<soap:Bodydata1>
<kt:name>
<mp:text3>
<xsl:value-of select="Item2/text3"/>
</mp:text3>
<mp:text4>
<xsl:value-of select="Item2/text4"/>
</mp:text4>
</kt:name>
</soap:Bodydata1>
</xsl:for-each>
:
:
:
</xsl:template>
<xsl:template match="*[not(*)][not(normalize-space())]" />
</xsl:stylesheet>