Solved

need to modify the xml through xslt

Posted on 2015-01-06
3
158 Views
Last Modified: 2015-01-13
Hi ,

I have the below xml, i need to change the name of the attribute through xslt, any suggestions on how to implement this. Thank you in advance.

current xml:

<Cor>
<Ics>
                                    <Captions>
                                                <Caption id="A">Your total reported Income</Caption>
                                                <Caption type="OrdinaryDeduction">
                                                            <Text>-Deductions</Text>
                                                            <Item id="D10">Personal Needs Allowance </Item>
                                                </Caption>
                                                <Caption id="C">=Payment Towards Your Cost of Care.</Caption>
                                                <Caption type="MedicalDeduction">
                                                            <Text>Medical Expense Deductions:</Text>
                                                            <Item id="M1">Medicare Premium</Item>
                                                            <Item id="M2">Other Insurance Premium</Item>
                                                </Caption>
                                    </Captions>
 
</Ics>
</cor>


i need to modify as below:



<Cor>
<Ics>
                                    <Captions>
                                                <Caption id="A">Your total reported Income</Caption>
                                                <Caption id="OrdinaryDeduction">
                                                            <Text>-Deductions</Text>
                                                            <Item id="D10">Personal Needs Allowance </Item>
                                                </Caption>
                                                <Caption id="C">=Payment Towards Your Cost of Care.</Caption>
                                                <Caption id="MedicalDeduction">
                                                            <Text>Medical Expense Deductions:</Text>
                                                            <Item id="M1">Medicare Premium</Item>
                                                            <Item id="M2">Other Insurance Premium</Item>
                                                </Caption>
                                    </Captions>
 
</Ics>
</cor>
0
Comment
Question by:sri1209
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 40534880
The following code should do what you want...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Caption/@type">
        <xsl:attribute name="id">
            <xsl:value-of select="." />
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:sri1209
ID: 40535729
Hi mccarl,

Thank you very much  it worked .. can you please let me know the purpose of "."  in

<xsl:value-of select="." />
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40536790
The "." simply refers to the current node that is being processed. In this case the template has matched a "type" attribute on a "Caption" element (and we haven't done any other processing that changes what node is the current node) so the current node in this context is the "type" attribute. So the xsl:value-of returns the value of the current node which is the value of the type attribute. And due to the <xsl:attribute> element that is around it, that means we are creating an "id" attribute that contains the value-of the "type" attribute.

Hope that helps, and I'm glad that it is working for you!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

911 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

21 Experts available now in Live!

Get 1:1 Help Now