2

I am trying to write an XSLT program where i would need to perform below steps.

  1. add some extra static xml fields
  2. Append some characters to each xml tag
  3. Occurance should also be consiered
  4. 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>
0

1 Answer 1

1

I see that you need namespaces, add a namespace to the element function, and it will all be handled with the XSLT-processor, depending and correct in conjunction with the declared namespaces in top of the result-xml.

Like this:

<xsl:param name="ns" select="string('https://a_namespace/')"/>

<xsl:element name="AnElement" namespace="{$ns}">

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.