今週、私は興味深いタスクを受け取りました:古いJavaアプリケーションを粉砕します。このアプリケーションは独自の形式で特定のXMLファイルを分析します。私はXMLがジュニア開発者にとってセクシーではないことを知っていますが、それは驚くべき利点を持っています。ファイルを文法に対して検証することができます。そのような文法はXSDと呼ばれ、XML Schema Definitionの略語です。面白い事実:あなたはXSDをXMLで書きます。 この記事では、問題、私が試したこと、そして最終的な作業ソリューションについて説明します。 問題 良い点は、アプリケーションはすでに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. に固定されています。 I assumed that the このプロジェクトは進められ、実装されたはずです. I added the latest version to my build, and... nothing. I dug deeper into the JAR and found the following: 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のサポートが一貫しているため、あなたが困惑している場合は、私の世界へようこそ。 プロジェクトには、潜在的に興味深い分野が2つあります。 そして ♪ 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 CENTRAL それは当時私の頭に浮かばなかったが、投稿を書いている間に、私はチェックした: The page does offer a XSD 1.1 distribution. It would mean getting the JAR, creating a dummy POM, and publishing them on the internal Artifactory, which I don't have writing access to. 可能ですが、時間がかかります。 XERCESダウンロード 代替案を探す 私は代替案を探しましたが、見つけた唯一のものはサクソンでした。 サクソンパッケージは、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. XPath プロセッサは、提供された API を介してアプリケーションにアクセスできます. This supports XPath 2.0 and XPath 3.1. It can also be used in backward-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 サクソンパッケージは、XMLドキュメントを処理するためのツールのコレクションです。 XSLT プロセッサは、コマンド ラインから使用したり、アプリケーションから呼び出したりすることもできます。Saxon は XSLT 3.0 Recommendation を実装しています。 XPath プロセッサは、提供された API を介してアプリケーションにアクセスできます. 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 3.1 をサポートし、XQuery 1.0 または 3.0 クエリを実行することもできます。Saxon-EE では、XQuery Update 1.0 Recommendation で定義された XQuery 拡張も使用できますが、XQuery Update の後期作業プロジェクトはサポートされていません(W3C はこれらのバージョンでの作業を中止しました)。 XML Schema Processor. This supports both XSD 1.0 and XSD 1.1. 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. このプログラムは、XSLTおよびXQueryプロセッサのSchema-aware機能をサポートするために使用されています。 サクソンって何? サクソンって何? 少し調べてみると、サックスには2つの大きな欠点があることがわかりました。 通常の JAXP API を使用することは可能ですが、XSD 1.1 認証を含む、サクソンの独占的な API に切り替えなければなりません。 Saxon は 2 つの味で提供されます: Enterprise Edition は有料で、Home Edition は無料で、後者は XSD 1.1 認証を提供しません。 予算もないし、狩る時間もなかった。 Back to 広場 1 状況を再評価する時が来た? 源泉からの構築? 実行不可能? サクソン? 支払い。 私の最後の希望はAIでした。 Xerces の 2 つのビルドが XSD 1.1 機能を搭載していることが判明 彼らはOpenGISによって2015年から2016年に公開され、そこで公開することはかなり簡単でした。 具体的には、どの支店から来ているのかというと、 . MAVEN CENTRAL 彼らの ポム 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 ・Java Geek