|
WSDKを使ったExcel(XMLスプレッドシート)の取り込み |
|
データ操作
|
|
作者 森 章
|
|
2010年1月06日(水曜日) 16:25 |
|
ここでは、Excelのデータ形式の1つであるXMLスプレッドシートをWSDKを使用し読み込む際のtipsを紹介いたします。
WSDKを使用したXML解析で、パス表現を使って要素を抽出したいときは、下記のように要素を抽出します。
{XDMElement.search "タグ名"}
該当するXMLに名前空間がつけられていると、単純に名前のみではパスの認識ができません。(下記のXML参照) XMLが名前空間を含んでいる場合はひと手間加える必要があります。
{XDMElement.search "名前空間:タグ名"}
ただし、単純に上記のように検索を行うと「XDMException: No declaration for prefix '名前空間'」
という例外が出てしまいます。
そこで、名前空間を検索前に認識させる必要が出てきます。(名前空間認識プロシージャを参照)
名前空間を持ったXML(Book2.xml)
<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"> <Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Center"/> <Borders/> <Font ss:FontName="MS Pゴシック" x:CharSet="128" x:Family="Modern" ss:Size="11"/> <Interior/> <NumberFormat/> <Protection/> </Style> <Style ss:ID="s21"> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> </Style> <Style ss:ID="s23"> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Interior ss:Color="#FFCC00" ss:Pattern="Solid"/> </Style> </Styles> </Workbook> |
名前空間認識プロシージャ
|
{define-proc {dwim-prefix-declarations subject:XDMElement, context:XDMNamespaceContext = xml-namespace-context }:void {subject.walk-children || walk descendants and self include-self? = true, include-descendants? = true, {proc {n:XDMNode}:bool {type-switch n case e:XDMElement do || check declared prefixes {if-non-null ps = e.namespace-declarations then {for p in {ps.get-prefixes} do {if-non-null ns = {ps.get-uri p} then {if {context.get-uri p} == null then || extend global declarations accordingly {context.declare-prefix? p, ns}}}}}} || continue walking {return true}}} }
|
例
|
{value let loc:Url = {url "TESTDATA/Book2.xml"} ||XMLdocumentModelエレメント let xml:XDMElement = {build-xml preserve-whitespace? = false, loc}.root ||名前空間認識 {dwim-prefix-declarations xml} let worksheets:XDMNodeSet = {xml.search "ss:Styles"} {output worksheets.size} }
|
|
|
最終更新 ( 2010年1月06日(水曜日) 16:50 )
|