![](/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.