【发布时间】:2017-03-10 22:34:12
【问题描述】:
首先,我对 Access 很熟悉,并且了解了 XML 的一般概念,但我是 XSLT 的新手,所以如果我的解释/术语表达不佳,请原谅我的帖子。
我有一个 XML 源文件,我必须将其导入 Access 数据库并链接所有 grandparent/parent/child/child/... 关系以用于报告目的。我在这里阅读了类似的帖子(非常感谢之前发布的所有努力)并拼凑了一个 XSLT 文件以在所有创建的表之间创建链接(学校链接到学生 使用 [unique] SchoolNumber 字段,Student 使用 [unique] SID 字段链接它的各个孩子。
但是,我不知道如何让名称节点数据在各自的表中正确对齐。在 XML 文件中,Name 属性同时用于 Student 节点和 Guardian 节点,因此它会创建一个 Name 表并将 Student 和 Guardian Name 字段放在一个 Name 表中。 Name 表应仅包含 Student 名(First、Middle、Last),而 Guardian 表应包含 监护人姓名(First、Last)以及其他Guardian 字段(Relationship、Phone、[PhoneType)。
此外,在 XML 中,Guardian Phone 属性包含一个嵌套元素 type="HOME",它应该包含在生成的 Guardian 表中。源 XML(我无法控制)如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:SchoolUpload xmlns:ns1="http://ontario.ca">
<ns1:School>
<ns1:SchoolNumber>123456789</ns1:SchoolNumber>
<ns1:Students>
<ns1:Student>
<ns1:SID>10101010</ns1:SID>
<ns1:Name>
<ns1:First>Student1</ns1:First>
<ns1:Middle>Middle</ns1:Middle>
<ns1:Last>Surname</ns1:Last>
</ns1:Name>
<ns1:AliasName>
<ns1:First>Red</ns1:First>
<ns1:Last>Blue</ns1:Last>
</ns1:AliasName/>
<ns1:Gender>M</ns1:Gender>
<ns1:BirthDate>1991-10-29</ns1:BirthDate>
<ns1:Language>en</ns1:Language>
<ns1:Guardian>
<ns1:Name>
<ns1:First>Primary</ns1:First>
<ns1:Last>Guardian</ns1:Last>
</ns1:Name>
<ns1:Relationship>MOTHER</ns1:Relationship>
<ns1:Phone type="HOME">111-111-1111</ns1:Phone>
</ns1:Guardian>
<ns1:Guardian>
<ns1:Name>
<ns1:First>Secondary</ns1:First>
<ns1:Last>Guardian</ns1:Last>
</ns1:Name>
<ns1:Relationship>FATHER</ns1:Relationship>
<ns1:Phone type="HOME">222-222-2222</ns1:Phone>
</ns1:Guardian>
<ns1:Address>
<ns1:StreetAddress>15 Main Street</ns1:StreetAddress>
<ns1:City>Guelph</ns1:City>
<ns1:Province>ON</ns1:Province>
<ns1:PostalCode>N5N5N5</ns1:PostalCode>
</ns1:Address>
<ns1:Phone type="HOME">333-333-3333</ns1:Phone>
</ns1:Student>
</ns1:Students>
</ns1:School>
</ns1:SchoolUpload>
我拼凑的XSLT文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:ns1="http://ontario.ca" exclude-result-prefixes="ns1">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns1:Student">
<xsl:copy>
<xsl:copy-of select="ancestor::ns1:School/ns1:SchoolNumber"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns1:Student/ns1:Name">
<xsl:copy>
<xsl:copy-of select="ancestor::ns1:Student/ns1:SID"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns1:AliasName|ns1:Guardian|ns1:Address">
<xsl:copy>
<xsl:copy-of select="ancestor::ns1:Student/ns1:SID"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
使用 XSLT 文件将 XML 导入 Access 会导致 Access 创建 6 个表(School、Student、Name、Guardian、Address、AliasName),所有这些表都可以链接。但是,我不知道如何达到我想要达到的最终结果。在此之前,我非常感谢您提供的任何反馈。
【问题讨论】: