Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating an XML NCName in Java

I'm getting some values from Java annotations in an annotation processor to generate metadata. Some of these values are supposed to indicate XML element or attribute names. I'd like to validate the input to find out if the provided values are actually legal NCNames according to the XML specification. Only the local name is important in this case, the namespace URI doesn't play a part here.

Is there some simple way of finding out if a string is a legal XML element or attribute name? Preferably I'd use some XML API that is readily available in Java SE. One of the reasons I'm doing this stuff in the first place is to cut back on dependencies. I'm using JDK 7 so I have access to the most up-to-date classes/methods.

So far, browsing through content handler classes and SAX/DOM stuff hasn't yielded any result.

like image 541
G_H Avatar asked Sep 29 '11 18:09

G_H


1 Answers

If you're prepared to have Saxon on your class path you can do

new Name10Checker().isValidNCName(s);

I can't see anything simpler in the public JDK interface.

like image 199
Michael Kay Avatar answered Oct 01 '22 05:10

Michael Kay