1

昨日キーについて質問を投稿したところ、非常に役立つ回答が得られました。私は今日のほとんどの時間、この特定のファイル セットの最後の詳細に取り組んできました。キーの適切な使用に関して、私が見逃しているものがあるに違いありません。

次の定義リストがあります。

<dl>
<dlentry>
<dt>BLARG a</dt>
<dd>BLARG Definition b</dd>
</dlentry>
<dlentry>
<dt outputclass="values">Value c<ph> Value Description d</ph></dt>
<dd/>
</dlentry>
<dlentry>
<dt outputclass="values">Value e<ph> Value Description f</ph></dt>
<dd/>
</dlentry>
<dlentry>
<dt outputclass="values">Value g<ph> Value Description h</ph></dt>
<dd/>
</dlentry>
<dlentry>
<dt>BLARG2 i</dt>
<dd>BLARG2 Description j</dd>
</dlentry>
<dlentry>
<dt outputclass="values">Value k
<ph>Value description l</ph></dt>
<dd/>
</dlentry>
<dlentry>
<dt outputclass="values">Value m
    <ph>Value description n</ph></dt>
<dd/>
</dlentry>
<dlentry>
    <dt outputclass="values">Value o
    <ph>Value description p</ph></dt>
<dd/>
</dlentry>
<dlentry><dt>BLARG3 q</dt>
<dd>BLARG3 Definition r</dd></dlentry>
</dl>

これが私の変換です (これはあまり機能しません。今日はこれよりも近かったのですが、なんとか悪化させました)。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="kFollowing" match="dlentry[child::dt/@outputclass='values']" 
    use="generate-id(preceding-sibling::dlentry[not(child::dt/@outputclass='values')])"/>

<!-- identity template -->
<xsl:template match="@*|node()" name="identity">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="dd[../following-sibling::dlentry[1]/dt[@outputclass='values']]">
    <xsl:variable name="vFollowing" select="key('kFollowing',generate-id(..))"/>
    <dd><xsl:value-of select="."/>
        <table>
            <tgroup cols="2">
                <colspec colnum="1" colname="col1" colwidth="*"/>
                <colspec colnum="2" colname="col2" colwidth="*"/>
                <tbody>
                    <xsl:for-each select="../following-sibling::dlentry[key('kFollowing',$vFollowing)]">
                        <row>
                            <entry colname="1">
                                <xsl:value-of select="./dt/text()"/>
                            </entry>
                            <entry colname="2">
                                <xsl:apply-templates select="./dt/ph/text()"></xsl:apply-templates>
                            </entry>
                        </row>
                    </xsl:for-each>
                </tbody>
            </tgroup>
        </table></dd>
</xsl:template>
</xsl:stylesheet>

そして私の望む出力:

<dl>
<dlentry>
    <dt>BLARG a</dt>
    <dd>BLARG Definition b
    <table>
        <tgroup cols="2">
            <colspec colnum="1" colname="col1" colwidth="*"/>
            <colspec colnum="2" colname="col2" colwidth="*"/>
            <tbody>
                <row>
                    <entry colname="1">Value c</entry>
                    <entry colname="2">Value Description d</entry>
                </row>
                <row>
                    <entry colname="1">Value e</entry>
                    <entry colname="2">Value Description f</entry>
                </row>
                <row>
                    <entry colname="1">Value g</entry>
                    <entry colname="2">Value Description h</entry>
                </row>
            </tbody>
        </tgroup>
    </table></dd>
</dlentry>
<dlentry>
    <dt>BLARG2 i</dt>
    <dd>BLARG2 Definition j
        <table>
            <tgroup cols="2">
                <colspec colnum="1" colname="col1" colwidth="*"/>
                <colspec colnum="2" colname="col2" colwidth="*"/>
                <tbody>
                    <row>
                        <entry colname="1">Value k</entry>
                        <entry colname="2">Value Description l</entry>
                    </row>
                    <row>
                        <entry colname="1">Value m</entry>
                        <entry colname="2">Value Description n</entry>
                    </row>
                    <row>
                        <entry colname="1">Value o</entry>
                        <entry colname="2">Value Description p</entry>
                    </row>
                </tbody>
            </tgroup>
        </table></dd>
</dlentry>
<dlentry><dt>BLARG3 q</dt>
    <dd>BLARG3 Definition r<dd></dlentry>
</dl>

グループ化ルール(Sean の編集)

ノードは、dt[outputclass="values"] 子を持たないノードと、dt[outputclass="values"] 子を持つすべての後続の兄弟ノードにグループ化されます。

したがって、ノードの変換はグループ化されます。グループに含まれるメンバーが 1 つだけの場合 (つまり、dt[outputclass="values"] を持つ dlentry s がない場合)、サンプル出力ドキュメントに示されているテーブル出力は抑制されることに注意してください。

4

1 に答える 1

1

これはどう ...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:key name="dl-group" match="dlentry[child::dt/@outputclass='values']" 
    use="generate-id( preceding-sibling::dlentry[ not( child::dt/@outputclass='values')][1])"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>

<xsl:template match="dlentry/dd">
 <xsl:variable name="head" select="generate-id(..)" />      
 <xsl:copy>
  <xsl:value-of select="." />
  <xsl:if test="count( key('dl-group',$head))"> 
   <xsl:text>&#x0A;</xsl:text>
   <table>
    <tgroup cols="2">
     <colspec colnum="1" colname="col1" colwidth="*"/>
     <colspec colnum="2" colname="col2" colwidth="*"/>
     <tbody>
      <xsl:for-each select="key('dl-group',$head)" >
       <row>
        <entry colname="1">
         <xsl:value-of select="normalize-space(./dt/text())" />
        </entry>
        <entry colname="2"><xsl:value-of select="./dt/ph/text()" /></entry>
       </row>
      </xsl:for-each>
     </tbody>
    </tgroup>
   </table>
  </xsl:if> 
 </xsl:copy>
</xsl:template>

<xsl:template match="dlentry[dt[@outputclass='values']]" />

</xsl:stylesheet>

説明

これは、キーの単純なアプリケーションです。あなたは物事を複雑にしすぎていたようです。おそらくあなたはミュンヒアンのグループ化を考えていたのでしょう。再計算を保存するために、ヘッダーddノードのIDを変数に入れます。行を生成するには、key()関数によって返されるノードセットを反復処理するだけです。

于 2012-06-15T04:03:49.593 に答える