1

I know this is a valid XML:

<ID>1</ID>
<NAME>ABC</NAME>
<Record>
    <PROFILEID>10</PROFILEID>
    <SIZE>100</SIZE>
</Record>
<Record>
    <PROFILEID>11</PROFILEID>
    <SIZE>120</SIZE>
</Record>

But I wonder is this valid?:

<PROFILEID>1</PROFILEID>
<NAME>ABC</NAME>
<Record>
    <PROFILEID>10</PROFILEID>
    <SIZE>100</SIZE>
</Record>
<Record>
    <PROFILEID>11</PROFILEID>
    <SIZE>120</SIZE>
</Record>

I mean can tags have same names for different depths? Or maybe can it have same name as its parent/child?

Thanks.

BurakS
  • 133
  • 1
  • 2
  • 6
  • 2
    Yes, its fine and normal, you'll just need to be careful with your XPath. You can use also namespaces to differentiate. – StuartLC May 26 '15 at 13:33
  • Technically, neither example is well-formed and therefore neither are valid because being well-formed is a prerequisite to being valid. You can add a single root element to make both examples be well-formed, but you cannot assess validity without the specification of a schema. Please see [Well-formed vs Valid XML](http://stackoverflow.com/a/25830482/290085) and [my answer below](http://stackoverflow.com/a/30461635/290085) for further details. – kjhughes May 26 '15 at 14:46
  • Thanks for the explanation guys. – BurakS Jun 01 '15 at 07:06

1 Answers1

0

Yes, same-named XML elements can appear at "different depths" in a well-formed XML document.

Validity is another matter and depends on the schema technology being used.

In XSD, yes, same-named XML elements can appear at different depths and can have different types at those different depths if so defined locally. However, in XSD 1.0 same-named XML elements cannot have different types as siblings. In XSD 1.1, conditional type assignment can be used to allow same-named siblings to have different types.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240