user3135584
user3135584

Reputation: 1

Copy a node from parent to child

I would like to copy a parent node into the child but I'm unsure on how to proceed.

I apologize in advanced for the weird looking source file, I'm new to this forum and have no clue on how to properly paste an XML file.

My XML source file is like this:

<?xml version="1.0"?>
<IncidentLogUpload>

<Header>

<BatchID>2013</BatchID>
<SystemID>2013</SystemID>
<DateTime>12/20/2013 3:37 PM</DateTime>

</Header>

<Item>

<IncidentLogs>

<IncidentLog>

<IncidentSource>Source</IncidentSource>
<Property>Property</Property>
<Location>B1</Location>
<SubLocation/>
<DailyLogID>IN2013</DailyLogID>
<IncidentID>IN2013</IncidentID>
<Reference/>
<DateTimeOccured>12/19/2013 8:17 PM</DateTimeOccured>
<IncidentType>Surveillance</IncidentType>
<Specific>Observation</Specific>
<Category>POI</Category>
<IncidentDetails>0400</IncidentDetails>
<RelatedIncidentNo/>
<DateTimeReported>12/19/2013 8:17 PM</DateTimeReported>
<ParticipantSubjectProfiles>

<ParticipantSubjectProfile>

<FirstName>James</FirstName>
<MiddleName></MiddleName>
<LastName>Henderson</LastName>
<ParticipantType>Subject</ParticipantType>
<MembershipNumber></MembershipNumber>
<DriversLicense></DriversLicense>
<PassportNumber></PassportNumber>
<IncidentID>IN2013</IncidentID>

</ParticipantSubjectProfile>

</ParticipantSubjectProfiles>
<ParticipantPersonnelProfiles>

<ParticipantPersonnelProfile>

<BusinessUnit>Games</BusinessUnit>
<FirstName>Edison</FirstName>
<MiddleName>John</MiddleName>
<LastName>Costabile</LastName>
<CSELNumber/>
<StaffID>000408</StaffID>
<DriversLicense/>
<AffBUKey>GamesIN2013</AffBUKey>
<ParticipantType>Personnel</ParticipantType>

</ParticipantPersonnelProfile>

</ParticipantPersonnelProfiles>

</IncidentLog>

</IncidentLogs>

</Item>

<Footer>

<NumberOfRecords>5</NumberOfRecords>

</Footer>

</IncidentLogUpload>

I would like to copy the <Property> node to both <ParticipantSubjectProfile> and <ParticipantPersonnelProfile>. The end result should be like this:

<ParticipantSubjectProfiles>

<ParticipantSubjectProfile>

<FirstName>James</FirstName>
<MiddleName></MiddleName>
<LastName>Henderson</LastName>
<ParticipantType>Subject</ParticipantType>
<MembershipNumber></MembershipNumber>
<DriversLicense></DriversLicense>
<PassportNumber></PassportNumber>
<IncidentID>IN2013</IncidentID>
<Property>Property</Property>

</ParticipantSubjectProfile>

</ParticipantSubjectProfiles>
<ParticipantPersonnelProfiles>

<ParticipantPersonnelProfile>

<BusinessUnit>Games</BusinessUnit>
<FirstName>Edison</FirstName>
<MiddleName>John</MiddleName>
<LastName>Costabile</LastName>
<CSELNumber/>
<StaffID>000408</StaffID>
<DriversLicense/>
<AffBUKey>GamesIN2013</AffBUKey>
<ParticipantType>Personnel</ParticipantType>
<Property>Property</Property>

</ParticipantPersonnelProfile>

</ParticipantPersonnelProfiles>

Please help! Thank you!

Upvotes: 0

Views: 49

Answers (2)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

Try this template out:

<?xml version="1.0" encoding="UTF-8"?>
<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="/">
    <ParticipantSubjectProfiles>
        <xsl:for-each select="//ParticipantSubjectProfiles/ParticipantSubjectProfile">
            <xsl:copy>
                <xsl:apply-templates/>
                <xsl:apply-templates select="../preceding-sibling::Property"></xsl:apply-templates>
            </xsl:copy>
        </xsl:for-each>
    </ParticipantSubjectProfiles>
    <ParticipantPersonnelProfiles>
        <xsl:for-each select="//ParticipantPersonnelProfiles/ParticipantPersonnelProfile">
            <xsl:copy>
                <xsl:apply-templates/>
                <xsl:apply-templates select="../preceding-sibling::Property"></xsl:apply-templates>
            </xsl:copy>

        </xsl:for-each>
    </ParticipantPersonnelProfiles>


</xsl:template>

</xsl:stylesheet>

Upvotes: 0

Siva Tumma
Siva Tumma

Reputation: 1701

Edited

<xsl:template match="/">
<ParticipantSubjectProfiles>
        <xsl:for-each select="IncidentLogUpload/Item/IncidentLogs/IncidentLog/ParticipantSubjectProfiles">

    <ParticipantSubjectProfile>  
        <FirstName>James</FirstName>
        <MiddleName></MiddleName>
        <LastName>Henderson</LastName>
        <ParticipantType>Subject</ParticipantType>
        <MembershipNumber></MembershipNumber>
        <DriversLicense></DriversLicense>
        <PassportNumber></PassportNumber>
        <IncidentID>IN2013</IncidentID>
        <Property> <xsl:value-of select="/IncidentLogUpload/Item/IncidentLogs/IncidentLog/Property"/></Property>
    </ParticipantSubjectProfile>

    </xsl:for-each>
</ParticipantSubjectProfiles>

</xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions