检测节点是否存在? [英] Detecting if a node exists?
问题描述
我有一组名为
的数据,里面有很多节点.
I have a set of data called <testData>
with many nodes inside.
如何检测节点是否存在?
How do I detect if the node exists or not?
我试过了
<xsl:if test="/testData">
和
<xsl:if test="../testData">
一个都不行.我确定这是可能的,但我不确定如何.:P
Neither one works. I'm sure this is possible but I'm not sure how. :P
对于上下文,XML 文件的布局如下
For context the XML file is laid out like this
<overall>
<body/>
<state/>
<data/>(the one I want access to
</overall>
我目前在 标签中,但我想全局访问它.
/overall/data
不应该工作吗?
I'm currently in the <body>
tag, though I'd like to access it globally. Shouldn't /overall/data
work?
编辑 2:现在我有一个数据索引,当将模板应用于 body 内部的标签时,我需要随时使用它.我怎么知道,在身体中,数据存在?有时会,有时不会.真的控制不了:)
Edit 2: Right now I have an index into data that I need to use at anytime when apply templates to the tags inside of body. How do I tell, while in body, that data exists? Sometimes it does, sometimes it doesn't. Can't really control that. :)
推荐答案
Try count(.//testdata) >0
.
但是,如果您的上下文节点是 textdata
并且您想测试它是否有 somenode
子节点,我会写:
However if your context node is textdata
and you want to test whether it has somenode
child or not i would write:
<xsl:if test="somenode">
...
</xsl:if>
但我认为这不是你真正想要的.我认为您应该阅读编写 XSLT 样式表的不同技术(推/拉处理等).应用这些时,通常不需要这样的表达式,样式表变得更简单.
But I think that's not what you really want. I think you should read on different techniques of writing XSLT stylesheets (push/pull processing, etc.). When applying these, then such expressions are not usually necessary and stylesheets become simplier.
这篇关于检测节点是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!