0
votes

I want to copy nodes from parent to the child. I am not really sure how this can be acheived.

My source xml

<Root1>
    <Family1>
      <Childrens>
        <Child>
          <ChildFirstName>Robin1</ChildFirstName>
          <ChildLastName>Jackman1</ChildLastName>
          <Address>
            <Street1>Street1</Street1>
            <State>State</State>
            <Country>Country</Country>
          </Address>
          <Activities>
            <Sports>
              <Cricket>
                <ParticipentNames>

                </ParticipentNames>
              </Cricket>
            </Sports>
          </Activities>
        </Child>
        <Child>
          <ChildFirstName>Robin2</ChildFirstName>
          <ChildLastName>Jackman2</ChildLastName>
          <Address>
            <Street1>Street1</Street1>
            <State>State</State>
            <Country>Country</Country>
          </Address>
          <Activities>
            <Sports>
              <Cricket>
                <ParticipentNames>

                </ParticipentNames>
              </Cricket>
            </Sports>
          </Activities>
        </Child>
        <Child>
          <ChildFirstName>Robin3</ChildFirstName>
          <ChildLastName>Jackman3</ChildLastName>
          <Address>
            <Street1>Street1</Street1>
            <State>State</State>
            <Country>Country</Country>
          </Address>
          <Activities>
            <Sports>
              <Cricket>
                <ParticipentNames>

                </ParticipentNames>
              </Cricket>
            </Sports>
          </Activities>
        </Child>
      </Childrens>
    </Family1>
  </Root1>

I would like to get the output as

<Root1>
    <Family1>
      <Childrens>
        <Child>
          <ChildFirstName>Robin1</ChildFirstName>
          <ChildLastName>Jackman1</ChildLastName>
          <Address>
            <Street1>Street1</Street1>
            <State>State</State>
            <Country>Country</Country>
          </Address>
          <Activities>
            <Sports>
              <Cricket>
                <ParticipentNames>
                  <ChildFirstName>Robin1</ChildFirstName>
                  <ChildLastName>Jackman1</ChildLastName>
                  <Address>
                    <Street1>Street1</Street1>
                    <State>State</State>
                    <Country>Country</Country>
                  </Address>
                </ParticipentNames>
              </Cricket>
            </Sports>
          </Activities>
        </Child>
        <Child>
          <ChildFirstName>Robin2</ChildFirstName>
          <ChildLastName>Jackman2</ChildLastName>
          <Address>
            <Street1>Street1</Street1>
            <State>State</State>
            <Country>Country</Country>
          </Address>
          <Activities>
            <Sports>
              <Cricket>
                <ParticipentNames>
                  <ChildFirstName>Robin2</ChildFirstName>
                  <ChildLastName>Jackman2</ChildLastName>
                  <Address>
                    <Street1>Street1</Street1>
                    <State>State</State>
                    <Country>Country</Country>
                  </Address>
                </ParticipentNames>
              </Cricket>
            </Sports>
          </Activities>
        </Child>
        <Child>
          <ChildFirstName>Robin3</ChildFirstName>
          <ChildLastName>Jackman3</ChildLastName>
          <Address>
            <Street1>Street1</Street1>
            <State>State</State>
            <Country>Country</Country>
          </Address>
          <Activities>
            <Sports>
              <Cricket>
                <ParticipentNames>
                  <ChildFirstName>Robin3</ChildFirstName>
                  <ChildLastName>Jackman3</ChildLastName>
                  <Address>
                    <Street1>Street1</Street1>
                    <State>State</State>
                    <Country>Country</Country>
                  </Address>
                </ParticipentNames>
              </Cricket>
            </Sports>
          </Activities>
        </Child>
      </Childrens>
    </Family1>
  </Root1>

I want the solution in XSLT 1.0.

I want to copy these nodes to child nodes

                  <ChildFirstName>Robin3</ChildFirstName>
                  <ChildLastName>Jackman3</ChildLastName>
                  <Address>
                    <Street1>Street1</Street1>
                    <State>State</State>
                    <Country>Country</Country>
                  </Address>

Thanks.

1

1 Answers

2
votes
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="ParticipentNames">
  <xsl:copy>
    <xsl:apply-templates select="ancestor::Child/ChildFirstName | ancestor::Child/ChildLastName | ancestor::Child/Address"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

should suffice (might want to add <xsl:output indent="yes"/> and <xsl:strip-space elements="*"/> for better formatting).