• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

need to modify the xml through xslt

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
sri1209
Asked:
sri1209
  • 2
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
 
sri1209Author Commented:
Hi mccarl,

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

<xsl:value-of select="." />
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now