Skip to content

Commit

Permalink
Merge pull request #75 from SasinduDilshara/fix_toxml_issues
Browse files Browse the repository at this point in the history
Add namespace awareness for validating xml against xsd files
  • Loading branch information
gimantha authored Feb 26, 2025
2 parents d0d4e08 + 66e2da0 commit 324c848
Show file tree
Hide file tree
Showing 29 changed files with 402 additions and 72 deletions.
12 changes: 12 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_10.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/ns" xmlns="http://example.com/ns" elementFormDefault="qualified">
<xs:element name="profile">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
<xs:element name="bio" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
37 changes: 37 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_11.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/ns1"
xmlns="http://example.com/ns1"
xmlns:ns2="http://example.com/ns2"
xmlns:ns3="http://example.com/ns3"
elementFormDefault="qualified">

<!-- Import schemas from ns2 and ns3 -->
<xs:import namespace="http://example.com/ns2" schemaLocation="TestCase2.xsd"/>
<xs:import namespace="http://example.com/ns3" schemaLocation="TestCase3.xsd"/>

<!-- Define the complexType for book -->
<xs:complexType name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<!-- Define the complexType for library -->
<xs:complexType name="libraryType">
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded" type="bookType"/>
<xs:element name="location" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<!-- Define the 'book' element using the bookType complexType -->
<xs:element name="book" type="bookType"/>

<!-- Define the 'library' element using the libraryType complexType -->
<xs:element name="library" type="libraryType"/>

</xs:schema>
30 changes: 30 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_12.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/ns2"
xmlns="http://example.com/ns2"
xmlns:ns1="http://example.com/ns1"
xmlns:ns3="http://example.com/ns3"
elementFormDefault="qualified">

<xs:import namespace="http://example.com/ns1" schemaLocation="TestCase1.xsd"/>
<xs:import namespace="http://example.com/ns3" schemaLocation="TestCase3.xsd"/>

<xs:element name="authorDetails">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
<xs:element name="books" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="bookTitle" type="xs:string"/>
<xs:element name="bookPrice" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>
35 changes: 35 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_13.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/ns3"
xmlns="http://example.com/ns3"
xmlns:ns1="http://example.com/ns1"
xmlns:ns2="http://example.com/ns2"
elementFormDefault="qualified">

<xs:import namespace="http://example.com/ns1" schemaLocation="TestCase1.xsd"/>
<xs:import namespace="http://example.com/ns2" schemaLocation="TestCase2.xsd"/>

<xs:element name="store">
<xs:complexType>
<xs:sequence>
<xs:element name="storeName" type="xs:string"/>
<xs:element name="location" type="xs:string"/>
<xs:element name="inventory">
<xs:complexType>
<xs:sequence>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="itemName" type="xs:string"/>
<xs:element name="itemPrice" type="xs:decimal"/>
<xs:element name="itemQuantity" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
12 changes: 12 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_6.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/ns" xmlns="http://example.com/ns" elementFormDefault="qualified">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
12 changes: 12 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_7.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/ns" xmlns="http://example.com/ns" elementFormDefault="qualified">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="orderId" type="xs:int"/>
<xs:element name="customerName" type="xs:string"/>
<xs:element name="totalAmount" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
12 changes: 12 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_8.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/ns" xmlns="http://example.com/ns" elementFormDefault="qualified">
<xs:element name="address">
<xs:complexType>
<xs:sequence>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="zipcode" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
12 changes: 12 additions & 0 deletions ballerina/tests/resources/xsd_tests/schemas/schema_9.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/ns" xmlns="http://example.com/ns" elementFormDefault="qualified">
<xs:element name="invoice">
<xs:complexType>
<xs:sequence>
<xs:element name="invoiceNumber" type="xs:string"/>
<xs:element name="invoiceDate" type="xs:date"/>
<xs:element name="amount" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<profile>
<name>John Doe</name>
<age>30</age>
<bio>Software Developer</bio>
</profile>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<profile xmlns="http://example.com/ns">
<name>John Doe</name>
<age>30</age>
<bio>Software Developer</bio>
</profile>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book>
<title>XML for Beginners</title>
<author>John Doe</author>
<price>19.99</price>
<publisher>XYZ Publishers</publisher>
</book>
<book>
<title>Advanced XML</title>
<author>Jane Smith</author>
<price>29.99</price>
<publisher>ABC Publishers</publisher>
</book>
<location>Main Branch</location>
</library>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<library xmlns="http://example.com/ns1" xmlns:ns2="http://example.com/ns2" xmlns:ns3="http://example.com/ns3">
<book>
<title>XML for Beginners</title>
<author>John Doe</author>
<price>19.99</price>
<publisher>XYZ Publishers</publisher>
</book>
<book>
<title>Advanced XML</title>
<author>Jane Smith</author>
<price>29.99</price>
<publisher>ABC Publishers</publisher>
</book>
<location xmlns="http://example.com/ns2">Main Branch</location> <!-- Wrong namespace for 'location' -->
</library>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<library xmlns="http://example.com/ns1" xmlns:ns2="http://example.com/ns2" xmlns:ns3="http://example.com/ns3">
<book>
<title>XML for Beginners</title>
<author>John Doe</author>
<price>19.99</price>
<publisher>XYZ Publishers</publisher>
</book>
<book>
<title>Advanced XML</title>
<author>Jane Smith</author>
<price>29.99</price>
<publisher>ABC Publishers</publisher>
</book>
<location>Main Branch</location>
</library>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<authorDetails>
<firstName>John</firstName>
<lastName>Doe</lastName>
<age>45</age>
<books>
<bookTitle>XML for Beginners</bookTitle>
<bookPrice>19.99</bookPrice>
</books>
<books>
<bookTitle>Advanced XML</bookTitle>
<bookPrice>29.99</bookPrice>
</books>
</authorDetails>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<authorDetails xmlns="http://example.com/ns2" xmlns:ns1="http://example.com/ns1" xmlns:ns3="http://example.com/ns3">
<firstName>John</firstName>
<lastName>Doe</lastName>
<age>45</age>
<books xmlns="http://example.com/ns1">
<bookTitle>XML for Beginners</bookTitle>
<bookPrice>19.99</bookPrice>
</books>
<books xmlns="http://example.com/ns1">
<bookTitle>Advanced XML</bookTitle>
<bookPrice>29.99</bookPrice>
</books>
</authorDetails>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<authorDetails xmlns="http://example.com/ns2" xmlns:ns1="http://example.com/ns1" xmlns:ns3="http://example.com/ns3">
<firstName>John</firstName>
<lastName>Doe</lastName>
<age>45</age>
<books>
<bookTitle>XML for Beginners</bookTitle>
<bookPrice>19.99</bookPrice>
</books>
<books>
<bookTitle>Advanced XML</bookTitle>
<bookPrice>29.99</bookPrice>
</books>
</authorDetails>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<store>
<storeName>SuperMart</storeName>
<location>New York</location>
<inventory>
<item>
<itemName>Apple</itemName>
<itemPrice>1.25</itemPrice>
<itemQuantity>100</itemQuantity>
</item>
<item>
<itemName>Banana</itemName>
<itemPrice>0.75</itemPrice>
<itemQuantity>150</itemQuantity>
</item>
</inventory>
</store>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<store xmlns="http://example.com/ns3" xmlns:ns1="http://example.com/ns1" xmlns:ns2="http://example.com/ns2">
<storeName>SuperMart</storeName>
<location>New York</location>
<inventory>
<item xmlns="http://example.com/ns1">
<itemName>Apple</itemName>
<itemPrice>1.25</itemPrice>
<itemQuantity>100</itemQuantity>
</item>
<item xmlns="http://example.com/ns1">
<itemName>Banana</itemName>
<itemPrice>0.75</itemPrice>
<itemQuantity>150</itemQuantity>
</item>
</inventory>
</store>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<store xmlns="http://example.com/ns3" xmlns:ns1="http://example.com/ns1" xmlns:ns2="http://example.com/ns2">
<storeName>SuperMart</storeName>
<location>New York</location>
<inventory>
<item>
<itemName>Apple</itemName>
<itemPrice>1.25</itemPrice>
<itemQuantity>100</itemQuantity>
</item>
<item>
<itemName>Banana</itemName>
<itemPrice>0.75</itemPrice>
<itemQuantity>150</itemQuantity>
</item>
</inventory>
</store>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://example.com/ns">
<title>XML for Beginners</title>
<price>29.99</price>
</book>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://example.com/ns">
<title>XML for Beginners</title>
<author>John Doe</author>
<price>29.99</price>
</book>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Missing orderId -->
<order xmlns="http://example.com/ns">
<customerName>Jane Doe</customerName>
<totalAmount>100.50</totalAmount>
</order>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- This is an order -->
<order xmlns="http://example.com/ns">
<orderId>12345</orderId>
<customerName>Jane Doe</customerName>
<totalAmount>100.50</totalAmount>
</order>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<address xmlns="http://example.com/ns">
<street>123 Elm Street</street>
<zipcode>98765</zipcode>
</address>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<address xmlns="http://example.com/ns">
<street> 123 Elm Street </street>
<city> Springfield </city>
<zipcode> 98765 </zipcode>
</address>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Missing amount element -->
<invoice xmlns="http://example.com/ns">
<invoiceNumber>INV123</invoiceNumber>
<invoiceDate>2024-02-25</invoiceDate>
</invoice>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Invoice details -->
<invoice xmlns="http://example.com/ns">
<invoiceNumber>INV123</invoiceNumber>
<invoiceDate>2024-02-25</invoiceDate>
<amount>250.75</amount>
</invoice>
Loading

0 comments on commit 324c848

Please sign in to comment.