Solved

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

Posted on 2015-01-22
2
115 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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…

867 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

16 Experts available now in Live!

Get 1:1 Help Now