Skip to main content

Notifications

XML Schema Validation

takolota1 Profile Picture Posted by takolota1 4,617


Want to validate XML messages? Don't want to use a $3600+ per year Azure Integration Account? Here's a way to validate XML messages against a schema for almost no cost, using a Python Azure Function & optionally some blob storage.


I personally had a project requirement to directly validate XML messages sent to one of my APIs and to return information on XML message errors to users calling the API. And my stakeholders did not want to convert to another format like JSON for the validations. I also did not like the need for an Azure Integration Account for what should be a relatively simple function. So I found a Python library that can do XML validations & created an Azure Function for it. 
One additional challenge is the XML schema for my validation use-case included several imports with multi-layered dependencies. So I also decided to create a public blob storage to host all the schema import files & I swapped out the import file references for URLs of my public blob storage files.


Examples

Schema validation failure due to a string in quantity integer field

Error message:
failed validating '1 TEST STRING IN INT ERROR' with XsdAtomicBuiltin(name='xs:positiveInteger'):
Reason: invalid literal for int() with base 10: '1 TEST STRING IN INT ERROR'
Schema component:
  <xs:simpleType xmlns:xs="http://www.w3.org/2001/XMLSchema" name="positiveInteger" id="positiveInteger">
    <xs:annotation>
      <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#positiveInteger" />
    </xs:annotation>
    <xs:restriction base="xs:nonNegativeInteger">
      <xs:minInclusive value="1" id="positiveInteger.minInclusive" />
    </xs:restriction>
  </xs:simpleType>
Instance type: <class 'xml.etree.ElementTree.Element'>
Instance:
  <quantity>1 TEST STRING IN INT ERROR</quantity>
Path: /shiporder/item[1]/quantity


Schema validation failure due to missing title field

Error message:
failed validating <Element 'item' at 0x7f2d2657d950> with XsdGroup(model='sequence', occurs=[1, 1]):
Reason: Unexpected child with tag 'note' at position 1. Tag 'title' expected.
Schema component:
  <xs:complexType xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:sequence>
      <xs:element name="title" type="xs:string" />
      <xs:element name="note" type="xs:string" minOccurs="0" />
      <xs:element name="quantity" type="xs:positiveInteger" />
      <xs:element name="price" type="xs:decimal" />
    </xs:sequence>
  </xs:complexType>
Instance type: <class 'xml.etree.ElementTree.Element'>
Instance:
  <item><note>Special Edition</note><quantity>1</quantity><price>10.90</price></item>
Path: /shiporder/item[1]



Schema validation success
​​​​​​​


Import & Set-Up

Find & download the Solution import package at the bottom of this main post. Go to the Power Apps home page (https://make.powerapps.com/). Select Solutions on the left-side menu, select Import solution, Browse your files & select the ValidateXML_1_0_0_xx.zip file you just downloaded. Then select Next & follow the menu prompts to apply or create the required connections for the solution flows. And finish importing the solution.


Once the solution is done importing, select the solution name in the list at the center of the screen. Once inside the solution click on the 3 vertical dots next to the flow name & select edit.


Now that the flow is imported & open, we need to set up the Azure Function used for the XML Schema Validation.


If you have already worked with and deployed Azure Functions before, then you can skip the extra installations.
If you haven't deployed Azure Functions, you can go to the Microsoft Store & make sure you have VS Code & Python installed.


Once VS Code is installed, open it. Go to the 4 blocks on the left side menu to open the list of extensions. Search for Azure in the extensions & select to install Azure Functions. Azure Account & Azure Resources will automatically be installed too.


Once all the extensions are installed, go to the Azure A on the left side menu & select to sign in to Azure.


Next set up a project folder on your machine for Azure Functions & a sub-folder for this XML Validation project.


Back in VS Code select the button to create a new Azure Function. Follow the Function set-up instructions selecting the XML Validations project folder you just created, Python language Model V2, and where in VS Code to open the new Azure Function project.


Once all the project files are loaded in VS Code, select the function_app.py file. Remove all the code in the file. Go back to the tab with the flow, open the "Azure Function Python Script" action, copy its contents & paste them into the function_app.py file in VS Code. Cntrl+S / Save the file.


Next go to the requirements.txt file. Go to the flow to the "Azure Function Requirements.txt" action & copy its contents. Paste the contents into the requirements.txt file in VS Code. Cntrl+S / Save the file.


Go back to the Azure A on the left-side menu. Select the Deploy function button. Select Create New in the list of function. Follow the menus/prompts to create a new function. (If Create New doesn't appear, you may have to log in to Azure, navigate to Azure Functions & go through the process to create a new function so the new function will appear in the list of function options to deploy to.)


Go to Azure & login. Go to Function App. Find & select the newly deployed function. Select the 1st function under Name. Select Get function URL & in the pop-up menu & copy the Function key url.



Paste the function URL into the URI input of the HTTP XML Validation action.


The flow is now set for you to test with the placeholder XML message & XML schema and/or for you to test your own content by replacing the XML message with one of your messages & replacing the Schema with your XML schema.


Optional Blob Schema Imports Example & HTTP Error Information Responses


Examples coming soon...






Categories:

AI Builder

Comments