XSLT to change XML in MS ACCESS import

微笑、不失礼 提交于 2019-12-02 06:30:20

You mostly need the identity transform, which simply copies the input XML to the output document. Apart from that, you can catch the relevant child nodes, <Training/> and <Driver/>, and attach the needed <Fetchdate/> to them.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Training|Driver">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:copy-of select="ancestor::F1Project/Fetchdate"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

There are two templates: one that matches every node, and one that matches the nodes that need the <Fetchdate/> added. They both copy the existing nodes to the output document, but in the second one, it copies:

  1. any attributes of the node
  2. the <Fetchdate/>, which it finds by visiting its <F1Project/> ancestor and finding its date
  3. the children nodes

That stylesheet produces this output when run on your example data:

<F1Project>
  <File>piloti.php</File>
  <Fetchdate>2014-05-23 11:37:41</Fetchdate>
  <IdTeam>614</IdTeam>
  <Training>
    <Fetchdate>2014-05-23 11:37:41</Fetchdate>
    <TrainingFirstSkill>accelerazione</TrainingFirstSkill>
    <TrainingSecondSkill>tecnica</TrainingSecondSkill>
  </Training>
  <TalentScout>
    <TalentScoutLevel>16</TalentScoutLevel>
    <TalentScoutFunding>25000</TalentScoutFunding>
  </TalentScout>
  <Drivers>
    <Driver Index="1">
      <Fetchdate>2014-05-23 11:37:41</Fetchdate>
      <DriverId>357352</DriverId>
      <DriverName>Doukas</DriverName>
      <DriverSurname>Nastos</DriverSurname>
      <DriverDPI>55344</DriverDPI>
    </Driver>
    <Driver Index="2">
      <Fetchdate>2014-05-23 11:37:41</Fetchdate>
      <DriverId>539134</DriverId>
      <DriverName>Jurica</DriverName>
      <DriverSurname>Andonovic</DriverSurname>
      <DriverDPI>1406</DriverDPI>
    </Driver>
    <Driver Index="3">
      <Fetchdate>2014-05-23 11:37:41</Fetchdate>
      <DriverId>473147</DriverId>
      <DriverName>Tommaso</DriverName>
      <DriverSurname>Galea</DriverSurname>
      <DriverDPI>5553</DriverDPI>
    </Driver>
  </Drivers>
</F1Project>

This is a simple transformation. If you have any questions, please ask.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!