Showing posts with label XML-XSD-XSLT. Show all posts
Showing posts with label XML-XSD-XSLT. Show all posts

Tuesday, December 29, 2009

C# - XPathNavigator class to navigate XML


Share/Bookmark

Sunday, August 9, 2009

XSD - XML Schema constraints and value checks


Share/Bookmark

Friday, August 7, 2009

Valid XHTML document


An XHTML document is technically an XML document. XML documents can include an XML declaration as the first line in the document, something like this:
<?xml version="1.0" encoding="UTF-8"?>

There are only a few major rules to consider, but you have to follow them if you want to create a valid XHTML document. Here they are in brief:

* In an XHTML document every tag must be closed.
* Empty elements without content, must be correctly closed with a closing slash.
For example, a break tag is formatted <br />.
* Tags must be nested correctly,
it works like a LIFO queue, the last tag you open must be closed first.
* All XHTML tags should be in lowercase.
* All attribute values are enclosed in quotation marks.
* A valid XHTML document needs a valid XHTML DOCTYPE declaration.

The DOCTYPE declaration works for several purposes:
* DOCTYPE helps your page to be validated as XHTML.
* DOCTYPE allows the browser knows the version of your page markup language.
* DOCTYPE references the specific DTD for your page markup language.
* DOCTYPE enables your page to be displayed properly in standard web browsers
(IE, Netscape Navigator, Mozilla, Firefox, Opera, Chrome, and ...).

For an XHTML 1.0 document, you can choose one of three different DOCTYPES:
- strict
- transitional
- frames

- strict DOCTYPE declaration:
If for presenting and styling your document you are using CSS.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

- transitional DOCTYPE declaration:
If your document includes any presentational or styling markup code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

- frames DOCTYPE declaration:
If your document is in frames.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

The easiest to use is the transitional DOCTYPE declaration, but using CSS to present document sometimes is the best way.

After XHTML DOCTYPE declaration, it must have an additional line of markup:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

In XHTML, the above line works instead of the opening <html> in HTML document. It adds information about the XHTML namespace. (A namespace is basically a vocabulary of elements and attributes)
Share/Bookmark

Monday, July 27, 2009

C# - Validate XML document against a Schema - XmlReaderSettings

Sample code for Validate XML document against a Schema:

...
using System.Xml;
using System.Xml.Schema;
...

public static bool XmlValidateByXsd(string xmlFile, string xsdFile)
{
bool retVal = false;
if( ! File.Exists( xmlFile ) || ! File.Exists( xsdFile ) )
return retVal;

try
{
XmlReaderSettings xmlRdrSettings = new XmlReaderSettings();
xmlRdrSettings.ValidationType = ValidationType.Schema;

xmlRdrSettings.Schemas = new XmlSchemaSet();
xmlRdrSettings.Schemas.Add(null, xsdFile);
xmlRdrSettings.ValidationEventHandler +=
new ValidationEventHandler(ValidationEventHandler);
using (XmlReader xmlRdr = XmlReader.Create(xmlFile, xmlRdrSettings))
{
while (xmlRdr.Read()) { }
}
retVal = true;
}
catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}

return retVal;
}

private static void ValidationEventHandler(object sender, ValidationEventArgs args)
{
// Do error handling
// Console.WriteLine("Error: " + args.Message);
}


The code line to call the method to validate XML against XSD:

bool retVal = myClass.XmlValidateByXsd(@"C:\employee.xml", @"C:\employee.xsd");


Sample XML file:

<?xml version="1.0" standalone="yes"?>
<Employees xmlns="http://iborn2code.blogspot.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://iborn2code.blogspot.com employee.xsd">
<Employee>
<SIN>525252525</SIN>
<FirstName>AB BA</FirstName>
<City>Vancouver</City>
<Zip>V1V</Zip>
</Employee>
<Employee>
<SIN>525252527</SIN>
<FirstName>BC CB</FirstName>
<City>Kelowna</City>
<Zip>V2V</Zip>
</Employee>
</Employees>


Sample XML Schema for above XML:

<div class="mycode">
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="SIN" type="xs:string"/>
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="City" type="xs:string"/>
<xs:element name="Zip" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>
</div>

Share/Bookmark