2013-03-18 134 views
1

比方說一個有這個基本的XML文檔:xslt模板匹配具有特定屬性的子元素值?

<result name="response" numFound="73" start="0"> 
    <doc> 
     <str name="contentType">Content1</str> 
     <str name="content">Some content here</str> 
    </doc> 
    <doc> 
     <str name="contentType">Content2</str> 
     <str name="content">Some other content</str> 
    </doc> 
</result> 

我打算使用不同的模板爲每個內容類型。什麼是模板匹配參數?當只有contentType字段是特定值時,我無法弄清楚如何匹配doc的其他子級。

+0

您是否忘記了第4或第5行的''標籤? – JLRishe 2013-03-18 16:17:38

回答

5

這聽起來像你要去什麼是這樣的:

<xsl:template match="doc[str[@name = 'contentType'] = 'Content1'] 
        /str[name = 'Content']"> 
    <!-- Process Content1 content str --> 
</xsl:template> 

<xsl:template match="doc[str[@name = 'contentType'] = 'Content2'] 
        /str[name = 'Content']"> 
    <!-- Process Content2 content str --> 
</xsl:template> 

或者,也許這樣的事情?

<xsl:template match="doc[str[@name = 'contentType'] = 'Content1']"> 
    <!-- Process Content1 doc --> 
</xsl:template> 

<xsl:template match="doc[str[@name = 'contentType'] = 'Content2']"> 
    <!-- Process Content2 doc --> 
</xsl:template> 

這些都是你要找的嗎?

+0

第二個正是我要找的。這也是我嘗試的迭代之一。看起來我的大多數問題都與默認模板(針對未知內容)上的全部匹配相關,而匹配更高的優先級。 – tlum 2013-03-18 17:28:25

相關問題