Solved

change name of xml tags with xslt if there other tag present.

Posted on 2015-01-22
2
112 Views
Last Modified: 2015-01-22
Hi i Have the following xml, just have to change the the tag name based on the existence of another tag, could anyone please direct me how to do this in xslt.

current xml:
<Details>
                        <Cash>
                                    <TitleLogo> cash.gif</TitleLogo>
                                    <Title> Benefits</Title>
                                    <Approval>
                                                <Title>Who qualifies?</Title>
                                                <TitleLogo> eligible.gif</TitleLogo>
                                                <Header1>Who qualifies?</Header1>
                                                <Header2>When?</Header2>
                                                <Header3>How Much?</Header3>
                                                <Qualified>
                                                            <Person>
                                                                        <Names>\b.PAT, PEN, TAM, TON\b0.\n\n\n\n\n.</Names>
                                                                        <When>Dec 26, 2014</When>
                                                                        <HowMuch>$158.00 twice per month</HowMuch>
                                                            </Person>
                                                </Qualified>
                                                <Footer>\b.PAT, PEN, TAM, TON\b0.: To get this benefit, you and all members of your amily must be working and have earned income that we use to determine your monthly benefit.\n\n.</Footer>
                                    </Approval>
                        </Cash>
                        <Cash>
                                    <TitleLogo> cash.gif</TitleLogo>
                                    <Title> Benefits</Title>
                                    <Rejection>
                                                <Title>Who does not qualify?</Title>
                                                <TitleLogo>d:\Apps\MCI\SAT\Adobe\Corr\Logo\icon_elig_ineligible.gif</TitleLogo>
                                                <Header1>Who?</Header1>
                                                <Header2>Date of our decision?</Header2>
                                                <Description>
                                                            <Reason>
                                                                        <Value1>\b.TAM, TON\b0.</Value1>
                                                                        <Value2>December 16, 2014\n\n.</Value2>
                                                            </Reason>
                                                </Description>
                                                <Footer>\b.TAM, TON\b0.: Inside this letter you can learn more about the income and deductions we used to decide if you qualify</Footer>
                                    </Rejection>
                        </Cash>
<Details>

Open in new window



need to change to below xml using xslt. the 2nd <cash> tag should be changed to <cashR> if there is a <Rejection> tag exists.



<Details>
                        <Cash>
                                    <TitleLogo> cash.gif</TitleLogo>
                                    <Title> Benefits</Title>
                                    <Approval>
                                                <Title>Who qualifies?</Title>
                                                <TitleLogo> eligible.gif</TitleLogo>
                                                <Header1>Who qualifies?</Header1>
                                                <Header2>When?</Header2>
                                                <Header3>How Much?</Header3>
                                                <Qualified>
                                                            <Person>
                                                                        <Names>\b.PAT, PEN, TAM, TON\b0.\n\n\n\n\n.</Names>
                                                                        <When>Dec 26, 2014</When>
                                                                        <HowMuch>$158.00 twice per month</HowMuch>
                                                            </Person>
                                                </Qualified>
                                                <Footer>\b.PAT, PEN, TAM, TON\b0.: To get this benefit, you and all members of your amily must be working and have earned income that we use to determine your monthly benefit.\n\n.</Footer>
                                    </Approval>
                        </Cash>
                       [b] <CashR>[/b]
                                    <TitleLogo> cash.gif</TitleLogo>
                                    <Title> Benefits</Title>
                                    <Rejection>
                                                <Title>Who does not qualify?</Title>
                                                <TitleLogo>d:\Apps\MCI\SAT\Adobe\Corr\Logo\icon_elig_ineligible.gif</TitleLogo>
                                                <Header1>Who?</Header1>
                                                <Header2>Date of our decision?</Header2>
                                                <Description>
                                                            <Reason>
                                                                        <Value1>\b.TAM, TON\b0.</Value1>
                                                                        <Value2>December 16, 2014\n\n.</Value2>
                                                            </Reason>
                                                </Description>
                                                <Footer>\b.TAM, TON\b0.: Inside this letter you can learn more about the income and deductions we used to decide if you qualify</Footer>
                                    </Rejection>
                        [b]</CashR>[/b]
<Details>

Open in new window

0
Comment
Question by:sri1209
2 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 40564850
Try this

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Cash">
        <xsl:variable name="ename">
            <xsl:text>Cash</xsl:text>
            <xsl:if test="Rejection">
                <xsl:text>R</xsl:text>
            </xsl:if>
        </xsl:variable>
        <xsl:element name="{$ename}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:element>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window


The first template is an identity transform
The second has a conditional element name
0
 

Author Comment

by:sri1209
ID: 40564908
Thanks Geert Bormans, you are awesome. i have sent you an private email
 just to thank you for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now