1

How to move a child node element value to another child node value using xslt. I need to replace the dummy city with the actual city value from a different child node.

In the below example i need to move the city from Address child node to the Name child node.

Source XML:

<?xml version="1.0" encoding="UTF-8"?>
<M:Top_Node xmlns:M="urn:com">
<M:First_Node>
    <M:Employee>
        <M:Name>
            <M:Employee_ID>001</M:Employee_ID>
            <M:Name>sam</M:Name>
            <M:City>Dummy_City_CA</M:City>
        </M:Name>
        <M:Address>
            <M:City>Actual_City_CA</M:City>
            <M:State>CA</M:State>
        </M:Address>
    </M:Employee>
    <M:Employee>
        <M:Name>
            <M:Employee_ID>002</M:Employee_ID>
            <M:Name>Van</M:Name>
            <M:City>Dummy_City_NY</M:City>
        </M:Name>
        <M:Address>
            <M:City>Actual_City_NY</M:City>
            <M:State>NY</M:State>
        </M:Address>
    </M:Employee>
</M:First_Node>

Expected Output:

<?xml version="1.0" encoding="UTF-8"?>
<M:Top_Node xmlns:M="urn:com">
<M:First_Node>
    <M:Employee>
        <M:Name>
            <M:Employee_ID>001</M:Employee_ID>
            <M:Name>sam</M:Name>
            <M:City>Actual_City_CA</M:City>
        </M:Name>
        <M:Address>
            **<M:City>Actual_City_CA</M:City>**
            <M:State>CA</M:State>
        </M:Address>
    </M:Employee>
    <M:Employee>
        <M:Name>
            <M:Employee_ID>002</M:Employee_ID>
            <M:Name>Van</M:Name>
            **<M:City>Actual_City_NY</M:City>**
        </M:Name>
        <M:Address>
            <M:City>Actual_City_NY</M:City>
            <M:State>NY</M:State>
        </M:Address>
    </M:Employee>
</M:First_Node>

1 Answer 1

2

Write a template for that element

<xsl:template match="M:Employee/M:Name/M:City">
  <xsl:copy-of select="../../M:Address/M:City"/>
</xsl:template>

plus the identity transformation template of course.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.