简体   繁体   中英

Copy part of parent nodes to child node XSLT

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.

<?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="/OrderItem/ProductionInformation/GSItem">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:for-each select="/OrderItem">
        <xsl:apply-templates select="@*|node()[name()!='ProductionInformation']"/>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM