Solved

need to modify the xml through xslt

Posted on 2015-01-06
3
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 36

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 36

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

728 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