XSL与每个子节点具有相同的父元素

时间:2013-04-27 12:39:18

标签: xslt

请使用xslt

帮助将以下xml转换为所需格式

每个元素,如时钟输入,时钟输出,中断开始,中断结束需要将TASDATA元素作为单独的父级

实际XML:

<?xml version="1.0" encoding="UTF-8"?>
<tXML>
  <Header>
    <Source>TASSRC</Source>
    <Action_Type>Create</Action_Type>
    <Batch_ID>TASBTID</Batch_ID>
    <Reference_ID>TASRFID</Reference_ID>
    <User_ID>TASUSR</User_ID>
    <Password>TASPWD</Password>
    <Message_Type>TAS</Message_Type>
    <Company_ID>810</Company_ID>
    <Purpose_Code>TAS</Purpose_Code>
    <Msg_Locale>en.US</Msg_Locale>
    <Version>1.0</Version>
    <Sequence_Number>TAS&amp;</Sequence_Number>
  </Header>
  <Message>
    <TimeAndAttendance>
      <TASData>
        <ClockIn>
          <TranNumber>000000001</TranNumber>
          <Warehouse>810</Warehouse>
          <EmployeeUserId>173633</EmployeeUserId>
          <EmpClockIn>2012-08-06T05:04:00</EmpClockIn>
        </ClockIn>
        <BreakStart>
          <TranNumber>000000002</TranNumber>
          <Warehouse>810</Warehouse>
          <EmployeeUserId>173633</EmployeeUserId>
          <BreakStartTime>2012-08-06T11:05:00</BreakStartTime>
          <Activity>UNPAIDBRK</Activity>
        </BreakStart>
        <BreakEnd>
          <TranNumber>000000003</TranNumber>
          <Warehouse>810</Warehouse>
          <EmployeeUserId>173633</EmployeeUserId>
          <BreakStartTime>2012-08-06T11:05:00</BreakStartTime>
          <BreakEndTime>2012-08-06T11:31:00</BreakEndTime>
          <Activity>UNPAIDBRK</Activity>
        </BreakEnd>
        <ClockOut>
          <TranNumber>000000004</TranNumber>
          <Warehouse>810</Warehouse>
          <EmployeeUserId>173633</EmployeeUserId>
          <EmpClockOut>2012-08-06T15:29:00</EmpClockOut>
        </ClockOut>
      </TASData>
    </TimeAndAttendance>
  </Message>
</tXML>

必需的XML:

<?xml version="1.0" encoding="UTF-8"?>
<tXML>
  <Header>
    <Source>TASSRC</Source>
    <Action_Type>Create</Action_Type>
    <Batch_ID>TASBTID</Batch_ID>
    <Reference_ID>TASRFID</Reference_ID>
    <User_ID>TASUSR</User_ID>
    <Password>TASPWD</Password>
    <Message_Type>TAS</Message_Type>
    <Company_ID>810</Company_ID>
    <Purpose_Code>TAS</Purpose_Code>
    <Msg_Locale>en.US</Msg_Locale>
    <Version>1.0</Version>
    <Sequence_Number>TAS&amp;</Sequence_Number>
  </Header>
  <Message>
    <TimeAndAttendance>
      <TASData>
        <ClockIn>
          <TranNumber>000000001</TranNumber>
          <Warehouse>810</Warehouse>
          <EmployeeUserId>173633</EmployeeUserId>
          <EmpClockIn>2012-08-06T05:04:00</EmpClockIn>
        </ClockIn>
      </TASData>
      <TASData>
        <BreakStart>
          <TranNumber>000000002</TranNumber>
          <Warehouse>810</Warehouse>
          <EmployeeUserId>173633</EmployeeUserId>
          <BreakStartTime>2012-08-06T11:05:00</BreakStartTime>
          <Activity>UNPAIDBRK</Activity>
        </BreakStart>
      </TASData>
      <TASData>
        <BreakEnd>
          <TranNumber>000000003</TranNumber>
          <Warehouse>810</Warehouse>
          <EmployeeUserId>173633</EmployeeUserId>
          <BreakStartTime>2012-08-06T11:05:00</BreakStartTime>
          <BreakEndTime>2012-08-06T11:31:00</BreakEndTime>
          <Activity>UNPAIDBRK</Activity>
        </BreakEnd>
      </TASData>
      <TASData>
        <ClockOut>
          <TranNumber>000000004</TranNumber>
          <Warehouse>810</Warehouse>
          <EmployeeUserId>173633</EmployeeUserId>
          <EmpClockOut>2012-08-06T15:29:00</EmpClockOut>
        </ClockOut>
      </TASData>
    </TimeAndAttendance>
  </Message>
</tXML>

1 个答案:

答案 0 :(得分:1)

这应该可以做到 - 它拦截现有的TimeAndAttendance/TASData节点并将每个子节点包装在一个新的TASData元素中:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>

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

    <xsl:template match="TimeAndAttendance/TASData">
        <xsl:apply-templates select="*" mode="AddTASDataParent"/>
    </xsl:template>

    <xsl:template match="*" mode="AddTASDataParent">
        <TASData>
            <xsl:copy-of select="."/>
        </TASData>
    </xsl:template>

</xsl:stylesheet>