将节点从父节点复制到子节点

时间:2013-12-26 03:38:33

标签: xslt-1.0

我想将父节点复制到子节点,但我不确定如何继续。

我为这个奇怪的源文件道歉,我是这个论坛的新手,并且不知道如何正确粘贴XML文件。

我的XML源文件是这样的:

  

<?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>

我想将<Property>节点复制到<ParticipantSubjectProfile><ParticipantPersonnelProfile>。最终结果应该是这样的:

  

<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>

请帮忙!谢谢!

2 个答案:

答案 0 :(得分:1)

<强>被修改          

<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>

答案 1 :(得分:0)

尝试使用此模板:

<?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>