2

私の仕事は、すべてのノード ラベルを表示する必要があることです。表示する前に、それらのノード/ラベルが /Datum/DCR/Global/* にあるかどうかを確認する必要があります。存在する場合は、グローバル要素の値を表示します。子またはそうでなければ、私の例のノード要素ラベルから表示したいノード/ラベル「ホーム」はグローバル要素の子ノードにないため、/結果/サイトマップ/セグメント/ノード[1]/ラベルから表示し、 「インベントリ」はグローバル要素の子ノードにあるため、/Datum/DCR/Global/Inventory からインベントリを表示する必要があります。

XML ファイル:-

<Properties>
    <Data>
        <Datum ID="D01" Type="DCR" Name="Global">
            <DCR Category="BAT" Type="global">
                <Global>
                    <FindaDealer>Find a Dealer</FindaDealer> 
                    <Tools>Tools</Tools> 
                    <Inventory>Inventory</Inventory> 
                    <ContactDealers>Contact Dealers</ContactDealers> 
                 </Global>
            </DCR>
        </Datum>
        <Result>
            <site-map id="hnh1clwg" >
                <segment id="0" >
                    <node id="hnhi" >
                        <label key="">home</label> 
                    </node>
                    <node id="hn4h">
                      <label key="">Offers</label>
                    </node>
                    <node id="hn4l">
                      <label key="">Tools</label>
                    </node>
                    <node id="hn62">
                      <label key="">Inventory</label>
                    </node>
                  </segment>
              </site-map>
        </Result>
    </Data>
</properties>

XSL コード:-

<xsl:element name="ul">
        <xsl:for-each select="Properties/Data/Result/site-map/segment/node">
            <li>
                <a href="#"> 
                    <xsl:apply-templates select="/Properties/Data/Datum[@Name='Global']/DCR/Global/*">
                        <xsl:with-param name="nodelabel" select="label"/>
                    </xsl:apply-templates>          
                </a>
            </li>                                   
        </xsl:for-each>
    </xsl:element>          

<xsl:template name="Glossary" match="/Properties/Data/Datum[@Name='Global']/DCR/Global/*">
    <xsl:param name="nodelabel"/>
    <xsl:variable name="name" select="name(.)"/>
    <xsl:variable name="value" select="."/>

    <xsl:if test="($nodelabel = $value)">
        <xsl:text>From Global element : </xsl:text>                                             
    </xsl:if>
    <xsl:if test="not($nodelabel = $value) and position()=last()">
        <xsl:text>From Sitemap : </xsl:text>                                                
    </xsl:if>

</xsl:template>

私が得ている出力: -

From Sitemap : 
From Sitemap : 
From Global element : From Sitemap : 
From Global element : From Sitemap : 

望ましい出力:-

From Sitemap : 
From Sitemap : 
From Global element : 
From Global element :

どんなポインタも役に立ちます。

ありがとう

4

1 に答える 1

1

このフラグメントのロジックを本当に理解しているかどうかはわかりません。すべてのサイトマップ ノードのすべての要素にテンプレートを適用する必要はないようです。グローバル値にキーGlobal/*を定義して、これらのノードを値で検索できるようにすることができます

<xsl:key name="globalValues" match="Global/*" use="." />

ノードに対して 2 つの異なるテンプレートを定義できるようになりました。1 つはグローバル リストに対応するノードが存在するノードに一致し、もう 1 つは存在しないノードに一致します。

<xsl:template match="node[key('globalValues', label)]" priority="10">
  <xsl:text>From Global element : </xsl:text>
  <!-- within this template you can use
         key('globalValues', label)
       to access the corresponding Global/* element -->
</xsl:template>

<xsl:template match="node" priority="5">
  <xsl:text>From Sitemap : </xsl:text>
</xsl:template>

(この場合、デフォルトの優先度が正しいことを行うため、明示的な優先度は厳密には必要ありませんが、意図がより明確になると思います)。これらを配置したら、単純にすべてのノード要素にテンプレートを適用すると、テンプレート マッチャーが各ノードに適切なテンプレートを選択します。

<xsl:element name="ul">
    <xsl:for-each select="Properties/Data/Result/site-map/segment/node">
        <li>
            <a href="#"> 
                <xsl:apply-templates select="." />
            </a>
        </li>                                   
    </xsl:for-each>
</xsl:element>

またはより慣用的に、<li><a>をターゲット テンプレートに移動し、 を取り除きますfor-each

<xsl:apply-templates select="Properties/Data/Result/site-map/segment/node" />
于 2013-11-07T18:38:53.770 に答える