?XML !doctype !element !element !element !element Note To /to From /from Heading /heading Body /body /note

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Department of Computer Science and Engineering (Data Science)

A.Y.: 2022-23 Class: S.Y.B.Tech. Sem: IV Sub: Web Engineering


Experiment 7
(XML & XSL)
Aim: To implement XML and XSL

1. Design XML using XML DTD and schema.

2. Implementing XSL elements in XML.

3. Validating XML data through DTD and storing in database.

Theory:
XML Schema:An XML Schema describes the structure of an XML document. The purpose
of an XML Schema is to define the legal building blocks of an XML document:

 the elements and attributes that can appear in a document


 the number of (and order of) child elements
 data types for elements and attributes
 default and fixed values for elements and attributes

The XML Schema language is also referred to as XML Schema Definition (XSD).

DTD: A DTD is a Document Type Definition. A DTD defines the structure and the legal
elements and attributes of an XML document. With a DTD, independent groups of people
can agree on a standard DTD for interchanging data. An application can use a DTD to verify
that XML data is valid.

An Internal DTD Declaration: If the DTD is declared inside the XML file, it must be
wrapped inside the <!DOCTYPE> definition: XML document with an internal DTD

<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
Department of Computer Science and Engineering (Data Science)
A.Y.: 2022-23 Class: S.Y.B.Tech. Sem: IV Sub: Web Engineering
In the XML file, select "view source" to view the DTD. The DTD is interpreted as follows:

 !DOCTYPE note defines that the root element of this document is note
 !ELEMENT note defines that the note element must contain four elements:
"to,from,heading,body"
 !ELEMENT to defines the to element to be of type "#PCDATA"
 !ELEMENT from defines the from element to be of type "#PCDATA"
 !ELEMENT heading defines the heading element to be of type "#PCDATA"
 !ELEMENT body defines the body element to be of type "#PCDATA"

An External DTD Declaration

If the DTD is declared in an external file, the <!DOCTYPE> definition must contain a
reference to the DTD file:

XML document with a reference to an external DTD

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

And here is the file "note.dtd", which contains the DTD:

<!ELEMENT note (to,from,heading,body)>


<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Create an XML document


1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. Then, create a
new XML file (on the File menu, point to New, and then click File).
2. Select the XML File type, and then click Open.
3. Add the following data to the XML document to represent a product in a catalog:
XMLCopy
Department of Computer Science and Engineering (Data Science)
A.Y.: 2022-23 Class: S.Y.B.Tech. Sem: IV Sub: Web Engineering
<Product ProductID="123">
<ProductName>Rugby jersey</ProductName>
</Product>
4. Save the file as Product.xml in a folder that you will be able to readily access later (the
code samples in this article assume a folder named C:\MyFolder).
Create a DTD and link to the XML document
1. In Visual Studio 2005 or in Visual Studio .NET, point to New on the File menu, and
then click File.
2. Select the Text File type, and then click Open.
3. Add the following DTD declarations to the file to describe the grammar of the XML
document:
XMLCopy
<!ELEMENT Product (ProductName)>
<!ATTLIST Product ProductID CDATA #REQUIRED>
<!ELEMENT ProductName (#PCDATA)>
4. Save the file as Product.dtd in the same folder as your XML document.
5. Reopen Product.xml in Visual Studio 2005 or in Visual Studio .NET; to do this, point
to Open on the File menu, and then click File. Add a DOCTYPE statement (below the ?
xml version="1.0" line), as follows. This links the XML document to the DTD file).
XMLCopy
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE Product SYSTEM "Product.dtd">
6. Save the modified XML document as ProductWithDTD.xml.

Perform validation by using a DTD

1. In Visual Studio 2005 or in Visual Studio .NET, create a new Visual Basic Console
Application project named ValidateXmlUsingVB.
2. Visual Studio 2005 or Visual Studio .NET displays a new file named Module1.vb. At
the beginning of this file, add two Imports statements, as follows:
vbnetCopy
Imports System.Xml ' For XmlTextReader and XmlValidatingReader
Imports System.Xml.Schema ' For XmlSchemaCollection (used later)
3. In Module1 (before the start of the Main subroutine), declare a boolean variable named
isValid, as follows:
vbnetCopy
'If a validation error occurs,
' set this flag to False
' in the validation event handler.
Private isValid As Boolean = True
4. In the Main subroutine, create an XmlTextReader object to read an XML document
from a text file. Then, create an XmlValidatingReader object to validate this XML data:
vbnetCopy
Dim r As New XmlTextReader("C:\MyFolder\ProductWithDTD.xml")
Dim v As New XmlValidatingReader(r)
Department of Computer Science and Engineering (Data Science)
A.Y.: 2022-23 Class: S.Y.B.Tech. Sem: IV Sub: Web Engineering
5. The XmlValidatingReader object has a ValidationType property, which indicates the
type of validation required (DTD, XDR, or Schema). Set this property to DTD, as
follows:
vbnetCopy
v.ValidationType = ValidationType.DTD
6. If any validation errors occur, the validating reader generates a validation event. Add
the following code to register a validation event handler :
vbnetCopy
AddHandler v.ValidationEventHandler, AddressOf MyValidationEventHandler
7. Add the following code to read and validate the XML document. If any validation
errors occur, MyValidationEventHandler will be called to handle the error. This
subroutine will set isValid to False (see step 8 of this section). Check the status of
isValid after validation to see whether the document is valid or invalid.
vbnetCopy
While v.Read()' Could add code here to process the content.
End While
v.Close()' Check whether the document is valid or invalid.
If isValid Then
Console.WriteLine("Document is valid")
Else
Console.WriteLine("Document is invalid")
End If
8. After the Main subroutine, write the MyValidationEventHandler subroutine, as
follows: vbnetCopy
Public Sub MyValidationEventHandler(ByVal sender As Object, _
ByVal args As ValidationEventArgs)
isValid = False
Console.WriteLine("Validation event" & vbCrLf & args.Message)
End Sub
9. Build and run the application.
The application should report that the XML document is valid.
10. In Visual Studio 2005 or in Visual Studio .NET, modify ProductWithDTD.xml to
make it invalid (for example, delete the ProductName Rugby
jersey/ ProductName element).
11. Run the application again.
The application should display the following error message:
Validation event Element 'Product' has incomplete content. Expected 'ProductName'.
An error occurred at file:///C:/MyFolder/ProductWithDTD.xml(4, 3). Document is
invalid
Store unstructured data in the SQL Server database
Most of the user-defined forms related platforms stores the XML template into the database
with XML data type and reference to that template user information will fill-up in the XML
document. A template will be the blank structure to the user for filling the details and filled
XML will be stored into the database as a user response.
Department of Computer Science and Engineering (Data Science)
A.Y.: 2022-23 Class: S.Y.B.Tech. Sem: IV Sub: Web Engineering
Advantages of XML data type in SQL Server:

 XML stores unstructured data in a relational database model


 XML itself validates the information inside the file with tags and its values using
DTD (Document Type Definition) and schema
 XML data type stores independent data structure; hence it can be integrated or used in
other database sources easily
 In a way, the XML data type reduces the back-end application burden as the XML can
be easily used with a UI
 The XML data type can be used with the input parameter in a function or stored
procedure
XML declaration in SQL Server
XML is a SQL Server system data type. The variable and column of the table can be defined
with the XML column in SQL Server. The current SQL Server databases are native XML
integrated and allow the user to store big unstructured data into the XML format and index
over the XML data type in order to enhance the Query performance:

The XML data type itself validates the input values while assigning value to the XML
column or variable. If something is wrong with the input, the query will return an error. For
example, I removed one closing tag (</title>) in the above XML, while assigning the value to
the variable. It will return an error “end tag does not match start tag” while assigning the
value to the variable:

Lab Assignments to complete in this session


1. Design XML using XML DTD and schema.
2. Implementing XSL elements in XML.
3. Validating XML data through DTD and storing in database.

You might also like