有没有办法让Excel在根元素中保留XML属性?

dev*_*xer 7 xml excel xsd

我一直在尝试使用MS Excel 2007来编辑存储在XML文件中的表格数据.它可以很好地导入甚至根据模式验证XML数据(xsd文件),但是当我导出时,它会从根元素中删除xmlns,xlmns:xsi和xsi:schemaLocation属性.它还将默认命名空间更改为显式命名空间.

这是一个前/后比较:

之前(导入Excel 之前的XML文件)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<database
  xmlns="experimentManager"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="experimentManager Database.xsd">
  <conditionTokens>
    ...
  </conditionTokens>
  <participants>
    ...
  </participants>
</database>
Run Code Online (Sandbox Code Playgroud)

之后(从Excel导出的XML文件)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:database xmlns:ns1="experimentManager">
    <ns1:conditionTokens>
        ...
    </ns1:conditionTokens>
    <ns1:participants>
        ...
    </ns1:participants>
</ns1:database>
Run Code Online (Sandbox Code Playgroud)

有没有办法阻止Excel剥离这些属性并搞乱命名空间?我已阅读有关XML映射和导入/导出的MS帮助,但GUI中似乎没有任何设置可用于我想要做的事情.如果我需要编写自定义宏,那是可能的,但如果有更好/更简单的方法,我宁愿不这样做.

第二个问题:是否有更好的工具允许使用类似Excel的UI轻松编辑XML文件的某些部分?

dev*_*xer 4

好吧,我硬着头皮写了一个很好的 VBA 宏。我想我会与大家分享,以防其他人遇到同样的问题。

该宏基本上调用 Excel 的内置 XML Export() 方法,然后对结果文件执行一系列文本替换。文本替换完全取决于您。只需将它们放在工作表中,如下面链接中的工作表...

如何设置“替换规则”的示例: 点我查看屏幕截图

在此示例中,我将制表符替换为空格-空格,将“:ns1”替换为空白,将“ns1:”替换为空白,并将精简的根元素替换为原始根元素。

您可以按照自己喜欢的方式格式化替换规则,只要遵循以下说明即可:

  1. 选择所有“查找内容”单元格并将其命名为*“FindWhat”(不要在选择中包含标题行;空白将被忽略)。
  2. 选择所有“替换为”单元格,并将其命名为*“ReplaceWith”(“查找内容”和“替换为”单元格之间应该存在一对一的映射;使用空格删除不需要的文本)。
  3. 在工作簿中的某处输入 XML 映射的名称,并将该单元格命名为“XmlMap”。
  4. 运行宏。(系统会要求您指定要导出到的文件。)

*如果您不熟悉 Excel 2007 中的命名范围,请单击“公式”选项卡并选择“名称管理器”。

好吧,我不会再让你悬念了(笑)...这是宏的代码。只需将其放置在 VBA 编辑器的模块中即可。我对这个免费代码不提供任何保证(如果您没有正确命名范围,您可能很容易破坏它),但我尝试过的几个示例对我有用。

Option Explicit

Sub ExportXml()
    Dim exportResult As XlXmlExportResult
    Dim exportPath As String
    Dim xmlMap As String
    Dim fileContents As String
    exportPath = RequestExportPath()
    If exportPath = "" Or exportPath = "False" Then Exit Sub
    xmlMap = range("XmlMap")
    exportResult = ActiveWorkbook.XmlMaps(xmlMap).Export(exportPath, True)
    If exportResult = xlXmlExportValidationFailed Then
        Beep
        Exit Sub
    End If
    fileContents = ReadInTextFile(exportPath)
    fileContents = ApplyReplaceRules(fileContents)
    WriteTextToFile exportPath, fileContents
End Sub

Function ApplyReplaceRules(fileContents As String) As String
    Dim replaceWorksheet As Worksheet
    Dim findWhatRange As range
    Dim replaceWithRange As range
    Dim findWhat As String
    Dim replaceWith As String
    Dim cell As Integer
    Set findWhatRange = range("FindWhat")
    Set replaceWithRange = range("ReplaceWith")
    For cell = 1 To findWhatRange.Cells.Count
        findWhat = findWhatRange.Cells(cell)
        If findWhat <> "" Then
            replaceWith = replaceWithRange.Cells(cell)
            fileContents = Replace(fileContents, findWhat, replaceWith)
        End If
    Next cell
    ApplyReplaceRules = fileContents
End Function

Function RequestExportPath() As String
    Dim messageBoxResult As VbMsgBoxResult
    Dim exportPath As String
    Dim message As String
    message = "The file already exists. Do you want to replace it?"
    Do While True
        exportPath = Application.GetSaveAsFilename("", "XML Files (*.xml),*.xml")
        If exportPath = "False" Then Exit Do
        If Not FileExists(exportPath) Then Exit Do
        messageBoxResult = MsgBox(message, vbYesNo, "File Exists")
        If messageBoxResult = vbYes Then Exit Do
    Loop
    RequestExportPath = exportPath
End Function

Function FileExists(path As String) As Boolean
    Dim fileSystemObject
    Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
    FileExists = fileSystemObject.FileExists(path)
End Function

Function ReadInTextFile(path As String) As String
    Dim fileSystemObject
    Dim textStream
    Dim fileContents As String
    Dim line As String
    Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
    Set textStream = fileSystemObject.OpenTextFile(path)
    fileContents = textStream.ReadAll
    textStream.Close
    ReadInTextFile = fileContents
End Function

Sub WriteTextToFile(path As String, fileContents As String)
    Dim fileSystemObject
    Dim textStream
    Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
    Set textStream = fileSystemObject.CreateTextFile(path, True)
    textStream.Write fileContents
    textStream.Close
End Sub
Run Code Online (Sandbox Code Playgroud)