2011-09-08 42 views
1

我有一組具有屬性的標記,我只想在匹配屬性後將某些值拉入新組。XSLT - 匹配組中的屬性並拉取這些屬性的值

輸入文件看起來是這樣的:

<content> 
<manifest> 
<item id="id1272682" href="ch01.html"/> 
<item id="id1272759" href="ch02.html"/> 
</manifest> 
<spine> 
<itemref idref="id1272759"/> 
<itemref idref="id1273380"/> 
</spine> 
</content> 

我希望我的XSLT在脊柱尋找任何的itemref/@ IDREF值(也可以是多個)的匹配項/ @的ID該清單並將該值添加到脊柱,以便輸出如下所示:

<spine> 
<itemref idref="id1272759"/> 
</spine> 

這是我到目前爲止所做的。它出現我的if:語句正在工作,因爲我正在獲取正確數量的itemref標記,但沒有獲得idref的值。

<xsl:if test="itemref[attribute::idref = ../../manifest/item/@id]"> 
          <xsl:element name="itemref"> 
           <xsl:attribute name="idref"> 
            <xsl:value-of select="@idref"/> 
           </xsl:attribute> 
          </xsl:element> 
          </xsl:if> 

而且我的輸出是:

<spine> 
    <itemref idref=""/> 
</spine> 
+0

問得好,+1。查看我的答案,獲得完整,簡單,簡短和簡單的解決方案。 –

回答

0

這種轉變

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

<xsl:template match="spine"> 
    <spine> 
     <xsl:copy-of select="itemref[@idref = /*/manifest/item/@id]"/> 
    </spine> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<content> 
    <manifest> 
     <item id="id1272682" href="ch01.html"/> 
     <item id="id1272759" href="ch02.html"/> 
    </manifest> 
    <spine> 
     <itemref idref="id1272759"/> 
     <itemref idref="id1273380"/> 
    </spine> 
</content> 

產生想要的,正確的結果

<spine> 
    <itemref idref="id1272759"/> 
</spine> 
+0

這非常有幫助。謝謝 – Doug

+0

@Doug:我很高興我的問題很有用。在SO這裏,表達感激的官方方式是通過點擊答案旁邊的複選標記來接受最好的答案。你能否考慮這樣做? –