質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

XSD

XSDは一般的に、XMLドキュメントの特定のタイプの表現を含んだ、W3C XML Schemaフォーマットで書かれたドキュメントを指します。

Q&A

0回答

324閲覧

XML Schema で属性の値によって他の属性の検証を行いたい

l9uftn

総合スコア3

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

XSD

XSDは一般的に、XMLドキュメントの特定のタイプの表現を含んだ、W3C XML Schemaフォーマットで書かれたドキュメントを指します。

0グッド

0クリップ

投稿2020/11/27 00:53

編集2020/11/27 00:56

前提・実現したいこと

XMLスキーマ検証で属性の値によって、指定できる属性を定めたいのですが、
どのように記載すべきかわかりませんでした。
もし、方法についてご存知の方がいらっしゃれば、
お教えいただけないでしょうか。

具体例

以下のような XML に対し、下記に示すような検証を行いたいです。
① type の値が rectangle であれば、point4 は必須
② type の値が circle であれば、radius は必須

<?xml version="1.0" encoding="UTF-8"?> <project name="サンプル"> <shape type="rectangle" point1="0,0" point2="0,1" point3="1,1" point4="1,0"/> <shape type="triangle" point1="0,0" point2="0,1" point3="1,1"/> <shape type="circle" point1="0,0" radius="1"/> <project/>

試したこと

以下のように、XMLスキーマを作成したのですが、
①、②に示すような検証が行えるように作成できませんでした。

XMLSchema

1<?xml version="1.0" encoding="utf-8"?> 2<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 3 <xsd:element name="project"> 4 <xsd:complexType> 5 <xsd:all> 6 <xsd:element name="shape" type="shape_type"/> 7 </xsd:all> 8 </xsd:complexType> 9 </xsd:element> 10 <xsd:complexType name="shape_type"> 11 <xsd:attribute name="type" use="required"> 12 <xsd:simpleType> 13 <xsd:restriction base="xsd:string"> 14 <xsd:enumeration value="rectangle"/> 15 <xsd:enumeration value="triangle"/> 16 <xsd:enumeration value="circle"/> 17 </xsd:restriction> 18 </xsd:simpleType> 19 </xsd:attribute> 20 <xsd:attribute name="point1" use="required"> 21 <xsd:simpleType> 22 <xsd:restriction base="xsd:string"/> 23 </xsd:simpleType> 24 </xsd:attribute> 25 <xsd:attribute name="point2" use="optional"> 26 <xsd:simpleType> 27 <xsd:restriction base="xsd:string"/> 28 </xsd:simpleType> 29 </xsd:attribute> 30 <xsd:attribute name="point3" use="optional"> 31 <xsd:simpleType> 32 <xsd:restriction base="xsd:string"/> 33 </xsd:simpleType> 34 </xsd:attribute> 35 <xsd:attribute name="point4" use="optional"> 36 <xsd:simpleType> 37 <xsd:restriction base="xsd:string"/> 38 </xsd:simpleType> 39 </xsd:attribute> 40 <xsd:attribute name="radius" use="optional"> 41 <xsd:simpleType> 42 <xsd:restriction base="xsd:string"/> 43 </xsd:simpleType> 44 </xsd:attribute> 45 </xsd:complexType> 46</xsd:schema>

補足情報

事情によって、要素名を変更したりすることができないため、
属性の値によって判断したいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問