I have a set of data called <testData>
with many nodes inside.
How do I detect if the node exists or not?
I've tried
<xsl:if test="/testData">
and
<xsl:if test="../testData">
Neither one works. I'm sure this is possible but I'm not sure how. :P
For context the XML file is laid out like this
<overall>
<body/>
<state/>
<data/>(the one I want access to
</overall>
I'm currently in the <body>
tag, though I'd like to access it globally. Shouldn't /overall/data
work?
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
.However if your context node is
textdata
and you want to test whether it hassomenode
child or not i would write: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.
This will work with XSL 1.0 if someone needs...
This XSLT:
Applied to this sample:
Will produce this correct result:
When applied to this sample:
Result will be: