2017-01-11 7 views
2

私はXSLの初心者です。 HTMLから2つの値(タイトルと説明)を抽出したいと思います。私は他のすべてのタイトルと説明の下を持ってXSLを使用してHTMLからタイトルと説明のようなメタデータを抽出する方法

/html/body/div[2]/div[4]/div[4]/table/tbody/ 

:私のHTMLは、例えばのために

... 
    tbody id="_tableBody"> 
    <tr id="tcm:526-94999" class="alt-rowcolor" style="display: table-row;"> 
    <th class="heading" scope="row" style="display: table-cell;"> 
    <a onclick="displayAgreementPDFPopIn('202', 'ddctable-526-93813', 'Link_1382596320857', '540', 'false')" href="javascript:void(0)">529 Plan – Investment Instructions</a> 
    </th> 
    <td class="description" style="display: table-cell;">Change how your future contributions are invested or make an exchange of the contributions and earnings currently invested in your 529 college savings plan.</td> 
    </tr> 
... 

どのように見えるかこれは、私はこれは要素のXPATHがある

<title> 529 Plan – Investment Instructions</title> 
<description> Change how your future contributions are invested or make an exchange of the contributions and earnings currently invested in your 529 college savings plan </description> 

たいですこのパス。私はこの変換のために以下のXSLを作成しました。

<xsl:template match="/"> 
    <xsl:apply-templates select="/html/body/div[2]/div[4]/div[4]/table/tbody" /> 
</xsl:template> 
<xsl:template match="tbody"> 
    <xsl:call-template name="PDF_metadata"> 
     </xsl:call-template> 
</xsl:template> 
<xsl:template name="PDF_metadata"> 
    <xsl:variable name="title" select="/tr/th/a"> 
    <xsl:variable name="description" select="/tr/th/td"/> 
    <xsl:attribute name="title"> 
     <xsl:value-of select="$title" /> 
    </xsl:attribute> 
    <xsl:attribute name="description"> 
     <xsl:value-of select="$description" /> 
</xsl:template> 

これはXSLを使用する正しい方法ですか?私はこの権利をしていますか?どんな助けもありがとう。

答えて

1

最後のテンプレートをあまりにも複雑に思っています。未テストが、私は、これはあなたが望むものに近いと思う:

<xsl:template name="PDF_metadata"> 
    <title> 
    <xsl:value-of select="tr/th/a" /> 
    </title> 
    <description> 
    <xsl:value-of select="tr/td" /> 
    </description> 
</xsl:template> 

更新オンラインXSLTテスターで遊ん

。これはあなたのために働くはずです。 3つのテンプレートすべてを1つの単純なテンプレートに置き換えます。

<xsl:template match="//tbody[@id='_tableBody']"> 
    <title> 
    <xsl:value-of select="tr/th/a" /> 
    </title> 
    <description> 
    <xsl:value-of select="tr/td" /> 
    </description> 
</xsl:template> 

説明:

//tbodyは、ルートノードの下任意<tbody/>ノードを見つけます。だけで<tbody/>と一致...

//tbody[@id='_tableBody']を、それがネストされている方法を深く重要で、またはそれは内部<div/>タグにあるものの位置、などしかし、これらの複数があることができ、そう...しません属性id='_tableBody'idの属性は一意である必要があるため、1つしか存在できません。

<xsl:value-of select="..." />では、すでに<tbody/>ノードにあります。タイトルを取得するには、現在のノード(<tbody/>ノード)からちょうどtr/th/aを使用して、文書のルートから/tr/th/a ...を検索する必要はありません(最初は/が見つからないことに注意してください)。説明のために同上。

+0

ありがとうございました。私は多くのtrとtdタグを持つ大きなhtmlファイルを持っています。私はその要素に到達するには、このパス全体/ html/body/div [2]/div [4]/div [4]/table/tbody /を指定する必要があると思います。どう思いますか? – Rose

+0

@NupurJaiswalが更新されました。あなたの 'tbody'タグは' id'属性を持っているので、 '/ html/body/div [2]/div [4]/div [4]/table/tbody'から ' // tbody [@id = '_ tableBody'] ' – AJNeufeld

+0

素晴らしい。どうもありがとう。私はこの答えをチェックします – Rose

関連する問題