2012-12-04 19 views
0

我有我需要合併成一個,然後按字母順序排序2個XSLT模板:如何在XSLT中組合兩個模板?

<xsl:template match="properties-for-sale"> 
    <xsl:for-each select="entry"> 
    <option value="{name}"> 
     <xsl:value-of select="name"/> 
    </option> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="properties-for-rent"> 
    <xsl:for-each select="entry"> 
    <option value="{name}"> 
     <xsl:value-of select="name"/> 
    </option> 
    </xsl:for-each> 
</xsl:template> 

怎麼能在這個XSLT可以實現嗎?

感謝您的幫助!

+0

我只是好奇,你是否需要'<期權價值= 「{名}」>'或'<期權價值= 「{名()}」>'..反正在我的答案我有使用過'{name}'.. –

回答

1

您可以使用|運營商匹配

<xsl:template match="properties-for-sale|properties-for-rent"> 
    <xsl:for-each select="entry"> 
    <option value="{name}"> 
     <xsl:value-of select="name"/> 
    </option> 
    </xsl:for-each> 
</xsl:template> 
+0

哦優秀。這樣可行。非常感謝!只是想知道如何從另一個樣式表中引用該模板?既然它沒有名字,那有點棘手,不是嗎?對不起,XML n00b ... – Tintin81

+1

@ Tintin81你根本不需要參考模板。 XSLT處理器爲您做到了這一點。另外,你真的不應該使用''。相反,你應該寫一個輸出'

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

    <xsl:template match="properties-for-sale|properties-for-rent"> 
    <xsl:for-each select="entry"> 
     <xsl:sort select="name" order="ascending"/> 
     <option value="{name}"> 
     <xsl:value-of select="name"/> 
     </option> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

使用|多的XPath ..和使用<xsl:sort>用於排序的值..

參考,如果你想了解更多關於它!

http://www.w3schools.com/xsl/el_sort.asp

+0

+1排序,這是接受的答案中缺少。 –

+0

@DimitreNovatchev,你好:)謝謝你的讚賞:) –