![](/img/trans.png)
[英]How to convert Excel to XML with nested elements using XSLT 1.0?
[英]Excel to XML nested elements
I am trying to use Excel to generate an XML, Overview of XML in Excel & Export XML data
我的 XML 看起来像这样,更多的块和元素,这只是一个例子,:
<Block>
<Element1>XXX</Element1>
<Element2>YYY</Element2>
<Element3>ZZZ</Element3>
<Nested_Elements>
<Nested_Element>AAA</Nested_Element>
<Nested_Element>BBB</Nested_Element>
<Nested_Element>CCC</Nested_Element>
</Nested_Elements>
</Block>
in Excel I can map element1,element2 and element3 add diffrent values and export to XML,all works in a nice easy way,which is exactly why I used excel instead of writing a python or c# code,the problem is with Nested_Element1, Nested_Element2,嵌套元素 3。
经过大量搜索后,我认为没有办法直接使用 Excel 进行嵌套元素,最好的中间工具是什么,我对 Excel 的了解不是很好,Z6E3EC7E6A9F69307B48380 是唯一的方法吗?
如果我使用VBA,我将不得不创建一个代码来编写整个Z3501BB093D363810B671059B671059B9CFED3CFED3F8Z文件,因此无法使用ZC1D81F5835835844B4B4B4B4B44EEFTRINTION?
Ok,it truned out very easy but I will post an answer here in case someone is still searching for this, 1- Map flat elements between excel cells with XML Map source. 2- VBA 代码导出 XML:
'Export XMLMap from worksheet,this will export the flat Mapped elements
Set Map = ActiveWorkbook.XmlMaps(1)
Map.Export Url:=XmlFile, Overwrite:=True
're-Load XML as DOM to process it
Set XDoc = New MSXML2.DOMDocument
XDoc.async = False: XDoc.validateOnParse = False
XDoc.Load XmlFile
'Update XML with nested elements
CellText = sheet_Board.Cells(22, "C").Value 'cell having nested elements values separated with "," ===> AAA,BBB,CCC
Set Node = XDoc.SelectSingleNode("//Block")
Set nodChild = XDoc.createElement("Nested_Elements")
Result = Split(CellText, ",")
For i = LBound(Result()) To UBound(Result())
Set nodGrandChild = XDoc.createElement("Nested_Element")
nodGrandChild.Text = Result(i)
nodChild.appendChild nodGrandChild
Next i
Node.appendChild nodChild
这将创建一个 XML ,其中添加的嵌套节点没有缩进:
<Block>
<Element1>XXX</Element1>
<Element2>YYY</Element2>
<Element3>ZZZ</Element3>
<Nested_Elements><Nested_Element>AAA</Nested_Element><Nested_Element>BBB</Nested_Element><Nested_Element>CCC</Nested_Element></Nested_Elements></Block>
要修复缩进,请添加这段代码(来自 stackoverflow https://stackoverflow.com/a/37634549/14360302 )
Set xslDoc = New MSXML2.DOMDocument
xslDoc.LoadXML "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" _
& "<xsl:stylesheet version=" & Chr(34) & "1.0" & Chr(34) _
& " xmlns:xsl=" & Chr(34) & "http://www.w3.org/1999/XSL/Transform" & Chr(34) & ">" _
& " <xsl:strip-space elements=" & Chr(34) & "*" & Chr(34) & " />" _
& " <xsl:output method=" & Chr(34) & "xml" & Chr(34) & " indent=" & Chr(34) & "yes" & Chr(34) & "" _
& " encoding=" & Chr(34) & "UTF-8" & Chr(34) & "/>" _
& " <xsl:template match=" & Chr(34) & "node() | @*" & Chr(34) & ">" _
& " <xsl:copy>" _
& " <xsl:apply-templates select=" & Chr(34) & "node() | @*" & Chr(34) & " />" _
& " </xsl:copy>" _
& " </xsl:template>" _
& "</xsl:stylesheet>"
xslDoc.async = False
Set XmlNewDoc = New MSXML2.DOMDocument
XDoc.transformNodeToObject xslDoc, XmlNewDoc 'Line to fix indention
XmlNewDoc.Save XmlFile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.