2009-06-28 7 views
6

私は、XMLファイルに格納された表形式のデータを編集するためにMS Excel 2007を使用して実験してきました。 XMLデータをスキーマ(xsdファイル)に対してインポートしたり検証したりする作業もしますが、エクスポートすると、xmlns、xlmns:xsi、およびxsi:schemaLocation属性がルート要素から削除されます。また、デフォルトの名前空間を明示的な名前空間に変更します。Excelにルート要素のXML属性を保存させる方法はありますか?

はここで、比較の前/後です:

(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> 

(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> 

Excelがこれらの属性を削除して邪魔しないようにする方法はありますか名前空間と一緒に? XMLマッピングとインポート/エクスポートに関するMSのヘルプを読んだことがありますが、GUIには何もしたくない設定はありません。私はカスタムマクロを書く必要がある場合は、それは可能性がありますが、より良い/簡単な方法がある場合は、私はむしろこれをやりたいと思います。

もう1つ質問:ExcelのようなUIを使用してXMLファイルの特定の部分を簡単に編集できる優れたツールはありますか?

+0

Excelは名前空間を変更していないことに注意してください。 'xsi:schemaLocation'を削除して、未使用の' xsi'名前空間を削除するだけです。 –

答えて

3

大丈夫、よく私は弾丸に噛み、良いol 'VBAマクロを書いた。他の誰かが同じ問題に遭遇した場合に備えて、私はそれをあなたと共有したいと考えました。

このマクロは基本的にExcelのビルトインXML Export()メソッドを呼び出し、結果のファイルに対して一連のテキスト置換を実行します。テキストの置換は完全にあなた次第です。ただ...下記のリンク内の1つのようなワークシートに

設定する方法の例をそれらを配置「のルールを置き換える」:この例では Click me for screen cap

、私は宇宙空間でタブを置き換えます、 ":ns1"(空白)、 "ns1:"(空白)、および削除されたルート要素(元のルート要素)。

あなただけの限り、あなたはこれらの指示に従って、ルールにあなたが好きなように取って代わるフォーマットすることができます。

  1. 全ての細胞およびそれらに名前を付ける*「FindWhat」(ドン」「見つける」を選択しますあなたの選択に見出し行を含める;空白は無視される)。
  2. すべての「置き換え」セルを選択し、* ReplaceWith(それらのセルに「何を見つけよう」と「置き換える」セルとの間に1対1のマッピングが必要です;空白を使用して不要なテキストを削除する必要があります)。
  3. ブックのどこかにXMLマップの名前を入力し、そのセルの名前を "XmlMap"にします。
  4. マクロを実行します。

* Excel 2007の名前の範囲に精通していない場合は、[式]タブをクリックし、[名前マネージャ]をクリックします。

さて、私はもうあなたをサスペンスにしていません...(LOL)...ここにマクロのコードがあります。それを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 
0

実際、それよりもはるかに簡単です。

  1. .xlsxサフィックス.zipに変更 - xlsx形式は、実際にxmlファイルをzip形式で圧縮されました!
  2. オープンxlサブディレクトリ
  3. コピー.zipフォルダ
  4. 編集お好みの名前空間にnsX:エントリを置き換えるために、ファイル以外の場所へxmlMaps.xmlファイルへ
  5. ブラウズWindowsエクスプローラでzipファイルと変更を保存します。
  6. コピーしたファイルやフォルダ.zip
  7. でバージョンを上書き今すぐあなたのXMLマップ(s)がお好みの名前空間を表示しますバック.xslx

にフォルダの名前を変更します。

関連する問題