xslt模板匹配具有特定属性的子元素值?

时间:2013-03-18 16:05:50

标签: xml xslt transform

让我们说有一个这个基本的xml文档:

<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的其他子节点。

1 个答案:

答案 0 :(得分: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>

这些都是你想要的吗?

相关问题