我是xml和xsl的新手,所以我会尝试解释我的问题。 我有这种类型的xml,它是以属性为中心的,并且不会导入到Access中。如何将属性数据转换为元素数据? 这是xml的一部分:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalogue version="3.0">
<App action="A" id="1">
<BaseVehicle id="1"/>
<Note>Use Separate Enclosed Connector</Note>
<Note>Slightly longer than original</Note>
<Qty>1</Qty>
<PartType id="8852"/>
<MfrLabel>CleanWipe</MfrLabel>
<Position id="104"/>
<Part>18CW</Part>
</App>
<App action="A" id="2">
<BaseVehicle id="1"/>
<BodyType id="6"/>
<Note>Use Separate Enclosed Connector</Note>
<Qty>1</Qty>
<PartType id="8852"/>
<MfrLabel>SuperWipe</MfrLabel>
<Position id="30"/>
<Part>22SW</Part>
</App>
</Catalogue>
这是我用于将xml导入Access的xsl (我真的不明白它是如何工作的,我在堆栈流程中找到了它):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
问题是我丢失了App ID。有没有办法在Access中导入此数据并在列中包含App ID? 我也有问题&#34;注意&#34;行,因为每个&#34; App&#34;有多个Note行,我无法导入所有Note行。您是否有建议如何导入此xml而不丢失任何数据? 谢谢大家!!!
编辑: 以下是我在Access中导入后的预期列表:
ID BaseVehicle Note数量PartType MfrLabel位置零件BodyType
1 1略长于原始1 8852 CleanWipe 104 18CW
1 1使用单独的封闭式连接器1 8852 CleanWipe 104 18CW
2 1使用单独的封闭式连接器1 8852 SuperWipe 30 22SW 6
或者像这样:
ID BaseVehicle Note数量PartType MfrLabel位置零件BodyType
1 1略长.. +使用单独的E .. 1 8852 CleanWipe 104 18CW
2 1使用单独的封闭式连接器1 8852 SuperWipe 30 22SW 6
问题是我无法导入列ID并在单独的行中组合注释或导入非首先注释...
答案 0 :(得分:1)
我找到了一个问题的答案。这是我正在寻找的XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Catalogue/App/@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="Catalogue/App/Note[1]">
<Note1><xsl:apply-templates select="@*|node()"/></Note1>
</xsl:template>
<xsl:template match="Catalogue/App/Note[2]">
<Note2><xsl:apply-templates select="@*|node()"/></Note2>
</xsl:template>
<xsl:template match="Catalogue/App/Note[3]">
<Note3><xsl:apply-templates select="@*|node()"/></Note3>
</xsl:template>
<xsl:template match="Catalogue/App/Note[4]">
<Note4><xsl:apply-templates select="@*|node()"/></Note4>
</xsl:template>
<xsl:template match="Catalogue/App/Note[5]">
<Note5><xsl:apply-templates select="@*|node()"/></Note5>
</xsl:template>
<xsl:template match="Catalogue/App/Note[6]">
<Note6><xsl:apply-templates select="@*|node()"/></Note6>
</xsl:template>
<xsl:template match="Catalogue/App/Note[7]">
<Note7><xsl:apply-templates select="@*|node()"/></Note7>
</xsl:template>
<xsl:template match="Catalogue/App/Note[8]">
<Note8><xsl:apply-templates select="@*|node()"/></Note8>
</xsl:template>
<xsl:template match="Catalogue/App/Note[9]">
<Note9><xsl:apply-templates select="@*|node()"/></Note9>
</xsl:template>
<xsl:template match="Catalogue/App/Note[10]">
<Note10><xsl:apply-templates select="@*|node()"/></Note10>
</xsl:template>
<xsl:template match="Catalogue/App/Note[11]">
<Note11><xsl:apply-templates select="@*|node()"/></Note11>
</xsl:template>
<xsl:template match="Catalogue/App/Note[12]">
<Note12><xsl:apply-templates select="@*|node()"/></Note12>
</xsl:template>
<xsl:template match="Catalogue/App/Note[13]">
<Note13><xsl:apply-templates select="@*|node()"/></Note13>
</xsl:template>
<xsl:template match="Catalogue/App/Note[14]">
<Note14><xsl:apply-templates select="@*|node()"/></Note14>
</xsl:template>
<xsl:template match="Catalogue/App/Note[15]">
<Note15><xsl:apply-templates select="@*|node()"/></Note15>
</xsl:template>
</xsl:stylesheet>
这样所有属性都被转换为元素和所有&#34;注意&#34;重复的元素将根据它们在&#34; App&#34;中的位置重命名。最多15次。但是因为我知道不会超过10次,所以这样可以正常工作。 15只是出于预防。 以下是转型后的看法:
<?xml version="1.0" encoding="UTF-8"?>
<Catalogue>
3.0
<App>
<action>A</action>
<id>1</id>
<BaseVehicle>1</BaseVehicle>
<Note1>Use Separate Enclosed Connector</Note1>
<Note2>Slightly longer than original</Note2>
<Qty>1</Qty>
<PartType>8852</PartType>
<MfrLabel>CleanWipe</MfrLabel>
<Position>104</Position>
<Part>18CW</Part>
</App>
<App>
<action>A</action>
<id>2</id>
<BaseVehicle>1</BaseVehicle>
<BodyType>6</BodyType>
<Note1>Use Separate Enclosed Connector</Note1>
<Qty>1</Qty>
<PartType>8852</PartType>
<MfrLabel>SuperWipe</MfrLabel>
<Position>30</Position>
<Part>22SW</Part>
</App>
</Catalogue>