How Do I Check if an XML File Has any nodes, other than the root?
With XSLT, I am processing an xml fil开发者_如何转开发e which might not have any nodes, other than the root. I want to output a special message on the html page for this case. I am using a for-each element to process the xml file. How do I check if the xml file has any actual nodes in it?
How Do I Check if an XML File Has No Nodes?
By definition, any well-formed XML document contains at least a top element.
Therefore, any XML document contains some nodes -- there can't be an XML document that has no nodes.
I understand this question as asking: "How to determine that the top element of an XML document has no descendents?"
This is in fact an XPath question. This XPath expression:
/*[not(node())]
is true()
exactly when the top element of the document has no child nodes (elements, text-nodes, processing instructions or comments).
The top element can still have attributes and it always has namespace nodes, but these two kinds of nodes are not considered to be exactly "children".
/*[not(node()) and not(@*)]
is true()
exactly when the top element has no child nodes and no attributes.
/*[not(*)]
is true()
exactly when the top element has no child element nodes (but it still can have text-node children, processing-instruction children and comment-nodes children).
精彩评论