XSLT:XHTMLからデータを取り出したいが上手く行かない
XHTMLから一部を切り出すためにXSLTの勉強中。
XSLTはXMLを読み込むから、XHTMLもいけるかと思ったけど上手く行かない。
とりあえずソース。
public class TestXSLT { public static void main( String[] args) throws Exception{ String xsl = "xsl/webryblog.xsl"; String xml = "xsl/webryblog.xml"; //String out = "xsl/result.xml"; String encoding = "UTF-8"; try { StreamSource tplXsl = new StreamSource(xsl); TransformerFactory trf = TransformerFactory.newInstance(); Transformer xslt = trf.newTransformer(tplXsl); StreamSource srcXml = new StreamSource(xml); xslt.setOutputProperty("encoding", encoding); xslt.transform(srcXml, new StreamResult(System.out)); } catch(Exception ex) { ex.printStackTrace(); } } }
実行すると以下のエラー。
エラー: 'Server returned HTTP response code: 503 for URL: http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
XHTMLファイルのドキュメントタイプ宣言を抜いて実行すると、以下のエラー。
エラー: 'The entity "nbsp" was referenced, but not declared.'
 を半角スペースに置換して実行すると、以下のエラー。
エラー: 'The reference to entity "ft" must end with the ';' delimiter.'
&を&に置換して実行すると、以下のエラー。
エラー: 'The element type "body" must be terminated by the matching end-tag "</body>".'
うーん。
タグの対応がおかしいみたい。
やっぱりXHTMLじゃ無理なのか。
CyberNeko HTML Parserを使うとHTMLをXMLとして扱えるってどこかに書かれていたけど、上手く解析できてない模様。
普通に正規表現で抜くのが早いのかな。
追記
よく見たらソースでdivタグを閉じまくってる部分を発見。
<script type="text/javascript"><!-- include_compo_left(1); // --></script> </div> <script type="text/javascript"><!-- include_compo_left(2); // --></script> </div> (以下略)
これが20まで続いてる。
Opera解析によるとbodyの閉じタグに該当する部分が↑の3つ目の/divになってる模様。
このソースは何がしたいのか。