-1

XSLTを使用してhtmlコンテンツをxml構造にスクレイプしようとしていますが、XALAN(CLI)を使用してxsltをhtmlに対してテストし、結果に満足したら、xsltファイルを取得し、トランスフォーマーを使用してjavaコードから使用しました( javax.xml.transform.TransformerFactory)、以下は実際のコードとスタイルシートに似たテスト値です。

私のhtmlサンプルデータ:

<html><body class='home'>
        <div >Welcome !!</div>
    <table border='0'><tr><td colspan='2'>asdas</td></tr>
        <tr><td class='footer' colspan='2' align='center'>Disclaimer: The information provided below is for informative purpose only and it is not a legal document.</td></tr>
        <tr><td colspan='2'>test;</td></tr>
    <tr><td class='Home' width='50%' aligh='center'> number:</td><td class='Home' width='50%' aligh='center'>515120</td></tr><tr><td class='Home' width='50%' aligh='center'>Connection :</td><td class='Home' width='50%' aligh='center'>123.23</td></tr><tr><td class='Home'>period (month / year):</td><td class='Home'>04/2012</td></tr><tr><td class='Home'>Date:</td><td class='Home'>APRIL     08,2012, 21:35</td></tr>  </table>
    </body>
    </html>

私のxslのみのテンプレートは次のとおりです。

<xsl:template match="*">
<usage_channel>
<head><xsl:value-of select="//div/text()" /></head>
<body><xsl:value-of select="//td/font/text()" /></body>
<footer><xsl:value-of select="body/table/tr[contains(td,'number')]/td[1]/text()" /></footer>
</usage_channel>
</xsl:template>

XALAN(cli)を使用した結果:

<?xml version="1.0" encoding="UTF-8"?><usage_channel><head>Welcome !!</head><body/><footer> number:</footer></usage_channel>

Javaトランスフォーマーを使用した結果:

<?xml version="1.0" encoding="UTF-8"?>
<usage_channel>
   <head>Welcome !!</head>
   <body/>
   <footer/>
</usage_channel>

tdの値をキャッチすることを期待してすべての組み合わせを試しましたが、失敗しました。ここで何が欠けていますか?

4

1 に答える 1

0

HTMLCleaner の使用により、トランスフォーマーによって使用される HMTL データが未加工のデータとわずかに異なることがわかりました。そのため、その問題に基づいて、XSLT 選択クエリのほとんどが無効になりました。HTML を印刷して問題を発見する必要がありました。

HTMLCleaner によってクリーンアップされた後に変更された HTML:

<html>
<head></head>
<body class="home">
   <div>Welcome !!
   <center>
   <table border="0">
     <tbody>
       <tr><td colspan="2">asdas</td></tr>
       <tr><td class="footer" colspan="2" align="center">Disclaimer: The information provided below is for informative purpose only and it is not a legal document.</td></tr>
       <tr><td colspan="2">test;</td></tr>
       <tr><td class="Home" width="50%" aligh="center">number:</td><td class="Home" width="50%" aligh="center">515120</td></tr>
       <tr><td class="Home" width="50%" aligh="center">Connection :</td><td class="Home" width="50%" aligh="center">123.23</td></tr>
       <tr><td class="Home">period (month / year):</td><td class="Home">04/2012</td></tr>
       <tr><td class="Home">Date:</td><td class="Home">APRIL     08,2012, 21:35</td></tr>    
    </tbody>
  </table>
  </center>
  </div>
</body>
</html>

<center>path に依存していた私のクエリのほとんどを壊し、body/table/tr/tdそれらを変更してbody/div/center/table/tr/td問題を解決した新しいタグに注意してください。コメントありがとうございます!

于 2012-06-10T08:53:52.033 に答える