이번 주, 나는 흥미로운 작업을 받았습니다 : 유래 된 Java 응용 프로그램을 먼지.이 응용 프로그램은 특정 XML 파일을 독점적 인 형식으로 분석합니다. XML은 주니어 개발자에게 섹시하게 들리지 않지만 놀라운 이점이 있습니다. 파일을 문법에 대해 검증 할 수 있습니다. 그러한 문법은 XML Schema Definition의 약자 인 XSD라고합니다. 재미있는 사실 : XML에서 XSD를 쓸 수 있습니다. 이 게시물에서는 문제, 내가 시도한 것, 그리고 최종 작업 솔루션을 설명합니다. 문제는 좋은 점은 애플리케이션이 이미 XSD를 사용하고 있다는 것입니다.그러나 애플리케이션이 시작되었을 때 XSD 검증이 이미 사용되었습니다. 몇 가지 중요한 기능을 추가했습니다.특히 v1.1는 추가합니다. 그리고 . 버전 1.0 버전 1.1 주장 정체성 제한 Java 코드에는 XSD 1.0에서 구현할 수 없지만 XSD 1.1에서 할 수 있는 수많은 '//TODO validate' 코멘트가 있었다. 순진한 접근법 뚜껑 아래, JDK는 파싱을 위해 포장 된 Xerces 구현을 사용합니다. 재미를 위해 설치된 JDK의 `com.sun.org.apache.xerces.internal.jaxp` 패키지를 확인하십시오.이 구현은 XSD 인증 1.1. 나는 그들이 프로젝트는 계속 진행되고 구현되었습니다.나는 내 빌드에 최신 버전을 추가했고... 아무것도 없습니다.나는 JAR에 더 깊이 파고 다음을 발견했습니다. 정규 Xerces public final class Constants { /** XML 1.1 feature ("xml-1.1"). */ public static final String XML_11_FEATURE = "xml-1.1"; // Constant to enable Schema 1.1 support public final static boolean SCHEMA_1_1_SUPPORT = false; public final static short SCHEMA_VERSION_1_0 = 1; public final static short SCHEMA_VERSION_1_0_EXTENDED = 2; } - 주식.java 주식.java 스키마 1.1 지원이 일정하기 때문에 혼란스럽다면, 내 세계에 오신 것을 환영합니다. 프로젝트에는 두 가지 잠재적으로 흥미로운 분야가 있습니다. 그리고 · The 클래스의 이전 분류는 다음과 같습니다 : 제스처 J xml-schema-1.1-dev xml-schema-1.1-tests Constants public final class Constants { /** XML 1.1 feature ("xml-1.1"). */ public static final String XML_11_FEATURE = "xml-1.1"; // Constant to enable Schema 1.1 support public final static boolean SCHEMA_1_1_SUPPORT = false; public final static short SCHEMA_VERSION_1_0 = 1; public final static short SCHEMA_VERSION_1_0_EXTENDED = 2; public final static short SCHEMA_VERSION_1_1 = 4; } 1) 아직 지원되지 않음 2.그러나 그것은 유망한 것처럼 보입니다. 주식.java 주식.java 나는 그런 예술을 찾고 있었다. : 헌신적 인 유물이 표시되지 않습니다. Maven 중앙 그것은 그때 내 마음에 들지 않았지만, 포스트를 쓰는 동안, 나는 확인했다 : 페이지는 XSD 1.1 배포를 제공합니다.JAR를 얻고, 멍청한 POM를 만들고, 내부 Artifactory에 게시하는 것을 의미합니다. Xerces 다운로드 대안을 찾는 나는 대안을 찾고 있었지만 내가 발견 한 유일한 것은 Saxon이었다. Saxon 패키지는 XML 문서를 처리하는 도구의 집합입니다. An XSLT processor, which can be used from the command line, or invoked from an application, using a supplied API. Saxon implements the XSLT 3.0 Recommendation. The product can also be used to run XSLT 2.0 stylesheets, or XSLT 1.0 stylesheets in backwards compatibility mode. An XPath processor accessible to applications via a supplied API. This supports XPath 2.0 and XPath 3.1. It can also be used in backwards-compatibility mode to evaluate XPath 1.0 expressions. An XQuery processor that can be used from the command line, or invoked from an application by use of a supplied API. This supports XQuery 3.1, which also allows XQuery 1.0 or 3.0 queries to be executed. With Saxon-EE, you can also use the XQuery extensions defined in the XQuery Update 1.0 Recommendation, but later working drafts of XQuery Update are not supported (W3C has abandoned work on these versions). An XML Schema Processor. . This can be used on its own to validate a schema for correctness, or to validate a source document against the definitions in a schema. It is also used to support the schema-aware functionality of the XSLT and XQuery processors. Like the other tools, it can be run from the command line, or invoked from an application. This supports both XSD 1.0 and XSD 1.1 Saxon 패키지는 XML 문서를 처리하는 도구의 집합입니다. 명령줄에서 사용할 수 있는 XSLT 프로세서 또는 제공된 API를 사용하여 애플리케이션에서 호출할 수 있는 XSLT 프로세서.Saxon은 XSLT 3.0 Recommendation을 구현합니다.The product can also be used to run XSLT 2.0 stylesheets, or XSLT 1.0 stylesheets in backward compatibility mode. 제공된 API를 통해 애플리케이션에 액세스할 수 있는 XPath 프로세서.This supports XPath 2.0 and XPath 3.1.It can also be used in backward-compatibility mode to evaluate XPath 1.0 expressions. 명령줄에서 사용할 수 있는 XQuery 프로세서, 또는 제공된 API를 사용하여 애플리케이션에서 호출할 수 있는 XQuery 프로세서.이것은 XQuery 3.1을 지원하며 XQuery 1.0 또는 3.0 쿼리를 실행할 수 있습니다.Saxon-EE를 사용하면 XQuery 업데이트 1.0 권장에 정의된 XQuery 확장을 사용할 수 있지만, 나중에 작동하는 XQuery 업데이트 초안은 지원되지 않습니다 (W3C는 이러한 버전에 대한 작업을 포기했습니다). XML Schema Processor.This는 XSD 1.0과 XSD 1.1을 모두 지원합니다.This는 스키마의 정밀성을 위해 스키마를 검증하거나 스키마의 정의에 대한 소스 문서를 검증하는 데 자체적으로 사용할 수 있습니다.It is also used to support the schema-aware functionality of the XSLT and XQuery processors.Like the other tools, it can be run from the command line, or invoked from an application. Saxon 란 무엇입니까? Saxon 란 무엇입니까? 약간의 조사 후, 나는 Sax가 두 가지 주요 단점이 있음을 발견했다 : 일반 JAXP API를 사용할 수 있지만, Saxon의 특허 API로 전환하여 전체 전원을 해제해야 합니다. Saxon은 두 가지 맛으로 제공됩니다 : 엔터프라이즈 에디션은 유료이며 홈 에디션은 무료입니다. 나는 그것을 사냥할 시간도 없고 예산도 없었다. Square One로 돌아갑니다. 그것은 상황을 재평가하는 시간이었다. 소스에서 건설? 실현할 수없는. 사산? 지불. 나의 마지막 희망은 AI 이었다. XSD 1.1 기능이있는 Xerces의 두 개의 버전이 존재하는 것으로 밝혀집니다. 그들은 OpenGIS에 의해 2015-2016 년에 출판되었으며, 그곳에서 출판하는 것이 꽤 쉬웠습니다. 실제로 그것이 어디에서 왔는지 언급 : . Maven 중앙 그들의 POM http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/ 이 시점에서, 나는 목표에서 몇 줄의 코드를 멀리했다 : var schemaFactory = SchemaFactory.newInstance(Constants.W3C_XML_SCHEMA11_NS_URI); var schema = schemaFactory.newSchema(schemaFile); var saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); saxParserFactory.setSchema(schema); var reader = saxParserFactory.newSAXParser().getXMLReader(); reader.setContentHandler(handler); reader.setErrorHandler(handler); reader.setEntityResolver(handler); XSD 1.1 버전을 설정 스케줄을 공장으로 설정하기 정규 SAX 파싱 나는 여전히이 " 야생"건설에서 보안 문제가 없다는 것을 제대로 검증해야하지만 효과가 있습니다 : 나는 XSD 1.1의 기능을 활용할 수 있습니다! 결론 이 게시물에서 나는 Java에서 XSD 1.1 검증을 구현하는 나의 여정을 설명했다.처음에 예상했던 것보다 훨씬 쉬운 일이지만, 같은 어려움에 처한 다른 사람들을 도울 수 있기를 바랍니다. 더 가기 : Java API for XML Processing (JAXP) Apache XercesTM 프로젝트 원본 출처: A Java Geek 자바 Geek