xslt模板匹配具有特定属性的子元素值?
问题描述:
比方说一个有这个基本的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的其他子级。
答
这听起来像你要去什么是这样的:
<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
您是否忘记了第4或第5行的''标签? – JLRishe 2013-03-18 16:17:38