Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…

685 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