2009-07-23 85 views
3

我一直在閱讀關於在文檔中選擇獨特節點的不同問題(使用Muenchian方法),但在我的情況下我不能使用鍵(或者我不知道如何),因爲我正在處理節點集而不是文檔。XSL:如何選擇節點集中的唯一節點

而且鍵不能在節點集上設置。基本上我有一個變量:

<xsl:variable name="limitedSet" select=" 
    $deviceInstanceNodeSet[position() &lt;= $tableMaxCol]" 
/> 

包含其本身含有<structure>元件<deviceInstance>節點 節點集合可以表示這種方式:

<deviceInstance name="Demux TSchannel" deviceIndex="0"> 
    <structure name="DemuxTschannelCaps"> 
    </structure> 
</deviceInstance> 
<deviceInstance name="Demux TSchannel" deviceIndex="1"> 
    <structure name="DemuxTschannelCaps"> 
    </structure> 
</deviceInstance> 
<deviceInstance name="Demux TSchannel" deviceIndex="3"> 
    <structure name="otherCaps"> 
    </structure> 
</deviceInstance> 

我不知道選擇<structure>元素只有不同的名字。選擇將在這個例子返回兩個<structure>元素,是:

<structure name="DemuxTschannelCaps"></structure> 
<structure name="otherCaps"></structure> 

我已經試過

select="$limitedSet//structure[not(@name=preceding::structure/@name)]" 

,但前面的那張軸沿所有文件,而不是$limitedSet

我被卡住了,有人可以幫助我。謝謝。

回答

3
<xsl:variable name="structure" select="$limitedSet//structure" /> 

<xsl:for-each select="$structure"> 
    <xsl:variable name="name" select="@name" /> 
    <xsl:if test="generate-id() = generate-id($structure[@name = $name][1])"> 
    <xsl:copy-of select="." /> 
    </xsl:if> 
</xsl:for-each> 

這可能是由一個鍵來輔助:

<xsl:key name="kStructureByName" match="structure" use="@name" /> 
<!-- ... --> 
<xsl:if test="generate-id() = generate-id(key('kStructureByName', $name)[1])"> 

根據您的輸入,關鍵必須捕捉到一些額外的上下文信息:

<xsl:key name="kStructureByName" match="structure" use=" 
    concat(ancestor::device[1]/@id, ',', @name) 
" /> 
<!-- ... --> 
<xsl:variable name="name" select="concat(ancestor::device[1]/@id, ',', @name)" /> 
<xsl:if test="generate-id() = generate-id(key('kStructureByName', $name)[1])"> 
+0

這是一個很好的答案,節省了我的一天,非常感謝那個快速回復。這個網站是偉大的,我會註冊給這個答案的排名。 Tahnk你這麼多。 SeB。 – SeB 2009-07-24 07:27:32

+0

我考慮了一下你的關鍵建議,但這不起作用,因爲我需要多個設備之間的單個結構,而且你的建議只適用於單個設備。 – SeB 2009-07-24 07:55:29

1
select="$limitedSet//structure[not(@name=preceding::structure[count($limitedSet) = count($limitedSet | ..)]/@name)]"