1

请帮助使用 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>
4

1 回答 1

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>
于 2013-04-27T12:54:58.643 回答