2016-05-17 12 views
1

Microsoft ExcelからエクスポートされたXMLスプレッドシートファイルの解析にVBScriptを使用しようとしています。私は<Row>要素の数を数えようと試みました。しかし、スクリプトは常に0を返します。私は間違って何をしましたか?複数の名前空間があるので、あなたはXPATHのためにこれらの名前空間を定義する必要がありますMicrosoft ExcelからエクスポートされたXMLスプレッドシートファイルを解析します。

<?xml version="1.0"?> 
<?mso-application progid="Excel.Sheet"?> 
<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"> 
    <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> 
     <Version>14.00</Version> 
    </DocumentProperties> 
    <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> 
     <AllowPNG/> 
    </OfficeDocumentSettings> 
    <Worksheet ss:Name="Sheet1"> 
     <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="2" x:FullColumns="1" 
     x:FullRows="1" ss:DefaultRowHeight="15"> 
      <Row ss:AutoFitHeight="0"> 
       <Cell><Data ss:Type="String">First Row</Data></Cell> 
      </Row> 
      <Row ss:AutoFitHeight="0"> 
       <Cell><Data ss:Type="String">Second Row</Data></Cell> 
      </Row> 
     </Table> 
    </Worksheet> 
</Workbook> 

答えて

4

Set oXML = CreateObject("Microsoft.XMLDOM") 

oXML.aSync = false 
oXML.SetProperty "SelectionLanguage", "XPath" 
oXML.SetProperty "ServerHTTPRequest", True 
oXML.validateOnParse = False 
oXML.resolveExternals = False 

oXML.Load "_test_.xml" 

MsgBox oXML.SelectNodes("//Row").length ' Return 0 

WScript.Quit 

' Looping through all nodes works fine 
Set nodes = oXML.selectNodes("//*")     
For i = 0 to nodes.length -1  
    Msgbox nodes(i).nodeName 
Next 

そして、ここでは、XMLファイルである:ここで

は私のVBScriptファイルです。これはデフォルトの名前空間であっても行う必要があります。そうでなければ、XPATHを使って名前空間から具体的な要素を得ることはできません。なぜ//*は動作しますが、がXPATHに属していないため、//Rowは動作しません。

名前空間を設定するには、setProperty Methodを使用します。 Second-Level DOM PropertiesおよびSelectionNamespaces Propertyも参照してください。

あなたの例:私はdでデフォルトの名前空間接頭辞をしているという例で

Set oXML = CreateObject("Microsoft.XMLDOM") 

oXML.aSync = false 
oXML.SetProperty "SelectionLanguage", "XPath" 
oXML.SetProperty "ServerHTTPRequest", True 
oXML.validateOnParse = False 
oXML.resolveExternals = False 

oXML.setProperty "SelectionNamespaces", "xmlns:d=""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""" 

oXML.Load "_test_.xml" 

MsgBox oXML.SelectNodes("//d:Row").length ' Return 2 

' Looping through all rows in Table 
Set nodes = oXML.selectNodes("//d:Table//*")  
For i = 0 to nodes.length -1 
    Msgbox nodes(i).nodeName 
Next 

+0

@Ansgar Wiechers:私は、IT技術テキストのリンクURIの透過性に対して何人かの人々が持っていることを決して理解していません。しかしそれでは。 ;-) –

+0

テキスト内のIMOリテラルURLは流暢で読むことを妨げるので、私はそれらを「通常の」リンクにすることを好みます。 URL短縮名とは異なり、マウスカーソルをリンク上に置くと完全なURLが表示されるため、これはリンクの透過性を著しく損なうものとは思われません。 –

+0

今はすべてが良いです。私は何かを学んだ。あなたのレッスンをありがとうございます:) – Teiv

関連する問題