How to copy a child record data from source to destination in xslt mapping

I have a source xml as below

<customerSource>
  <Name>abc</Name>
  <Account>123</Account>
  <AccountInfoSource>
    <AccountLocaiton>Florida-MI</AccountLocaiton>
    <AccountZip>12341</AccountZip>
    <AccountBank>BOA</AccountBank>
    <AccountBalance>0.0001</AccountBalance>
    <AccountStatus>Active</AccountStatus>
    .
    .
    .
    .
    .
  </AccountInfoSource>
  <Employed>Yes</Employed>
</customerSource>

I can have any elemets in AccountInfoSource. So, I have to copy AccountInfoSource Record data into AccountInfoDestination, Can you please help?

I'm trying as below

<ns0:customerDestination>
  <ns0:Account>
    <xsl:value-of select="ns0:customerDestination/ns0:Account" />
  </ns0:Account>
  <ns0:AccountInfoDestination>
    <xsl:value-of select="ns0:customerDestination/ns0:AccountInfoSource/text()" />
  </ns0:AccountInfoDestination>
  <ns0:Employed>
    <xsl:value-of select="ns0:customerDestination/ns0:Employed" />
  </ns0:Employed>
</ns0:customerDestination>

The output should looks like this.

<ns0:customerDestination>
  <ns0:Account>123</ns0:Account>
  <ns0:AccountInfoDestination>
    <ns0:AccountLocaiton>Florida-MI</ns0:AccountLocaiton>
    <ns0:AccountZip>12341</ns0:AccountZip>
    <ns0:AccountBank>BOA</ns0:AccountBank>
    <ns0:AccountBalance>0.0001</ns0:AccountBalance>
    <ns0:AccountStatus>Active</ns0:AccountStatus>
    .
    .
    .
    .
    .
  </ns0:AccountInfoDestination>
  <ns0:Employed>Yes</ns0:Employed>
</ns0:customerDestination>

Upvotes: 0

Views: 433

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

You tagged your question with both xslt-1.0 and xslt-2.0, which makes no sense because those tags are meant to be mutually exclusive. The solution below works for both versions of XSLT, because an XSLT 2.0 processor can handle XSLT 1.0 documents as well.

The most important template:

<xsl:template match="AccountInfoSource/* | Account | Employed">

matches all child element nodes of AccountInfoSource, Account and Employed elements. Then, it constructs new elements that are prefixed with ns0::

<xsl:element name="{concat('ns0:',local-name())}">

The text content of those elements is then copied unchanged to the output tree.

Stylesheet

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns0="http://www.namespace.com">

    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" encoding="UTF-8"/>

    <xsl:template match="/customerSource">
        <ns0:customerDestination>
            <xsl:apply-templates/>
        </ns0:customerDestination>
    </xsl:template>

    <xsl:template match="Name"/>

    <xsl:template match="AccountInfoSource">
        <ns0:AccountInfoDestination>
            <xsl:apply-templates/>
        </ns0:AccountInfoDestination>
    </xsl:template>

    <xsl:template match="AccountInfoSource/* | Account | Employed">
        <xsl:element name="{concat('ns0:',local-name())}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

</xsl:transform>

XML Output

Note: The expected output you show is not well-formed because there is the ns0: prefix, but the namespace is not defined.

<ns0:customerDestination xmlns:ns0="http://www.namespace.com">
   <ns0:Account>123</ns0:Account>
   <ns0:AccountInfoDestination>
      <ns0:AccountLocaiton>Florida-MI</ns0:AccountLocaiton>
      <ns0:AccountZip>12341</ns0:AccountZip>
      <ns0:AccountBank>BOA</ns0:AccountBank>
      <ns0:AccountBalance>0.0001</ns0:AccountBalance>
      <ns0:AccountStatus>Active</ns0:AccountStatus>
   </ns0:AccountInfoDestination>
   <ns0:Employed>Yes</ns0:Employed>
</ns0:customerDestination>

Upvotes: 1

Related Questions