Extending System.Xml Ted Neward

Slides:



Advertisements
Similar presentations
Chungnam National University DataBase System Lab
Advertisements

Programação com XML e XSLT. 1 Web Control XML – System.Web.UI.WebControls.XML Use the Xml control to display the contents of an XML document without formatting.
Inside an XSLT Processor Michael Kay, ICL 19 May 2000.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
XML: Extensible Markup Language
XML & Data Structures for the Internet Yingcai Xiao.
Chapter 13 XML Yingcai Xiao. What is XML? What is it for? Examples How to write? How to validate? How to read? How to display? How to format? How to translate?
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
Implementation of One Stop Search by XSLT By Dave Low University of Hong Kong 9-Dec-2003.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
XSL Transformations Transforming XML document into other (XML) documents 1XSL Transformations.
Technical Track Session XML Techie Tools Tim Bornholt.
C# The new language for Updated by Pavel Ježek © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
PHP and XML TP2653 Advance Web Programming. PHP and XML PHP5 – XML-based extensions, library and functionalities (current XAMPP PHP version is )
.NET and XML (or XML in.NET) David Oguns Matt Harding.
XML.NET Concepts and Implementation Badar Gillani.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
XML Fundementals XML vs.. HTML XML vs.. HTML XML Document (elements vs. attributes) XML Document (elements vs. attributes) XML and RDBMS XML and RDBMS.
Sheet 1XML Technology in E-Commerce 2001Lecture 6 XML Technology in E-Commerce Lecture 6 XPointer, XSLT.
XML – Extensible Markup Language XML eXtensible – add to language. Markup – delimit info using tags. Language – a way to express info.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
XML and XPath. Web Services: XML+XPath2 EXtensible Markup Language (XML) a W3C standard to complement HTML A markup language much like HTML origins: structured.
Presentation XML. NET SEMINAR By: Siddhant Ahuja (SID)
XML and its applications: 4. Processing XML using PHP.
ASP.NET and XML Presented By: Shravan S. Mylavarapu 1.
Copyright © Software Insight Querying and Transforming XML with.NET.
CIS 451: XSL Dr. Ralph Westfall February, Problems With XML no formatting capabilities contra formatting tags like, etc. in HTML CSS can be used.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
LINQ TO XML Mike Taulty Developer & Platform Group Microsoft UK
XML DOM Functionality in.NET DSK Chakravarthy
Programming Pillars Introduction to Object- Oriented Programming.
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
1 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XML and the.NET framework Heavily inspired by: Support WebCast: Programming XML in the Microsoft.NET Framework.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Transforming Documents „a how-to of transforming xml documents“ Lecture on Walter Kriha.
Лектор: Борислава Палева. Galin Iliev MCT, MCPD, MCSD.NET LINQ to XML.
WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms.
DAT 379 XML Today And Tomorrow Mark Fussell Lead Program Manager Microsoft Corporation.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 Introduction  Extensible Markup Language (XML) –Uses tags to describe the structure of a document –Simplifies the process of sharing information –Extensible.
Declaratively Producing Data Mash-ups Sudarshan Murthy 1, David Maier 2 1 Applied Research, Wipro Technologies 2 Department of Computer Science, Portland.
Core Indigo Patterns Ted Neward
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
XML eXtensible Markup Language. XML A method of defining a format for exchanging documents and data. –Allows one to define a dialect of XML –A library.
May 24, 2004IVOA Interop Meeting1 An AXIS-based Java SkyNode Ramon Williamson NCSA T HE US N ATIONAL V IRTUAL O BSERVATORY.
Dom and XSLT Dom – document object model DOM – collection of nodes in a tree.
Web Technologies for Bioinformatics Ken Baclawski.
.NET and XML (or XML in.NET) Matt Harding David Oguns.
Representing data with XML SE-2030 Dr. Mark L. Hornick 1.
 XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents.
Effective.NET Ted Neward Neward & Associates
Martin Kruliš by Martin Kruliš (v1.1)1.
C Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Introduction to XML Standards.
Open XML Developer Workshop XSLT and Open XML. Open XML Developer Workshop Disclaimer The information contained in this slide deck represents the current.
XSLT, XML Schema, and XPath Matt McClelland. Introduction XML Schema ▫Defines the content and structure of XML data. XSLT ▫Used to transform XML documents.
I Copyright © 2004, Oracle. All rights reserved. Introduction.
XSLT ‘Extreme’ TLA327 Dave McMahon MCAD,MCDBA,MVP – Connected Systems Developer.
XML for .NET Session 1 Introduction to XML Introduction to XSLT
XML: Extensible Markup Language
Fast Train Computer School
Displaying XML Data with XSLT
XML in Web Technologies
More Sample XML By Sadia Anjum.
XML Problems and Solutions
Chapter 13 XML Yingcai Xiao.
Querying XML XSLT.
XML and its applications: 4. Processing XML using PHP
Plug-In Architecture Pattern
Presentation transcript:

Extending System.Xml Ted Neward

XML in.NET.NET has tremendous XML support “out of the box”  Both Infoset-based and stream-based parser support  XPath navigation across Infosets  Schema-to-object support  XML Serialization  … and so on

XML in.NET Despite this, sometimes it’s just not enough  Working with XML in a strongly-typed language is awkward  XSLT doesn’t have all the behavior we want  We need to “reach out” from an XSLT to someplace else (database?)  We want to “extend” XPath to other hierarchical storage (Registry? filesystem? database? XML database?) Fortunately, System.Xml provides for this

Extending System.Xml The.NET XML classes are highly extensible  We’ll look at four ways to extend the XML support in.NET  Extend XmlDocument and friends  Extend XmlPathNavigator to navigate across other hierarchies  Extend XmlReader/XmlWriter to produce/consume XML  Extend XslTransform to include custom behavior

Extending XmlDocument XmlDocument is a concrete, non-sealed class  LoadXml() parses XML into Infoset form  Uses various CreateXXX() methods to create the Infoset tree  Some methods are marked virtual for easy overriding  Allow for creation of objects which can plug into Infoset tree

Extending XmlDocument Start with your basic Person public class Person { public string FirstName { get { return “Fred”; } } public string LastName { get { return “Wesley”; } } public override string ToString() { return FirstName + " " + LastName; }

Extending XmlDocument We want to create Persons from XML static void Main(string[] args) { XmlDocument doc = new XmlDocument(); string xml = " ” + “ Fred ” + “ Wesley ” + “ “; doc.LoadXml(xml); Person p = // ??? }

Extending XmlDocument Solution: Person IS-A XmlElement... class Person : XmlElement { internal Person(string prefix, string lname, string nsuri, XmlDocument doc) : base(prefix, lname, nsuri, doc) { } public string FirstName { get { return this["firstName"].InnerText; } } public string LastName { get { return this["lastName"].InnerText; } } public override string ToString() { return FirstName + " " + LastName; }

Extending XmlDocument... and we use a custom XmlDocument to create them class PersonDocument : XmlDocument { public override XmlElement CreateElement(string prefix, string lname, string nsuri) { if (lname=="person") return new Person(prefix, lname, nsuri, this); else return base.CreateElement(prefix, lname, nsuri); }

Extending XmlDocument... and now we can create Persons from XML static void Main() { string XML = // as before PersonDocument pdoc = new PersonDocument(); pdoc.LoadXml(XML); Person p = (Person)pdoc.DocumentElement; Console.WriteLine(p); }

Extending XmlDocument So what?  We can already do XML-to-object-to-XML mappings with xsd.exe  Why bother overriding XmlDocument?  No schema definition required  Flexible creation: what if we want to change types based on the XML?  We want to work with both XML and type representations at once  Allows for objects to be extensible; unrecognized elements (age? SSN?) are still part of Person, just not captured strongly  Requires extending XmlElement (implementation inheritance)

Extending XPathNavigator XPathNavigator: abstract class providing XPath cursor  Provides ability to execute XPath queries on hierarchical storage  XPathDocument provides XPathNavigator for XmlDocument objects  Extend XPathNavigator and override as necessary to provide customization  (See Aaron Skonnard’s “XML Files” article in MSDN for examples) public static void Main() { XPathDocument doc = new XPathDocument("books.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator ni = nav.Select("/bookstore/book/title"); while (ni.MoveNext()) Console.WriteLine(ni.Current.Value); }

Extending XPathNavigator So what?  XPath provides powerful hierarchical query API  Extend that to other hierarchical storage systems for ease-of-use  Because XslTransform takes XPathNavigator as source argument, use custom XPathNavigator to do transforms on non-XML sources

Extending XmlReader/XmlWriter XmlReader and XmlWriter  “Source” and “sink” for XML, respectively  Abstract base classes with numerous abstract methods XmlTextReader and XmlTextWriter  Derivatives of XmlReader and XmlWriter, respectively  Specifically deal with producing/consuming XML from text streams  Useful as templates for creating customized reader/writer classes

Extending XmlReader/XmlWriter So what?  We have XmlTextReader/XmlTextWriter, what else do we need?  XML may come in forms other than plain text  encrypted  compressed  XML may come from other sources than files  Fixed-length flat files  CSV files  XML could be processed entirely in-proc: no storage whatsoever

Extending XslTransform XslTransform does XSLT processing programmatically  Create an XslTransform object  Call Transform(), passing in source and output: public static void Main() { XslTransform xslt = new XslTransform(); XmlUrlResolver resolver = new XmlUrlResolver(); resolver.Credentials = new NetworkCredential("username","password","domain"); xslt.Load(“stylesheet.xsl", resolver); xslt.Transform(new XPathDocument("test.xml"), null, new XmlTextWriter(Console.Out), resolver); }

Extending XslTransform Two ways to add behavior to XSLT processing:  Add extensions as “script” within XSLT stylesheet itself  Add objects to XSLT arguments passed into Transform()

Extending XslTransform Add script extensions to stylesheet <xsl:stylesheet version=“1.0” xmlns:xsl=“...” xmlns:msxsl=“urn:schemas-microsoft-com:xslt” xmlns:ted=“ public string Concat(string lhs, string rhs) { return lhs + “ “ + rhs; }

Extending XslTransform Add objects to argument list to Transform()  First, create an “extension object”: methods will be called from XSLT public class Extension { public string Concat(string s1, string s2) { return s1 + " " + s2; } }

Extending XslTransform Add objects to argument list to Transform()  Add extension objects to XsltArgumentList and pass to Transform() public static void Main() { XslTransform xslt = new XslTransform(); XsltArgumentList args = new XsltArgumentList(); Extension obj = new Extension(); args.AddExtensionObject(" obj); xslt.Load("extended.xsl", resolver); xslt.Transform(new XPathDocument("test.xml"), args, new XmlTextWriter(Console.Out), resolver); }

Extending XslTransform Add objects to argument list to Transform()  Use the called method in the stylesheet <xsl:stylesheet version="1.0“ xmlns:xsl=" xmlns:ted="

Extending XslTransform So what?  XSLT has a lot of built-in behavior; what more do we need?  Extensions can provide additional “reach” to XSLT  database  network (FTP, HTTP, WebServices/SOAP,...)  other web services  Extensions can also provide similar-yet-different behavior  Such as concat-with-whitespace, concat-without-whitespace, etc.  Best of both worlds: XSLT + the.NET FCL

Summary Extending System.Xml is a lot easier than you might think Look for ways to use this flexibility  Custom XmlDocument types to provide type-safety and Infoset APIs  Custom XPathNavigators to allow for easy access and XSLT transformation  Custom XML sources and sinks  Custom behavior in XSL transformations

Questions ?

Credentials Who is this guy?  Independent consultant  Author  C# in a Nutshell (O’Reilly, with Drayton, Albahari, 2001)  Server-Based Java Programming (Manning, 2000)  SSCLI Essentials (O’Reilly, with Stutz, Shilling, 2003)  Effective Enterprise Java (Addison-Wesley, 3Q 2003)  Papers at  Blog at