0
votes

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.

2
This appears to work test="normalize-space(text()[1])" My parser complains and fails any time I use text() and it hits a node with text with children interspersed in the middle of the string (it tells me "a sequence of more than one node is not allowed as the first argument..."). So by simply going after the first text node, I can definitively say that there is text in the node. Still, this seems dumb. There's got to be a better approach...wolfmason
Can you elaborate? Is there an error in the xml? It's an empty node, obviously, but it's only there to convey that there could be an element prior to the text. Perhaps even an empty one. [edit: intended recipient deleted comment, but info may still be relevant.]wolfmason

2 Answers

2
votes

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.

0
votes

You can try the following XPath expression to test :

test="text()[normalize-space()] and *"

Given a context element, above expression tests if current context element has non-empty text node(s) child as well as child element(s).

I'm not familiar with , but the expression works in XPath (f.e used within XPath predicate) and XSLT, so this worth a try.