空の単純な要素と空の複雑な要素の違い-XML
xmlファイルの空の単純な要素と空の複雑な要素の間に違いはありますか?どうすればそれらを区別できますか?
私が見つけたw3schoolsで:
- 空の単純な要素
<element></element>
または<element />
; さらに、「空の要素は属性を持つことができます。」と書かれています。 - 空の複雑な要素
<product pid="1345"/>
回答
ある単純なまたは複雑なことではない(直接)要素のタイプの特性です。要素は型(具体的にはXSDスキーマで定義された型)に準拠していると言え、型は単純または複雑であると言えますが、単純型と複合型のカテゴリは、彼らは許可します。具体的には、複合型と単純型の両方で、などの空の要素<e/>
、およびなどのテキストコンテンツを含む要素が許可される場合があります<e>foo</e>
。属性を持つ要素または子要素が表示された場合、それは単純な型に準拠できないことがわかりますが、どちらも持っていない場合は、スキーマを見ないとわかりません。
The answer to your question "how can I distinguish them" is that you need to look in the schema; you can't distinguish them just from the instance alone.
An element being simple implies that it has no child elements and no attributes. If an element has child elements or attributes, it is considered to be complex.
Separately, to say that an element is empty is to say that it has no content – not only does it have no child elements, it also has no child text nodes.
Therefore, both simple and complex elements may be empty or non-empty.
Side note: Whether a start tag is self closing, <e/>
vs <e></e>
, regardless of its element being simple or complex, is insignificant and generally not able to be detected or defined at the XML level.
Examples
<e></e>
is simple and empty.<e>abc</e>
is simple and non-empty.<e a="1"></e>
is complex and empty.<e a="1">abc</e>
is complex and non-empty.
See also
- Define an XML element that must be empty and has no attributes
Summary
Generally, think of a complex type as having XML substructure and a simple type as lacking XML substructure. Think of empty as implying no content.