Professional Documents
Culture Documents
Objectives
Introduce
XML Including:
XML Documents Basics XML Schema XML Stylesheets & Transformations (XMLS/T)
Explore
Contents
Have The
XML Architecture
.NET
Looking Back
Tightly
Proprietary, Data
Non-extensible
XML!
XML
technologies introduced:
XML 1.0 - Document Basics XML Schemata XSLT: Style sheets and Transformations
.NET
& XML:
is XML?
Instance
Document by Example
XML Parser
What is XML?
Stands Language
1/2
Syntax rules Syntax & Grammar for creating Document Type Definitions
Widely
What is XML?
Designed
2/2
Data is logically structured Human readable, writeable and understandable text file! Easy to Parse; Easy to Read; and Easy to Write! Data that describes data; data with semantics
Metadata:
Looks
Uses tags to delimit data and create structure Does not specify how to display the data
XML Tag-Sets
Begin
Can have an empty element: <someTag /> XML document declaration: <?xml ... ?> Comments: <!-- some comment --> The document type declaration
Exceptions
Promote
Document Document
version information encoding type: UTF-8, UTF-16, ISO-10646-UCS-2, etc standalone declaration; indicates if there are external file references Namespace declaration(s), Processing Instructions (for applications), etc
An Internal declaration
<!DOCTYPE CustomerOrder [ <!-- internal DTD goes here! --> ]>
An External reference
<!DOCTYPE CustomerOrder SYSTEM "http://www.myco.com/CustOrder.dtd">
Document
Instance
This is the XML document instance Read as: the XML-ized data
Root Element
Required if a document type declaration exists Must have the same name as the declaration Can contain other elements Can have attributes assigned to them May or may not have a value Properties that are assigned to elements Provide additional element information
Elements
Attributes
XMLData + DTD
<!-- XML Data--> <a> <b> Some </b> <c> 100 </c> <c> 101 </c> </a>
DTD
<!ELEMENT a (b+, c?) > <!ELEMENT b (#PCDATA) > <!ELEMENT c (#PCDATA) >
Not Valid!
<!-- XML Data--> <a> <b> Some </b> <b> Thing </b> </a>
Valid
Whats a DTD?
Document Type Definition (DTD) Defines the syntax, grammar & semantics Defines the document structure
What Elements, Attributes, Entities, etc are permitted? How are the document elements related & structured?
Referenced by or defined in XML documents, but its not XML! Enables validation of XML documents using an XML Parser Can be referenced to by more than one XML document DTDs may reference other DTDs
DTD By Diagram
CustomerOrder Customer Person Address Address Address FName LName OrderNo Orders OrderNo ProductNo ProductNo ProductNo ProductNo ProductNo Orders Orders
DTD By Example
http://www.myco.com/dtd/order.dtd <?xml version = 1.0 encoding = UTF-8 ?> <!DOCTYPE CustomerOrder [ <!ELEMENT CustomerOrder (Customer, Orders*) > <!ELEMENT Customer (Person, Address+) > <!ELEMENT Person (FName, LName) > <!ELEMENT FName (#PCDATA) > <!ELEMENT LName (#PCDATA) > <!ELEMENT Address (#PCDATA) > <!ATTLIST Address AddrType ( billing | shipping | home ) shipping > <!ELEMENT Orders (OrderNo, ProductNo+) > <!ELEMENT OrderNo (#PCDATA) > <!ELEMENT ProductNo (#PCDATA) > ]>
XML Parser
Reads, parses & interprets the DTD and XML document Performs substitutions, validation or additional processing
Knows
Creates
stands for Document Object Model interface for HTML & XML documents
Programming An
Defines
W3C
http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/ http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/
<?xml version=1.0?>
Parser
Dom Tree Root Element Child Element Text Child Element Text
XML Document
All
Microsoft
IBM/Apache
XML Schema
Whats
Schema
Datatypes
Not Valid!
<!-- XML Data--> <a> <b> Some </b> <b> Thing </b> </a>
Valid
Whats a Schema?
Websters
The
A structured framework for your XML Documents! A definition language - with its own syntax & grammar A means to structure data and enhance it with semantics! Best of all: Its an alternative to the DTD!
Composed
of two parts:
datatypes
Complex types
Facets Declaring
<schema>
Built-in
Primitive Datatypes
string, double, recurringDuration, etc CDATA, integer, date, byte, etc Derived from the primitive types Example: integer is derived from double
Derived Datatypes:
User-defined
Simplest Type Declaration: on a primitive or the derived built-in datatypes contain sub-elements or attributes
Based
Cannot Can
May
May
complexContent
Substitution Abstract
http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/
Specify
Examples
Declaring <element> Elements using the <element> tag Elements are declared
<element name = FirstName type = string /> Based
<element name = Orders > <complexType> <sequence> <element name = OrderNo type = string /> <element name = ProductNo type = string /> </sequence> </complexType> </element> May
Declaring Attributes
Declared Value Can May
pairs
only be assigned to <complexType> types be grouped into an attribute group more later! on a <simpleType>, by reference or explicitly
Based
<attribute name = age type=integer /> <!-- OR --> <attribute name = age > <simpleType> <restriction base=integer> <maxLength = 3/> </restriction> </simpleType> </attribute>
to group related attributes together logical organization reuse defined once, referenced many times
Promotes
maintenance
Schema readability
Referenced
Schema Namespaces
Equivalent
to XML namespaces
http://www.w3.org/TR/1999/REC-xml-names-19990114/
Used
to qualify schema elements must itself be qualified with the schema namespace may have a namespace prefix for the schema
<schema>
<schema> targetNamespace
attribute
Helps the parser differentiate type definitions Used during schema validation Differentiates differing schema vocabularies in the schema Should match the schema namespace declaration
targetNamespace:CO ="http://www.myCo.com/CO"
targetNamespace:namespace_prefix = some_URI...
Example:
<!-- Declare the root element of our schema --> <xsd:element name="CustomerOrder" type="CO:CustomerOrder"/> <!-- Further Definitions & declarations not shown --> </xsd:schema>
found at:
http://www.w3.org/TR/1999/REC-xpath-19991116
Roadmap To Selection
Location
Syntax Paths
Location
parent, child, ancestor, attribute, / (the document), etc By tag name, node selector or wildcard (*) node( ), text( ), comment( ), etc position( ), count( ), etc Example: /Address:: * [@AddrType=billing]
Syntax exists
Operators
To
CustomerOrder/Customer/Address[@AddrType]
To
CustomerOrder/Orders/ProductNo[1] [$ProductNo]
Can
< (for <), > (for >), <= (for <=), etc
Can
and or
is XSLT?
Transforming
What is XSLT?
Widely used and open standard defined by the W3C A sub-specification of XSL http://www.w3.org/TR/1999/REC-xslt-19991116 Designed to be used independently of XSL Designed primarily for the transformation needed in XSL W3C defines XSLT: a language for transforming XML documents XSLT is more than a language its an XML programming language Can have rules, evaluate conditions, etc Offers the ability to transform one XML document into another Transform an XDR Schema to and XSD Schema! Transform an XML document into an HTML document
Source Schema
Processor contains a loaded XSLT style-sheet Loads the specified Stylesheet templates... Traverses the source document, node by node... Where a node matches a template... Applies the template to the node Outputs the (new) XML or HTML result document
Processor
<Orders > <OrderNo> 10 </OrderNo> <ProductNo> 100 </ProductNo> <ProductNo> 200 </ProductNo> </Orders > <Orders > <OrderNo> 20 </OrderNo> <ProductNo> 501 </ProductNo> </Orders >
Process of Transmutation
XSLT Stylesheet
XSLT Processor
<HTML> <BODY> <TABLE border = 3> <TR> <TD> 10 </TD> <TD> 100</TD> </TR> <TR> <TD> 10 </TD> <TD> 200</TD> </TR> <TR> <TR></TR> <TD> 20 </TD> <TD> 501 </TD> </TR> </TABLE> </BODY> </HTML>
Alchemy Anyone?
Need
Use
patterns to locate nodes in the source document transform the nodes as you like!
Then
<xsl:template/>
Used for selecting node or node sub-tree Use the match attribute to select a specific node
Then apply changes Used to recursively process children of the selected node Used to select all nodes with a specific value
<xsl:apply-templates />
<TABLE border = 3> <xsl:for-each select=Customer/Orders/OrderNo"> <xsl:for-each select=Customer/Orders/ProductNo"> <TR> <TD> <xsl:value-of select=OrderNo"/></TD> <TD> <xsl:value-of select=ProductNo"/></TD> </TR> </xsl:for-each> <TR></TR> </xsl:for-each> </TABLE>
</BODY> </HTML>
</xsl:template> <xsl:stylesheet>
Select a node and insert its value into the output stream
Many,
Repetition
Conditional processing
< xsl:if test = > < xsl:choose >, <xsl:when test = >, <xsl:otherwise>
Sorting
<xsl:sort >
Etc
Some
XML 1.0
http://www.w3.org/TR/1998/REC-xml-19980210
XML Namespaces
http://www.w3.org/TR/1999/REC-xml-names-19990114/
XML Schemas
http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/ http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/
XPath expressions
http://www.w3.org/TR/1999/REC-xpath-19991116
XSL/T transformations
http://www.w3.org/TR/1999/REC-xslt-19991116
http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/ http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/
SOAP 1.1
http://msdn.microsoft.com/xml/general/soapspec.asp
System.Xml Namespace
Overall
namespace for classes that provide XML support for creating, editing, navigating XML documents writing and manipulating documents via the DOM
Classes
Reading,
Use the XmlDocument class for XML documents Use the XmlDataDocument class relational or XML data
Classes
XmlReader
Abstract Fast, Base
Properties
Value: Gets the value of the node NodeType: Returns the type of node HasValue: Returns true if the node has a value LocalName: Gets the name of the node without its prefix ReadState: Returns the ReadState of the stream
XmlWriter
Abstract Fast, Base
Properties
XmlLang: Returns the current xml:lang scope XmlSpace: Returns the current xml:space
from the XmlReader & XmlWriter abstract classes all the functionality defined by their base classes
Implement Designed
Inherit
the properties of the XmlReader and XmlWriter methods support reading XML elements methods support writing XML elements
XmlTextReader
XmlTextWriter
XmlDocument
Derived
Properties of Interest:
ChildNodes: Returns all the children of the current node DocumentType: Gets the DOCTYPE declaration node DocumentElement: Returns the root XmlElement XmlResolver: Used to resolve DTD & schema references FirstChild: Returns the first child of the current node ParentNode: Returns the parent of the current node Value: Returns the (string) value of the current node CreateComment: Creates a comment node CreateElement: Creates an element node Load: Loads XML data using a URL, file, Stream, etc Save: Saves the XML document to a file, Stream, orwriter
Methods of Interest
.Serialization .Schema
XmlNode XmlNodeReader XmlNodeType XmlNotation XmlReader XmlSpace XmlText XmlTextReader XmlTextWriter XmlUrlResolver XmlWhitespace XmlWriter ...
XmlCharType XmlComment XmlConvert XmlDataDocument XmlDeclaration XmlDocument XmlDocumentFragment XmlDocumentType XmlElement XmlEntity XmlEntityReference XmlNamedNodeMap
XmlDocument By Example
using System.Xml; //Create an XmlDocument, Load it, Write it to the Console //One way: XmlDocument xDoc = new XmlDocument(); xDoc.Load( C:\\myData.xml"); xDoc.Save( Console.Out); //Second way (Use a XmlTextReader to read in load the XML): XmlTextReader reader = new XmlTextReader(C:\\myData.xml"); xDoc.Load( reader ); xDoc.Save( Console.Out); //Third way (Use a XmlTextWriter to output the XML document): XmlTextWriter writer = new XmlTextWriter( Console.Out ); writer.Formatting = Formatting.Indented; xDoc.WriteContentTo( writer ); writer.Flush(); Console.WriteLine(); writer.Close();
System.Xml.Xsl Namespace
Provides Some
of the classes:
XsltTransform: Transforms using a stylesheet XsltException: Used to handle transformation exceptions XsltContext: The XSLT processors execution context
XsltTransform
Four
Transformation By Example
Using System.Xml.Xsl; // 1. Create a XslTransform object XslTransform xslt = new XslTransform(); // 2. Load an XSL stylesheet xslt.Load("http://somewhere/favorite.xsl"); // 3 & 4. Load the XML data file & transform! xslt.Transform(http://somewhere/mydata.xml, C:\\somewhere_else\\TransformedXmlOutput.xml );
Summary
XML XML
is powerful, flexible, open & extensible is easy to learn easy to read & easy to use XML Schema and XSLT combine to let you
XML,
Have data with semantics Dictate and enforce you data structure Separate data and data representation Easily transform your data
.NET
Section 4: Q&A
Load an XML file using a parser Use the parsers programming interface to:
Navigate through the Document Object Model Manipulate the DOM: Add, Delete, Move, Modify DOM elements
Using Some
a DOM the parser can insure well formed documents parsers can validate the DOM Validating Parser
System.Xml
etc
.NET
http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/ http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/
XML Namespaces
Another
1/2
W3C specification
http://www.w3.org/TR/REC-xml-names/
Create Used
Multiple documents can use the same tag differently For example:
Document A may use <name/> to designate a persons name Document B may use <name/> to designate a file name
XML Namespaces
A
2/2
May
assign a namespace prefix to the namespace the prefix to differentiate elements & attributes have a default namespace
xmlns:ms=urn:schemas-microsoft-com:data
Use
Documents