Evan Hahn
Evan Hahn

Reputation: 12712

Differences in closing empty tags with XML

In XML, is there a difference between this...

<mytag></mytag>

...and this?

<mytag />

I understand that the latter is shorter and probably more idiomatic, but are they parsed differently?

Upvotes: 0

Views: 66

Answers (2)

Adriano Galesso Alves
Adriano Galesso Alves

Reputation: 819

Evan,

It will not parse differently.

Actually, I have a XML with both, look at ISBN and Model... It's likely that one had a value and now there is no value anymore. This XML was created by .net serialization.

  <SKU PartNumber="3697">
    <Product>6d690816-e06e-4a06-9205-97f0ee2f2500</Product>
    <RealPrice>145.00</RealPrice>
    <PromotionalPrice>145.00</PromotionalPrice>
    <Stock>1000</Stock>
    <FreeShipping>false</FreeShipping>
    <Dimensions>
      <Weight>0.50</Weight>
      <Height>0</Height>
      <Width>0</Width>
      <Depth>0</Depth>
    </Dimensions>
    <SpecificFields>
      <ISBN></ISBN>
      <EAN13>9780139242182</EAN13>
      <Model />
    </SpecificFields>
    <Variations />
    <Code>00000000-0000-0000-0000-000000000000</Code>
    <Status>true</Status>
    <MinStock>0</MinStock>
    <Default>true</Default>
  </SKU>

Upvotes: 1

Jim Garrison
Jim Garrison

Reputation: 86774

There is no semantic difference between the two forms.

They are represented exactly the same way in an in-memory DOM, and an XML serializer is allowed to output either one for an empty element. Some serializers provide a configuration option allowing you to choose the form you prefer.

Upvotes: 1

Related Questions