xsl if and xsl foreach where element value is
this is my xml:
<root>
<a>
<b>
<t>1</t>
</b>
<b>
<t>2</t>
</b>
</a>
</root>
i want to ask: thats tell me if any t exist but i want true answer onky if b exsit and he have开发者_开发知识库 t=1
tanks
Use:
boolean(/*/*/b[t=1])
When evaluated against the provided XML document, the result is:
true()
Remember: Always try to avoid the //
abbreviation, because it causes extremely inefficient traversing of the whole (sub) tree rooted at the context node.
The test you looking for is
//b/t[text() = '1']
This test can now be used in a template
as the match, in a for-each
loop as a selector or in a if
statement as the test - e.g.:
<xsl:template match="//b/t[text() = '1']">
<!-- all t children of b with a content of 1 -->
</xsl:template>
<xsl:for-each select="//b/t[text() = '1']">
<!-- all t children of b with a content of 1 -->
</xsl:for-each>
<xsl:if test="//b/t[text() = '1']">
<!-- This is the true case -->
</xsl:if>
Note:
- This post is based on the asumption that you query from the root level and do not know where the b/t combination is. If you query from a point deeper in the hierarchy or know exactly the path to the b/t combination, you
might wantshouldtoreplace//
with something more appropriate to avoid inefficiencies. Also please note, that theThe text() node-test returns all text nodes underneath the context node. (Martin, thanks for pointing this out).text()
function concatenates the textual content of all descendant nodes, i.e. only use it in the above way if you can be sure that there are no further descendants.
精彩评论