Solved

need to modify the xml through xslt

Posted on 2015-01-06
3
156 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now