2016-10-16 9 views
0

XSLTを使用してXMLファイルに変換するHTMLファイルがあります。後者の子ノードで使用する最初の子ノードの日付値だけを取得する必要があります。後者の子ノードはデータ値を持たず、タグは空に見えます。私はXSLTのためにそれを行うためにポジショニングを使用しようとしたが、役に立たなかった。XSLT後の子ノードで最初の子ノード値を使用

関連するHTMLコードは次のとおりです。

<tbody> 
    <tr> 
    <th rowspan="8" scope="rowgroup">2005DEC</th> 
    <th scope="row">Blemish on Card</th> 

    </tr> 

    <tr> 
    <th scope="row">Damaged</th> 

    </tr> 

    <tr> 
    <th rowspan="8" scope="rowgroup">2006JAN</th> 
    <th scope="row">Lost</th> 

    </tr> 

    <tr> 
    <th scope="row">Stolen</th> 

    </tr> 
</tbody> 

関連するXSLTコードは次のとおりです。

<xsl:for-each select="html/body/div/div/div/table/tbody/tr"> 
    <Entry> 
     <xsl:choose> 
      <xsl:when test="th[@scope='rowgroup']"> 
       <Year> 
        <xsl:value-of select="substring(th[@scope='rowgroup']/., 1, 4)" /> 
       </Year> 
       <Month> 
        <xsl:value-of select="substring(th[@scope='rowgroup']/., 5, 3)" /> 
       </Month> 
      </xsl:when> 
      <xsl:otherwise> 
       <Year> 
        <xsl:value-of select="substring(tr[1]/th[@scope='rowgroup']/., 1, 4)" /> 
       </Year> 
       <Month> 
        <xsl:value-of select="substring(tr[1]/th[@scope='rowgroup']/., 5, 3)" /> 
       </Month> 
      </xsl:otherwise> 
     </xsl:choose> 
    </Entry> 
</xsl:for-each> 

出力されるXMLコードは、私が第二子として望むものではありません以下であり、ノードは、最初の子ノードの日付値を持っており、ちょうど一年と月の空の値をタグとして表示されていません。

<Entry> 
    <Year>2005</Year> 
    <Month>DEC</Month> 
    <Reason>Blemish on Card</Reason> 
</Entry> 
<Entry> 
    <Year/> 
    <Month/> 
    <Reason>Damaged</Reason> 
</Entry> 
<Entry> 
    <Year>2006</Year> 
    <Month>JAN</Month> 
    <Reason>Lost</Reason> 
</Entry> 
<Entry> 
    <Year/> 
    <Month/> 
    <Reason>Stolen</Reason> 
</Entry> 

私が必要としているのは:

<Entry> 
    <Year>2005</Year> 
    <Month>DEC</Month> 
    <Reason>Blemish on Card</Reason> 
</Entry> 
<Entry> 
    <Year>2005</Year> 
    <Month>DEC</Month> 
    <Reason>Damaged</Reason> 
</Entry> 
<Entry> 
    <Year>2006</Year> 
    <Month>JAN</Month> 
    <Reason>Lost</Reason> 
</Entry> 
<Entry> 
    <Year>2006</Year> 
    <Month>JAN</Month> 
    <Reason>Stolen</Reason> 
</Entry> 

私のXSLTはどうすればいいですか?

答えて

1

最初の行からデータを選択する場合は、最初に移動する必要があります(substring(../tr[1]/th[@scope='rowgroup']/., 1, 4))。あなたはscope="rowgroup"属性を持つ最初の前の兄弟行のデータを選択したい場合は、その後一つの方法は、あなたのアプローチを継続することは、私ものの

<xsl:for-each select="//tbody/tr"> 
    <Entry> 
     <xsl:choose> 
      <xsl:when test="th[@scope='rowgroup']"> 
       <Year> 
        <xsl:value-of select="substring(th[@scope='rowgroup']/., 1, 4)" /> 
       </Year> 
       <Month> 
        <xsl:value-of select="substring(th[@scope='rowgroup']/., 5, 3)" /> 
       </Month> 
      </xsl:when> 
      <xsl:otherwise> 
       <Year> 
        <xsl:value-of select="substring(preceding-sibling::tr[th[@scope='rowgroup']][1]/th[@scope='rowgroup'], 1, 4)" /> 
       </Year> 
       <Month> 
        <xsl:value-of select="substring(preceding-sibling::tr[th[@scope='rowgroup']][1]/th[@scope='rowgroup'], 5, 3)" /> 
       </Month> 
      </xsl:otherwise> 
     </xsl:choose> 

     <xsl:for-each select="th[@scope='row']"> 
      <Reason> 
       <xsl:value-of select="." /> 
      </Reason> 
     </xsl:for-each> 
    </Entry> 
</xsl:for-each> 

で、あなたが望むものではありませんあなたのコメントに基づいて

XSLT 2.0プロセッサを使用する場合、for-each-group group-starting-with="tr[th[@scope = 'rowgroup']]"を使用する方が簡単かもしれません。

+0

これは機能しますが、foreachに影響します。 foreachは複数の期間を対象としますが、各foreach内の後の子ノードは最初の子ノードの値を取得しますが、最初のforeachからの最初の子ノードの値のみを使用します。 – iteong

+0

あなたは1つのテーブルと1つのテーブルしか表示していないので、私はあなたのコメントが何に関係しているのか分かりません。問題が解決しない場合は、質問を編集して、必要な入力内容、必要なXSLT、必要な出力、取得したものを表示します。 –

+0

'substring(先行兄弟:: tr [@スコープ= '行グループ'] [1]/th [@スコープ= '行グループ'] /。、1、4)'あなたが望む結果を与える?私は現在、関連する値を持つ行をどのように識別したいかはわかりません。 XSLで –

関連する問題