You can load the XSD into Java and extract the expressions. Then you can use them in methods .matches()or create objects Patternif you intend to use them repeatedly.
First you need to load the XML into a Java program (I named it CodexSchema.xsd):
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document source = builder.parse(new File("CodexSchema.xsd"));
XPath , ( , , ). XPath, :
XPathFactory xPathfactory = XPathFactory.newInstance();
String typeName = "myCodex";
String xPathRoot = "//*[local-name()='simpleType'][@name='"+typeName+"']/*[local-name()='restriction']/*[local-name()='pattern']";
XPath patternsXPath = xPathfactory.newXPath();
, org.xml.dom.NodeList, <xs:pattern>.
NodeList patternNodes = (NodeList)patternsXPath.evaluate(xPathRoot, source, XPathConstants.NODESET);
value. :
public List<Pattern> getPatterns(NodeList patternNodes) {
List<Pattern> expressions = new ArrayList<>();
for(int i = 0; i < patternNodes.getLength(); i++) {
Element patternNode = (Element)patternNodes.item(i);
String regex = patternNode.getAttribute("value");
expressions.add(Pattern.compile(regex));
}
return expressions;
}
Pattern. String.
Java, :
for(Pattern p : getPatterns(patternNodes)) {
System.out.println(p);
}
:
Pattern pattern3 = getPatterns(patternNodes).get(2);
Matcher matcher = pattern3.matcher("47385628403");
System.out.println("test1: " + matcher.find());
System.out.println("test2: " + "47385628403".matches(pattern3.toString()));