Skip to main content

XML syntax validation

· 4 min read
Mattias Kindborg
Developer Offering

Why did we continue with validating XML code blocks for syntax validity?

The importance of well-formed XML

As a continuation of JSON syntax validation, we've now extended our validation efforts to XML code blocks. I'm not saying that writing well-formed XML is harder, it's just a different kind of hard.

For transparency, we're currently validating that the XML examples are syntactically correct and well-formed. This means you won't run into missing closing tags, malformed attributes, or namespace issues, but it's still possible for an example to have incorrect element names or values. Validating the semantics of XML examples against specific schemas is a much bigger challenge, but catching syntax errors is a solid foundation that'll save you from those frustrating moments when nothing works and you can't figure out why.

As with the JSON post, let's play a quick game of Spot the error. Below are three real XML examples from our documentation (all of which have now been fixed, alongside another 160+ similar issues we discovered). See if you can find the syntax mistake in each one. Expand the solution to check your answer!

Spot the error

Example 1

<axtpc:RemoveThirdPartyCredentialByEmail>
<axtpc:Email>kalle.karlsson@example.com</axtpc:Email>
<axtpc:ProviderActions type="" Name="" Value="" />
<axtpc:ProviderName>HIDMobileAccess</axtpc:ProviderName>
</axtpc:RemoveThirdPartyCredetialByEmail>
Solution for example 1

A classic typo that'll get you every time! The closing tag has a sneaky misspelling: </axtpc:RemoveThirdPartyCredetialByEmail> instead of </axtpc:RemoveThirdPartyCredentialByEmail>. Notice the missing "n" in "Credential"?

<axtpc:RemoveThirdPartyCredentialByEmail>
<axtpc:Email>kalle.karlsson@example.com</axtpc:Email>
<axtpc:ProviderActions type="" Name="" Value="" />
<axtpc:ProviderName>HIDMobileAccess</axtpc:ProviderName>
</axtpc:RemoveThirdPartyCredetialByEmail>

Example 2

<tns1:Device aev:NiceName="Device">
<tnsaxis:HardwareFailure aev:NiceName="Hardware failure">
<RadarFailure wstop:topic="true" aev:NiceName="Radar data failure">
<aev:MessageInstance aev:isProperty="true">
<aev:SourceInstance>
<aev:SimpleItemInstance
aev:NiceName="Channel Identifier"
Type="xsd:int"
Name="channel">
</aev:SourceInstance>
<aev:DataInstance>
<aev:SimpleItemInstance
aev:NiceName="Reason Code"
Type="xsd:int"
Name="reason" />
<aev:SimpleItemInstance
aev:NiceName="Reason Description"
Type="xsd:string"
Name="reasonstr" />
<aev:SimpleItemInstance
aev:NiceName="Radar Data Disruption"
Type="xsd:boolean"
Name="disruption"
isPropertyState="true" />
</aev:DataInstance>
</aev:MessageInstance>
</RadarFailure>
</tnsaxis:HardwareFailure>
</tns1:Device>
Solution for example 2

This one's particularly tricky because it's buried deep in a complex XML structure. The <aev:SimpleItemInstance> tag is missing its closing tag.

<tns1:Device aev:NiceName="Device">
<tnsaxis:HardwareFailure aev:NiceName="Hardware failure">
<RadarFailure wstop:topic="true" aev:NiceName="Radar data failure">
<aev:MessageInstance aev:isProperty="true">
<aev:SourceInstance>
<aev:SimpleItemInstance
aev:NiceName="Channel Identifier"
Type="xsd:int"
Name="channel">
</aev:SourceInstance>
<aev:DataInstance>
<aev:SimpleItemInstance
aev:NiceName="Reason Code"
Type="xsd:int"
Name="reason" />
<aev:SimpleItemInstance
aev:NiceName="Reason Description"
Type="xsd:string"
Name="reasonstr" />
<aev:SimpleItemInstance
aev:NiceName="Radar Data Disruption"
Type="xsd:boolean"
Name="disruption"
isPropertyState="true" />
</aev:DataInstance>
</aev:MessageInstance>
</RadarFailure>
</tnsaxis:HardwareFailure>
</tns1:Device>

Example 3

<axtpc:SetThirdPartyCredential>
<axtpc:ThirdPartyCredential>
<axtpc:Credential token="credential_token">
<axtpc:UserToken />
<axtpc:Description />
<axtpc:ValidFrom>2017-05-17T22:48:01Z</axtpc:ValidFrom>
<axtpc:ValidTo>2017-05-17T22:48:01Z</axtpc:ValidTo>
<axtpc:Enabled>true</axtpc:Enabled>
<axtpc:Status>NotActivated</axtpc:Status>
<axtpc:IdData Name="PIN" Value="1234" />
<axtpc:Attribute
type=""
Name="Email"
"Value="kalle.karlsson@example.com" />
<axtpc:Attribute
type=""
Name="LastName"
"Value="Karlsson" />
<axtpc:Attribute
type=""
Name="FirstName"
"Value="Kalle" />
<axtpc:AuthenticationProfile />
<axtpc:AuthenticationProfile />
<axtpc:CredentialAccessProfile>
<axtpc:ValidFrom>2017-05-17T22:48:01Z</axtpc:ValidFrom>
<axtpc:ValidTo>2017-05-17T22:48:01Z</axtpc:ValidTo>
<axtpc:AccessProfile />
</CredentialAccessProfile>
<axtpc:CredentialAccessProfile>
<axtpc:ValidFrom>2017-05-17T22:48:01Z</axtpc:ValidFrom>
<axtpc:ValidTo>2017-05-17T22:48:01Z</axtpc:ValidTo>
<axtpc:AccessProfile />
</axtpc:CredentialAccessProfile>
</axtpc:Credential>
</axtpc:ThirdPartyCredential>
<axtpc:ProviderActions type="" Name="" Value="" />
<actpc:ProviderName>HIDMobileAccess</axtpc:ProvideName>
</axtpc:SetThirdPartyCredential>
Solution for example 3

This example is a perfect storm of XML syntax errors that would make any developer's day a little bit worse! There are actually multiple issues here:

  1. Malformed attributes: The Value attributes have an extra quote mark, "Value="kalle.karlsson@example.com" should be Value="kalle.karlsson@example.com". It's a subtle typo that completely breaks the XML parsing.

  2. Mismatched closing tag: The </CredentialAccessProfile> closing tag is missing its namespace prefix, it should be </axtpc:CredentialAccessProfile>.

  3. Wrong namespace prefix: At the very end, <actpc:ProviderName> should be <axtpc:ProviderName>, and the closing tag has the wrong element name entirely, </axtpc:ProvideName> instead of </axtpc:ProviderName>.

<axtpc:SetThirdPartyCredential>
<axtpc:ThirdPartyCredential>
<axtpc:Credential token="credential_token">
<axtpc:UserToken />
<axtpc:Description />
<axtpc:ValidFrom>2017-05-17T22:48:01Z</axtpc:ValidFrom>
<axtpc:ValidTo>2017-05-17T22:48:01Z</axtpc:ValidTo>
<axtpc:Enabled>true</axtpc:Enabled>
<axtpc:Status>NotActivated</axtpc:Status>
<axtpc:IdData Name="PIN" Value="1234" />
<axtpc:Attribute
type=""
Name="Email"
"Value="kalle.karlsson@example.com" />
<axtpc:Attribute
type=""
Name="LastName"
"Value="Karlsson" />
<axtpc:Attribute
type=""
Name="FirstName"
"Value="Kalle" />
<axtpc:AuthenticationProfile />
<axtpc:AuthenticationProfile />
<axtpc:CredentialAccessProfile>
<axtpc:ValidFrom>2017-05-17T22:48:01Z</axtpc:ValidFrom>
<axtpc:ValidTo>2017-05-17T22:48:01Z</axtpc:ValidTo>
<axtpc:AccessProfile />
</CredentialAccessProfile>
<axtpc:CredentialAccessProfile>
<axtpc:ValidFrom>2017-05-17T22:48:01Z</axtpc:ValidFrom>
<axtpc:ValidTo>2017-05-17T22:48:01Z</axtpc:ValidTo>
<axtpc:AccessProfile />
</axtpc:CredentialAccessProfile>
</axtpc:Credential>
</axtpc:ThirdPartyCredential>
<axtpc:ProviderActions type="" Name="" Value="" />
<actpc:ProviderName>HIDMobileAccess</axtpc:ProvideName>
</axtpc:SetThirdPartyCredential>