如何将具有父/子关系的XML导入Access?

时间:2017-01-31 21:32:37

标签: xml ms-access xslt import

我正在尝试将XML文件导入Access,但它会创建9个不相关的表。也就是说,子记录被导入到子表中,但是无法知道哪个子记录属于哪个父记录。

如何导入数据以维护父节点和子节点(记录)之间的关系?

以下是XML数据的示例:

<?xml version="1.0" encoding="UTF-8"?>
<iavmNotice xmlns="http://stuff.com" noticeId="138643">
   <xmlUrl>https://fakexmlurl.xml</xmlUrl>
   <htmlUrl>https://fakehtmlurl.htm</htmlUrl>
   <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
   <title>Cisco Vulnerability</title>
   <type>B</type>
   <state>FINAL</state>
   <lastSaved>2012-08-24T10:34:13</lastSaved>
   <precoordDueDate>2012-08-23T11:00:00</precoordDueDate>
   <releaseDate>2012-08-23</releaseDate>
   <acknowledgeDate>2012-08-28</acknowledgeDate>
   <knownExploits>true</knownExploits>
   <knownDodIncidents>false</knownDodIncidents>
   <executiveSummary>Cisco incidents.</executiveSummary>
   <techOverview>
      <entry>
         <title>2012-2490</title>
         <description>Cisco ID 71.</description>
      </entry>
      <entry>
         <title>2012-2525</title>
         <description>Cisco ID 69.</description>
      </entry>
   </techOverview>
   <fixAction>Apply appropriate vendor update</fixAction>
   <note>&lt;b&gt;Upgrade later&lt;/b&gt;&lt;br&gt;&lt;br&gt;</note>
   <tempMitStrat>
      <header>&lt;b&gt;Cisco &lt;/b&gt;</header>
      <body>&lt;br&gt;None</body>
   </tempMitStrat>
   <vulnAppsSysAndCntrmsrs>Cisco IP Communicator 8.6&lt;br&gt;&lt;br&gt;</vulnAppsSysAndCntrmsrs>
   <references>
   <reference>
      <title>Release Notes for Cisco IP Communicator Release 8.6</title>
      <url>http://www.fakeurl.com/html</url>
   </reference>
   </references>
   <deepSightBids>
      <bid>54850</bid>
   </deepSightBids>
   <revisions>
      <revision>
         <majorNum>0</majorNum>
         <minorNum>0</minorNum>
         <type>MAJOR</type>
         <date>2012-08-23</date>
         <details>Initial Release</details>
      </revision>
      <revision>
         <majorNum>0</majorNum>
         <minorNum>1</minorNum>
         <type>MINOR</type>
         <date>2012-08-24</date>
         <details>Updated Retina Audit</details>
      </revision>
   </revisions>
   <patches>
      <patch>
         <type>V</type>
         <title>Cisco</title>
         <url>http://www.fake.com</url>
      </patch>
   </patches>
   <scanners>
      <retina>Audit 2548</retina>
   </scanners>
   <vms>
      <stigFindingSeverity>1</stigFindingSeverity>
      <affectedEnvs>B</affectedEnvs>
      <posture>1029</posture>
      <posture>1712</posture>
      <posture>59</posture>
   </vms>
</iavmNotice>

我很难理解如何将其转换为xslt文件

1 个答案:

答案 0 :(得分:4)

您的主要问题是未声明的命名空间前缀xmlns="http://stuff.com"。在XSLT中,您可以使用xmlns:stuff="http://stuff.com"为下面的前缀指定前缀,然后在每个子元素前面加上它以访问其内容。

您的下一个问题是检索共享的id,以便所有表都可以关联。考虑首先运行Identity Transform按原样复制文档,然后在选择节点中运行ancestor::*以检索根目录下的iavmNoticeNumber,并将其与原始内容一起复制到节点中。< / p>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:stuff="http://stuff.com"  exclude-result-prefixes="stuff">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

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

  <xsl:template match="stuff:entry|stuff:tempMitStrat|stuff:reference|stuff:deepSightBids|
                       stuff:revision|stuff:patch|stuff:scanners">
    <xsl:copy>      
      <xsl:copy-of select="ancestor::stuff:iavmNotice/stuff:iavmNoticeNumber"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="stuff:vms">
    <xsl:copy>
      <xsl:copy-of select="ancestor::stuff:iavmNotice/stuff:iavmNoticeNumber"/>
      <xsl:copy-of select="stuff:stigFindingSeverity|stuff:affectedEnvs"/>
      <xsl:element name="posture" namespace="http://stuff.com">
        <xsl:for-each select="stuff:posture">
          <xsl:value-of select="."/>
          <xsl:if test="position()!=last()">
            <xsl:text>,</xsl:text>
          </xsl:if>
        </xsl:for-each>
      </xsl:element>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

<强>输出

<?xml version="1.0" encoding="utf-8"?>
<iavmNotice noticeId="138643" xmlns="http://stuff.com">
  <xmlUrl>https://fakexmlurl.xml</xmlUrl>
  <htmlUrl>https://fakehtmlurl.htm</htmlUrl>
  <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
  <title>Cisco Vulnerability</title>
  <type>B</type>
  <state>FINAL</state>
  <lastSaved>2012-08-24T10:34:13</lastSaved>
  <precoordDueDate>2012-08-23T11:00:00</precoordDueDate>
  <releaseDate>2012-08-23</releaseDate>
  <acknowledgeDate>2012-08-28</acknowledgeDate>
  <knownExploits>true</knownExploits>
  <knownDodIncidents>false</knownDodIncidents>
  <executiveSummary>Cisco incidents.</executiveSummary>
  <techOverview>
    <entry>
      <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
      <title>2012-2490</title>
      <description>Cisco ID 71.</description>
    </entry>
  </techOverview>
  <fixAction>Apply appropriate vendor update</fixAction>
  <note>&lt;b&gt;Upgrade later&lt;/b&gt;&lt;br&gt;&lt;br&gt;</note>
  <tempMitStrat>
    <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
    <header>&lt;b&gt;Cisco &lt;/b&gt;</header>
    <body>&lt;br&gt;None</body>
  </tempMitStrat>
  <vulnAppsSysAndCntrmsrs>Cisco IP Communicator 8.6&lt;br&gt;&lt;br&gt;</vulnAppsSysAndCntrmsrs>
  <references>
    <reference>
      <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
      <title>Release Notes for Cisco IP Communicator Release 8.6</title>
      <url>http://www.fakeurl.com/html</url>
    </reference>
  </references>
  <deepSightBids>
    <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
    <bid>54850</bid>
  </deepSightBids>
  <revisions>
    <revision>
      <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
      <majorNum>0</majorNum>
      <minorNum>0</minorNum>
      <type>MAJOR</type>
      <date>2012-08-23</date>
      <details>Initial Release</details>
    </revision>
    <revision>
      <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
      <majorNum>0</majorNum>
      <minorNum>1</minorNum>
      <type>MINOR</type>
      <date>2012-08-24</date>
      <details>Updated Retina Audit</details>
    </revision>
  </revisions>
  <patches>
    <patch>
      <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
      <type>V</type>
      <title>Cisco</title>
      <url>http://www.fake.com</url>
    </patch>
  </patches>
  <scanners>
    <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
    <retina>Audit 2548</retina>
  </scanners>
  <vms>
    <iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
    <stigFindingSeverity>1</stigFindingSeverity>
    <affectedEnvs>B</affectedEnvs>
    <posture>1029</posture>
    <posture>1712</posture>
    <posture>59</posture>
  </vms>
</iavmNotice>