Search code examples
xmlxpathschematron

How to determine if there is text at a node with xpath/schematron


I'm looking for a way to write an xpath/schematron test to identify a specific node any time it contains any sort of non-white space, unwrapped text anywhere directly within the node (meaning possibly among, but not in a child element). So if my xml looks like this:

<root>
...
 <node>
     <arbitraryChild/>
     Find me
     <arbitraryChild>Don't find me</arbitraryChild>
     more text
 </node>
 ...
 <node>
     <arbitraryChild/> <arbitraryChild/>
     <arbitraryChild>Don't find me</arbitraryChild>
 </someNode>
...
</node>    

It would identify the first instance of somenode, but not the second. I've tried about every variation of contains(...), text(), and test="..." I can think of, but clearly I'm approaching this from the wrong direction.


Solution

  • I think it's simply

    node[text()[normalize-space()]]

    I don't think the "and *" in the answer from @har07 relates to any requirement stated in your question: you didn't say there had to be at least one element child, only that there had to be (non-empty) text.