copyright © software insight 2003 1 querying and transforming xml with.net

62
Copyright © Software Insight 2003 Copyright © Software Insight 2003 1 Querying and Transforming XML Querying and Transforming XML with .NET with .NET <BrianNoyes /> <BrianNoyes />

Upload: janis-hubbard

Post on 27-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

Copyright © Software Insight 2003Copyright © Software Insight 2003 11

Querying and Transforming XML Querying and Transforming XML with .NETwith .NET

<BrianNoyes /><BrianNoyes />

Page 2: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

22Copyright © Software Insight 2003Copyright © Software Insight 2003

Software ConsultantSoftware Consultant– 12+ Years programming, engineering, management experience12+ Years programming, engineering, management experience– C#, VB.NET, C++, JavaC#, VB.NET, C++, Java

Contributing Editor:Contributing Editor:– asp.netPROasp.netPRO– C#PROC#PRO– Visual Studio MagazineVisual Studio Magazine– .NET Magazine.NET Magazine

AuthoredAuthored– .NET and COM: Working Together (MightyWords press 2001).NET and COM: Working Together (MightyWords press 2001)– Teach Yourself DirectX 7 in 24 Hours, Sams Press (co-author)Teach Yourself DirectX 7 in 24 Hours, Sams Press (co-author)

Participates in the .NET design reviewsParticipates in the .NET design reviews

Speaker at conferences and user groupsSpeaker at conferences and user groups– INETA Speakers BureauINETA Speakers Bureau

Contact at Contact at www.idesign.netwww.idesign.net or [email protected] or [email protected]

About Brian NoyesAbout Brian Noyes

Page 3: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

33Copyright © Software Insight 2003Copyright © Software Insight 2003

GameplanGameplan

Quick Intro to XML TechnologiesQuick Intro to XML Technologies

Loading XMLLoading XML

Query DOM DocumentsQuery DOM Documents

Working with XPathDocumentsWorking with XPathDocuments

Transforming XML with XslTransformTransforming XML with XslTransform

Page 4: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

44Copyright © Software Insight 2003Copyright © Software Insight 2003

XML Technologies OverviewXML Technologies Overview

XML is the basis for many emerging technologies and XML is the basis for many emerging technologies and capabilitiescapabilities

Hype clouds the issue Hype clouds the issue – XML itself is very simpleXML itself is very simple– derivative technologies and use are where the complexity liesderivative technologies and use are where the complexity lies

XML forms the glue that allows systems to interoperateXML forms the glue that allows systems to interoperate

Page 5: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

55Copyright © Software Insight 2003Copyright © Software Insight 2003

XML UsesXML Uses

XML is used extensively forXML is used extensively for– Data definitionData definition– B2B communicationB2B communication– Messaging technologiesMessaging technologies– Structured storage of dataStructured storage of data

Page 6: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

66Copyright © Software Insight 2003Copyright © Software Insight 2003

XML Derivative TechnologiesXML Derivative Technologies

Many derivative technologies and grammars continue to Many derivative technologies and grammars continue to be defined and evolve:be defined and evolve:– XML Schema 1.0XML Schema 1.0– XPath 1.0XPath 1.0– XSLT 1.0XSLT 1.0– XPointerXPointer– XLinkXLink– SOAPSOAP– RSSRSS– WebDAVWebDAV– ……

Page 7: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

77Copyright © Software Insight 2003Copyright © Software Insight 2003

What is XML?What is XML?

eXtensible Markup LanguageeXtensible Markup Language

Simple syntax for dataSimple syntax for data– Self describing markupSelf describing markup– Strict but simple rules define a documentStrict but simple rules define a document– Human readable textHuman readable text– Derived from SGMLDerived from SGML

As is HTMLAs is HTML

Page 8: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

88Copyright © Software Insight 2003Copyright © Software Insight 2003

XML Syntax RulesXML Syntax Rules

XML Syntax RulesXML Syntax Rules– Case sensitiveCase sensitive– A document is composed of nodesA document is composed of nodes

Everything in a document is a nodeEverything in a document is a node

– A document has one and only one root nodeA document has one and only one root nodeThat root node is an elementThat root node is an element

– Elements have both start and end tagsElements have both start and end tags– Elements are properly nestedElements are properly nested– Elements can contain attributes, child elements, or textElements can contain attributes, child elements, or text– Special node definitions for specific purposes:Special node definitions for specific purposes:

Comments, CDATA, Processing Instructions, EntitiesComments, CDATA, Processing Instructions, Entities

Page 9: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

99Copyright © Software Insight 2003Copyright © Software Insight 2003

Sample XML DocumentSample XML Document

<?<?xmlxml versionversion="1.0"="1.0" encodingencoding="utf-8"="utf-8" ?>?>

<!<!-- The Music element is the root node ---- The Music element is the root node -->>

<<MusicMusic>>

<<ArtistArtist namename="Evanescense">="Evanescense">

<<AlbumAlbum namename="Fallen">="Fallen">

<<TrackTrack numbernumber="1">Going Under</="1">Going Under</TrackTrack>>

<<TrackTrack numbernumber="2">Bring Me To Life</="2">Bring Me To Life</TrackTrack>>

<<TrackTrack numbernumber="3">Everybody's Fool</="3">Everybody's Fool</TrackTrack>>

</</AlbumAlbum>>

</</ArtistArtist>>

<<ArtistArtist namename="Disturbed">="Disturbed">

<<AlbumAlbum namename="Believe">="Believe">

</</AlbumAlbum>>

</</ArtistArtist>>

</</MusicMusic>>

Page 10: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

1010Copyright © Software Insight 2003Copyright © Software Insight 2003

XML NamespacesXML Namespaces

Used to scope named objects in a documentUsed to scope named objects in a document

Similar to programming language namespacesSimilar to programming language namespaces

Consist of an arbitrary stringConsist of an arbitrary string

Should be unique to ensure no name clashes with other Should be unique to ensure no name clashes with other XML documentsXML documents– Two conventions exist for namespaces to try to ensure Two conventions exist for namespaces to try to ensure

uniqueness:uniqueness:URL: Used because domain names are guaranteed to be unique in URL: Used because domain names are guaranteed to be unique in the Internet spacethe Internet space

xmlns=“http://www.idesign.net/DataAccess”xmlns=“http://www.idesign.net/DataAccess”

URN: arbitrary naming with specific syntaxURN: arbitrary naming with specific syntax

xmlns=“urn:idesign-net”xmlns=“urn:idesign-net”

Page 11: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

1111Copyright © Software Insight 2003Copyright © Software Insight 2003

XML NamespacesXML Namespaces

Namespaces can be declared at document or element scopeNamespaces can be declared at document or element scope– Child nodes are scoped to the default namespace of their parent nodeChild nodes are scoped to the default namespace of their parent node

Prefixes are used to allow scoping to multiple namespaces with Prefixes are used to allow scoping to multiple namespaces with shorthand notationshorthand notation

<?xml version="1.0" encoding="utf-8" ?><Root xmlns="http://www.idesign.net/DataAccess" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:foo="urn:yadda-foobar-skwee"><!-- Unscoped elements are assumed to be defined in http://www.idesign.net/DataAccess namespace --> <GrandParent foo:myattribute="hello"> <Parent xmlns="urn:idesign-net" xmlns:yadda="urn:yadda"> <!-- Now unscoped elements are assumed to be defined in urn:idesign-net namespace --> <Child> <yadda:SomeElement>DeDoDoDoDeDaDaDa</yadda:SomeElement> </Child> </Parent> </GrandParent></Root>

Page 12: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

1212Copyright © Software Insight 2003Copyright © Software Insight 2003

Commonly Used XML Grammars in .NETCommonly Used XML Grammars in .NET

XML SchemasXML Schemas– Used to define the structure / type system of an XML documentUsed to define the structure / type system of an XML document

XPathXPath– Used for querying XML documentsUsed for querying XML documents

XSLTXSLT– Used to transform XML into other document formatsUsed to transform XML into other document formats

Page 13: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

1313Copyright © Software Insight 2003Copyright © Software Insight 2003

XML SchemasXML Schemas

XML Schemas define:XML Schemas define:– Allowable structure of an XML documentAllowable structure of an XML document

What elements and attributes are allowed where and in what orderWhat elements and attributes are allowed where and in what order

– The type system of the XML documentThe type system of the XML documentThe elements and attributes represent data, so the organization of The elements and attributes represent data, so the organization of those nodes and the kind of data contained in them defines the type those nodes and the kind of data contained in them defines the type system of the documentsystem of the document

XML Documents are instances of a particular schemaXML Documents are instances of a particular schema

Page 14: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

1414Copyright © Software Insight 2003Copyright © Software Insight 2003

XML SchemasXML Schemas

Three generations to contend with:Three generations to contend with:– Document Type Definitions (DTDs)Document Type Definitions (DTDs)– XML Data Reduced (XDR)XML Data Reduced (XDR)– XML Schema 1.0 Definition Language (XSD)XML Schema 1.0 Definition Language (XSD)

Page 15: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

1515Copyright © Software Insight 2003Copyright © Software Insight 2003

Document Type DefinitionsDocument Type Definitions

Structural definition of an XML documentStructural definition of an XML document

Not XML itselfNot XML itself

Limited type definition capabilityLimited type definition capability

Quickly becoming obsoleteQuickly becoming obsolete

<!DOCTYPE Music [ <!ELEMENT Music (Artist*)><!ELEMENT Artist (Album*)><!ATTLIST name CDATA #REQUIRED><!ELEMENT Album (Track*)><!ATTLIST name CDATA #REQUIRED><!ELEMENT Track (#PCDATA)> <!ATTLIST number CDATA “”>]>

Page 16: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

1616Copyright © Software Insight 2003Copyright © Software Insight 2003

XML Data Reduced (XDR)XML Data Reduced (XDR)

XML Schema Definition XML Schema Definition Language (XDR)Language (XDR)– Early attempt by Early attempt by

Microsoft to standardize Microsoft to standardize an XML-based schema an XML-based schema language based on the language based on the W3C Schema W3C Schema Recommendation before Recommendation before W3C approvalW3C approval

– Not as expressive as Not as expressive as XML Schema 1.0XML Schema 1.0

<?xml version="1.0" encoding="utf-8" ?> <Schema xmlns="urn:idesign.net-music" xmlns:dt="urn:schemas-microsoft-com:datatypes"> <AttributeType name="number" dt:type="int" /> <AttributeType name="name" dt:type="string" /> <ElementType name=“Track” content="mixed"> <attribute type="name" /> </ElementType> <ElementType name="Album"> <element type="Track"/> <attribute type="name" /> </ElementType> <ElementType name="Artist" > <element type="Album" /> <attribute type="name" /> </ElementType> <ElementType name=“Music model=“closed”> <element type="Artist" minOccurs="0" maxOccurs="*" /> </ElementType></Schema>

Page 17: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

1717Copyright © Software Insight 2003Copyright © Software Insight 2003

XML Schema 1.0 (XSD)XML Schema 1.0 (XSD)

The formal recommendation approved by W3CThe formal recommendation approved by W3C

Includes:Includes:– Primitive type definitionsPrimitive type definitions– Simple and complex type definitionsSimple and complex type definitions– Element and attribute declarationsElement and attribute declarations– Namespace scopingNamespace scoping– Inheritance, includes, importsInheritance, includes, imports

The default schema mechanism used by .NETThe default schema mechanism used by .NET

Page 18: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

1818Copyright © Software Insight 2003Copyright © Software Insight 2003

XML Schema 1.0 ExampleXML Schema 1.0 Example

Page 19: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

1919Copyright © Software Insight 2003Copyright © Software Insight 2003

XPath OverviewXPath Overview

Query Syntax for XMLQuery Syntax for XML– Can think of XPath as the SQL for XMLCan think of XPath as the SQL for XML– Provides common syntax and semantics for location Provides common syntax and semantics for location

specifications in a document for both XSLT and specifications in a document for both XSLT and XPointerXPointer

– Navigates the hierarchical structure of a documentNavigates the hierarchical structure of a document– Represents a document as a tree of nodesRepresents a document as a tree of nodes

Page 20: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

2020Copyright © Software Insight 2003Copyright © Software Insight 2003

XPath ExpressionsXPath Expressions

Evaluate to a node set, boolean, number, or stringEvaluate to a node set, boolean, number, or string

Expressions are evaluated based on a current context or Expressions are evaluated based on a current context or position within a documentposition within a document– Kind of like a cursor in a databaseKind of like a cursor in a database

Not XML themselvesNot XML themselves

Page 21: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

2121Copyright © Software Insight 2003Copyright © Software Insight 2003

XPath Expression CompositionXPath Expression Composition

XPath Expressions are composed of location stepsXPath Expressions are composed of location steps– Each location step hasEach location step has

Axis (optional/implied)Axis (optional/implied)

Node TestNode Test

PredicatePredicate

– Results from one location step are used as the context node for Results from one location step are used as the context node for the next location stepthe next location step

– Location steps are separated by slashesLocation steps are separated by slashes

locstep1/locstep2/locstep3/…locstep1/locstep2/locstep3/…

Page 22: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

2222Copyright © Software Insight 2003Copyright © Software Insight 2003

XPath Expression CompositionXPath Expression Composition

Axes to define a relative pathAxes to define a relative path– descendant::, child::, parent::, etc.descendant::, child::, parent::, etc.– Implicit child:: if not providedImplicit child:: if not provided– Shorthand operators for common axes:Shorthand operators for common axes:

/ - root/ - root// - descendant// - descendant. – current node. – current node.. – parent node.. – parent node

Node Test defines a node to match againstNode Test defines a node to match against– Element, attribute, built-in functionElement, attribute, built-in function

Predicate defines conditional logic to determine matchesPredicate defines conditional logic to determine matches– Can define multiple predicatesCan define multiple predicates

Implicit AND of separate predicatesImplicit AND of separate predicates

– Can define complex predicatesCan define complex predicatesBoolean logic, built-in functions, extension functionsBoolean logic, built-in functions, extension functions

Page 23: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

2323Copyright © Software Insight 2003Copyright © Software Insight 2003

XPath and NamespacesXPath and Namespaces

Nodes referred to in an XPath expression are frequently Nodes referred to in an XPath expression are frequently scoped to a namespacescoped to a namespace

Up to the processor to decide what to do about itUp to the processor to decide what to do about it– Usually will only match against properly scoped node names Usually will only match against properly scoped node names

with namespaceswith namespaces

Can include namespace prefixes in node names to Can include namespace prefixes in node names to scope to their namespacescope to their namespace– Processor has to be made aware of the namespace URI Processor has to be made aware of the namespace URI

associated with the prefix somehowassociated with the prefix somehowUse XmlNamespaceManager in .NET to create collection of Use XmlNamespaceManager in .NET to create collection of prefix/URI associations in the NameTable of the processor objectprefix/URI associations in the NameTable of the processor object

Page 24: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

2424Copyright © Software Insight 2003Copyright © Software Insight 2003

XPath ExamplesXPath Examples

Examples: Music/ArtistMusic/Artist/@name/Music/Artist/Album[@name=‘Fallen’]descendant::Album[@name='Fallen']descendant::Album/@name/Music/child::Artistdescendant::*[parent::Album]descendant::*[parent::Album][@number = 2]descendant::*[parent::Album][@number = 2][contains(text(),'Life')]descendant::*[parent::myns:Album][@number = 2]

Page 25: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

2525Copyright © Software Insight 2003Copyright © Software Insight 2003

XSLT OverviewXSLT Overview

Extensible Stylesheet Language TransformationsExtensible Stylesheet Language TransformationsDefines transformations for XSLDefines transformations for XSL– Conversions from one document structure to anotherConversions from one document structure to another– Can be used to output document formats suitable for presentation (i.e. Can be used to output document formats suitable for presentation (i.e.

HTML, RTF, PDF, etc.)HTML, RTF, PDF, etc.)

XSLT transforms source trees into result treesXSLT transforms source trees into result trees– Uses pattern associations specified in XPathUses pattern associations specified in XPath– Output determined by templatesOutput determined by templates

Fragments of markup that are output based on the matching patterns Fragments of markup that are output based on the matching patterns encountered in the source documentencountered in the source documentmatch attributes use XPath expressions to evaluate source document node match attributes use XPath expressions to evaluate source document node matchesmatches

– Based on the current context in the documentBased on the current context in the document– Selection paths are relative to the current nodeSelection paths are relative to the current node

Really a programming language unto itselfReally a programming language unto itself

Page 26: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

2626Copyright © Software Insight 2003Copyright © Software Insight 2003

XSLT ConstructsXSLT Constructs

XSLT instructionsXSLT instructions– Declare templatesDeclare templates– Apply templatesApply templates– Call templatesCall templates– Declare/pass arguments/parametersDeclare/pass arguments/parameters– Get the value of a matched nodeGet the value of a matched node– Copy a nodeCopy a node– Evaluate functionsEvaluate functions– Control program flowControl program flow

for-each, if, choose-when-otherwisefor-each, if, choose-when-otherwise

– Emit nodesEmit nodesElements, attributes, etc.Elements, attributes, etc.

Page 27: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

2727Copyright © Software Insight 2003Copyright © Software Insight 2003

XSLT ExampleXSLT Example<?xml version="1.0" encoding="UTF-8" ?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">

<html><body lang="EN-US">

<h1>Artists</h1><xsl:apply-templates select="Music/Artist" />

</body></html>

</xsl:template>

<xsl:template match="Artist"><li><xsl:apply-templates select="attribute::name" /></li>

</xsl:template>

<xsl:template match="name"><xsl:apply-templates />

</xsl:template></xsl:stylesheet>

Page 28: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

2828Copyright © Software Insight 2003Copyright © Software Insight 2003

Managing XML in .NETManaging XML in .NET

Lots of support for XML in .NETLots of support for XML in .NET– DOM SupportDOM Support

XmlDocumentXmlDocument

– Data-Oriented SupportData-Oriented SupportDataSet / XmlDataDocumentDataSet / XmlDataDocument

– XML Navigation SupportXML Navigation SupportXPathNavigator / XPathDocumentXPathNavigator / XPathDocument

– Streaming SupportStreaming SupportXmlReader / XmlWriterXmlReader / XmlWriter

– Serialization SupportSerialization SupportXmlSerializationXmlSerializationSOAP formatted object serializationSOAP formatted object serialization

– XML Web ServicesXML Web Services

Page 29: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

2929Copyright © Software Insight 2003Copyright © Software Insight 2003

Loading XMLLoading XML

Document Object Model (DOM) OrientedDocument Object Model (DOM) Oriented– XmlDocument.Load()XmlDocument.Load()

Load from disk or streamLoad from disk or stream

– XmlDocument.LoadXml()XmlDocument.LoadXml()Load from a string representation of XML documentLoad from a string representation of XML document

Data OrientedData Oriented– DataSet.ReadXml()DataSet.ReadXml()

Loads XML that is suitable for representation as a DataSetLoads XML that is suitable for representation as a DataSet

Will infer a schema if one is not presentWill infer a schema if one is not present

– XmlDataDocument constructorXmlDataDocument constructorTakes a DataSet and provides an XmlDocument derived classTakes a DataSet and provides an XmlDocument derived class

Page 30: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

3030Copyright © Software Insight 2003Copyright © Software Insight 2003

Loading XMLLoading XML

XPath OrientedXPath Oriented– XPathDocument() constructorXPathDocument() constructor

Load from stream, uri, readerLoad from stream, uri, reader

XSLT OrientedXSLT Oriented– XslTransform.Load()XslTransform.Load()

Load from URLLoad from URL

Stream OrientedStream Oriented– Stream + XmlTextReaderStream + XmlTextReader

Read only/forward only node access from a streamRead only/forward only node access from a stream

– Document + XmlNodeReaderDocument + XmlNodeReaderRead only/forward only node access from a documentRead only/forward only node access from a document

Page 31: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

3131Copyright © Software Insight 2003Copyright © Software Insight 2003

XmlResolverXmlResolver

Handles all aspects of resolving external entities referred Handles all aspects of resolving external entities referred to by an XML Documentto by an XML Document– DTDs, Entities, schemas, stylesheetsDTDs, Entities, schemas, stylesheets

Handles securityHandles security– Logs you into remote locationsLogs you into remote locations

Handles negotiating network protocols to access entitiesHandles negotiating network protocols to access entities

Optional argument to most XML load operationsOptional argument to most XML load operations

Page 32: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

3232Copyright © Software Insight 2003Copyright © Software Insight 2003

Performing DOM QueriesPerforming DOM Queries

XmlNode class methodsXmlNode class methods– SelectNodes()SelectNodes()– SelectSingleNode()SelectSingleNode()

XmlDocument class methodsXmlDocument class methods– GetElementsByTagName()GetElementsByTagName()– GetElementById()GetElementById()

Page 33: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

3333Copyright © Software Insight 2003Copyright © Software Insight 2003

XmlNode.SelectNodes()XmlNode.SelectNodes()

Easiest access queries when working with DOM Easiest access queries when working with DOM documentsdocuments

Takes an XPath expressionTakes an XPath expression

Optionally takes an XmlNamespaceManager to support Optionally takes an XmlNamespaceManager to support namespace prefixed node namesnamespace prefixed node names

Returns an XmlNodeListReturns an XmlNodeList– Iterate through the results and operate on them accordinglyIterate through the results and operate on them accordingly

Provides Read/Write access to nodes in the DOMProvides Read/Write access to nodes in the DOM

– Abstract base class – actually an XPathNodeList instanceAbstract base class – actually an XPathNodeList instance

Uses an XPathNavigator under the coversUses an XPathNavigator under the covers– Derived DocumentXPathNavigator objectDerived DocumentXPathNavigator object

Page 34: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

3434Copyright © Software Insight 2003Copyright © Software Insight 2003

XmlNode.SelectNodes()XmlNode.SelectNodes()

// Load the documentXmlDocument doc = new XmlDocument();doc.Load("C:\\demos\\MusicBase.xml");// Call SelectNodes with a valid XPath QueryXmlNodeList nodes = doc.SelectNodes( "descendant::Track[@number=2]/text()");foreach (XmlNode node in nodes){ Console.WriteLine(node.Value);}

Page 35: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

3535Copyright © Software Insight 2003Copyright © Software Insight 2003

XmlNode.SelectSingleNode()XmlNode.SelectSingleNode()

Used to obtain a single match for a queryUsed to obtain a single match for a query

Actually just calls SelectNodes and returns first node in Actually just calls SelectNodes and returns first node in listlist– Not very efficient, query could return a large result setNot very efficient, query could return a large result set– Just a convenience methodJust a convenience method

Improving performance: use smarter queriesImproving performance: use smarter queries– Construct an XPath query that will only return a single nodeConstruct an XPath query that will only return a single node

Smart knowledge of the target dataSmart knowledge of the target data

[postion() = 1] predicate[postion() = 1] predicate

Page 36: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

3636Copyright © Software Insight 2003Copyright © Software Insight 2003

XmlNode.SelectSingleNode()XmlNode.SelectSingleNode()

// Load the documentXmlDocument doc = new XmlDocument();doc.Load("C:\\demos\\MusicBase.xml");// Select a single nodeXmlNode dumb = doc.SelectSingleNode( "descendant::Track[@number=2]");XmlNode smart = doc.SelectSingleNode( "descendant::Track[@number=2][position()=1]");Console.WriteLine(dumb.InnerText);Console.WriteLine(smart.InnerText);

Page 37: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

3737Copyright © Software Insight 2003Copyright © Software Insight 2003

Other DOM Query OptionsOther DOM Query Options

XmlDocument.GetElementsByTagName()XmlDocument.GetElementsByTagName()– Convenience method for getting collections of elementsConvenience method for getting collections of elements– Uses internal XmlDocument maintained element tables to obtainUses internal XmlDocument maintained element tables to obtain– Does not allow you to control level of element selected in Does not allow you to control level of element selected in

heirarchyheirarchy

XmlDocument.GetElementByID()XmlDocument.GetElementByID()– Takes an ID value and returns the element whose ID attribute Takes an ID value and returns the element whose ID attribute

matchesmatches– Only works with DTD schema definition associated with Only works with DTD schema definition associated with

documentdocument– Not very usefulNot very useful

Page 38: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

3838Copyright © Software Insight 2003Copyright © Software Insight 2003

Beware of XmlNodeList.CountBeware of XmlNodeList.Count

XmlNodeList is an abstract base classXmlNodeList is an abstract base class

XPathNodeList is returned from SelectNodes()XPathNodeList is returned from SelectNodes()– Very lightweight list implementationVery lightweight list implementation

You may be tempted to check the Count property to see You may be tempted to check the Count property to see how many nodes you gothow many nodes you got– Triggers an extremely inefficient walk and test of every node in Triggers an extremely inefficient walk and test of every node in

the node set to calculate the countthe node set to calculate the count

Page 39: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

3939Copyright © Software Insight 2003Copyright © Software Insight 2003

XPathNodeList.CountXPathNodeList.Countpublic virtual int get_Count() {

if (!(this.done))this.ReadUntil(2147483647);

return this.list.Count;}

internal int ReadUntil(int index) {int local0;XmlNode local1;

local0 = this.list.Count;while (!(this.done) && index >= local0) {

if (this.iterator.MoveNext()) {local1 = this.GetNode(this.iterator.Current);if (local1 == null)

continue;this.list.Add(local1);local0++;continue;

}this.done = 1;break;

}return local0;

}

Page 40: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

4040Copyright © Software Insight 2003Copyright © Software Insight 2003

XmlDataDocumentXmlDataDocument

Bridges the XML and relational views of dataBridges the XML and relational views of data– Easy access to XML functionality on the dataEasy access to XML functionality on the data

Queries, transformation, validationQueries, transformation, validation

– Easy access to relational functionality with the dataEasy access to relational functionality with the data.NET Databinding.NET Databinding

Derives from XmlDocumentDerives from XmlDocument– ““Is a” XmlDocumentIs a” XmlDocument– Looks like a DataSet when needed through a propertyLooks like a DataSet when needed through a property

Everything covered about XmlDocument appliesEverything covered about XmlDocument applies– Its just a derived classIts just a derived class– Has a specialized XPathNavigator implementationHas a specialized XPathNavigator implementation

Page 41: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

4141Copyright © Software Insight 2003Copyright © Software Insight 2003

Enter the .NET XPath Processing EngineEnter the .NET XPath Processing Engine

.NET provides a new model for working with XML for .NET provides a new model for working with XML for navigation and queryingnavigation and querying

Other models good for what they were designed forOther models good for what they were designed for– DOM is heavyweight, read/write, random access, handles all DOM is heavyweight, read/write, random access, handles all

XML node typesXML node typesRich access to complex object modelRich access to complex object model

– XmlReader is lightweight, read-only, forward-only, handles all XmlReader is lightweight, read-only, forward-only, handles all XML Node TypesXML Node Types

Fast serialized access for getting data in memoryFast serialized access for getting data in memory

Need something that gives the best of both worldsNeed something that gives the best of both worlds

Page 42: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

4242Copyright © Software Insight 2003Copyright © Software Insight 2003

XPathNavigatorXPathNavigator

Abstract base class used to define XML navigation and Abstract base class used to define XML navigation and query API for XML in .NETquery API for XML in .NET– Document classes that support implement IXPathNavigableDocument classes that support implement IXPathNavigable

Lightweight object modelLightweight object model– Hierarchical tree basedHierarchical tree based– Restricted to just elements, attributes, namespacesRestricted to just elements, attributes, namespaces

Name and valueName and value

Read-OnlyRead-Only– Controlled through the API defined by the XPathNavigator Controlled through the API defined by the XPathNavigator

methodsmethods

Random-AccessRandom-Access– Can move forward or backward in document, traverse up or Can move forward or backward in document, traverse up or

down levels easilydown levels easily

Page 43: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

4343Copyright © Software Insight 2003Copyright © Software Insight 2003

XPathDocumentXPathDocument

Document class for loading XML into memory for an Document class for loading XML into memory for an XPathNavigator to operate againstXPathNavigator to operate against

Constructor takes stream, URL, or readerConstructor takes stream, URL, or reader

Loads XML into memory in a much lighter weight object Loads XML into memory in a much lighter weight object model than XmlDocumentmodel than XmlDocument

Used to construct an XPathNavigator through Used to construct an XPathNavigator through CreateNavigator() callCreateNavigator() call– Actually an XPathDocumentNavigator instanceActually an XPathDocumentNavigator instance

Page 44: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

4444Copyright © Software Insight 2003Copyright © Software Insight 2003

XPath ObjectsXPath Objects

Node List

...

XPathDocument

XPathNode

XPathNodeXPathNode

XPathNodeXPathNode

XPathNodeXPathNode

XPathNode

XPathNavigator

Doc

Current Node

XPathNodeIterator

Current Node

Node ref

Node ref

Node ref

Select

Page 45: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

4545Copyright © Software Insight 2003Copyright © Software Insight 2003

XPathNavigator MethodsXPathNavigator Methods

MoveXXX MethodsMoveXXX Methods– Allows forward/backward navigation amongst the nodesAllows forward/backward navigation amongst the nodes

SelectXXX MethodsSelectXXX Methods– Select() – primary XPath query evaluation method, returns iterator for Select() – primary XPath query evaluation method, returns iterator for

node set resultsnode set results– SelectAncenstors()/SelectChildren()/SelectDescendants()SelectAncenstors()/SelectChildren()/SelectDescendants()

Easier selection along particular axesEasier selection along particular axes

OthersOthers– Compile() – Pre-compile XPathExpression for rapid repeated executionCompile() – Pre-compile XPathExpression for rapid repeated execution– Evaluate() – Similar to Select(), but works for queries that do not return Evaluate() – Similar to Select(), but works for queries that do not return

node sets (string, bool, number)node sets (string, bool, number)– Clone() – Get a copy of the iterator for navigating child nodesClone() – Get a copy of the iterator for navigating child nodes– GetAttribute()/GetNamespace() – access node infoGetAttribute()/GetNamespace() – access node info– Positional checksPositional checks

Page 46: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

4646Copyright © Software Insight 2003Copyright © Software Insight 2003

XPathNavigator PropertiesXPathNavigator Properties

NodeTypeNodeType

Name – fully qualifiedName – fully qualified

LocalName – no namespaceLocalName – no namespace

NamespaceURINamespaceURI

ValueValue

Prefix – namespace prefixPrefix – namespace prefix

NameTableNameTable

HasAttributesHasAttributes

HasChildrenHasChildren

Etc.Etc.

Page 47: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

4747Copyright © Software Insight 2003Copyright © Software Insight 2003

XPathExpressionsXPathExpressions

Class to support declaring and compiling XPath QueriesClass to support declaring and compiling XPath Queries

Not created directly, returned from Not created directly, returned from XPathNavigator.Compile()XPathNavigator.Compile()

Only way to associate namespace information with a Only way to associate namespace information with a queryquery

Page 48: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

4848Copyright © Software Insight 2003Copyright © Software Insight 2003

XPathNodeIteratorsXPathNodeIterators

What gets returned from a call to Select()What gets returned from a call to Select()

Lightweight iterator on the resulting node-setLightweight iterator on the resulting node-set

Access the current node with Current propertyAccess the current node with Current property

Loop through with MoveNext()Loop through with MoveNext()

Count property is pre-calculatedCount property is pre-calculated

Page 49: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

4949Copyright © Software Insight 2003Copyright © Software Insight 2003

Querying with XPathNavigatorQuerying with XPathNavigator

Load the XML into a document classLoad the XML into a document class

Get an XPathNavigator for the documentGet an XPathNavigator for the document

Optionally compile an XPath expression for reuseOptionally compile an XPath expression for reuse

Call Select() to get an XPathNodeIteratorCall Select() to get an XPathNodeIterator

oror

Call Evaluate() to get a value backCall Evaluate() to get a value back– Returned as an object, must cast to the expected typeReturned as an object, must cast to the expected type– Can also return node set iterators this way tooCan also return node set iterators this way too

Page 50: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

5050Copyright © Software Insight 2003Copyright © Software Insight 2003

Simple XPathNavigator QuerySimple XPathNavigator Query

// Load the document objectXPathDocument doc = new XPathDocument("C:\\demos\\MusicBase.xml");// Get a navigator for the documentXPathNavigator nav = doc.CreateNavigator();// Perform the queryXPathNodeIterator iter = nav.Select("descendant::Track[@number=2]");// Move through the results with the iteratorwhile (iter.MoveNext()){ Console.WriteLine(iter.Current.Value);}

Page 51: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

5151Copyright © Software Insight 2003Copyright © Software Insight 2003

Working with NamespacesWorking with Namespaces

Create an XmlNamespaceManager using the Create an XmlNamespaceManager using the NameTable of the navigatorNameTable of the navigator

Add namespace prefix/URI combos Add namespace prefix/URI combos

Compile the XPath expressionCompile the XPath expression

Set the XPathExpression contextSet the XPathExpression context

Page 52: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

5252Copyright © Software Insight 2003Copyright © Software Insight 2003

Working with NamespaceWorking with Namespace

// Load the documentXPathDocument doc = new XPathDocument("C:\\demos\\MusicDefNs.xml");// Get a navigator for the documentXPathNavigator nav = doc.CreateNavigator();// Compile the query with namespace prefixesXPathExpression exp = nav.Compile("descendant::foo:Track[@number=2]");// Create the namespace managerXmlNamespaceManager mgr = new XmlNamespaceManager(nav.NameTable);// Add an alias for the default namespacemgr.AddNamespace("foo","urn:foo");// Set the context on the expressionexp.SetContext(mgr);// Perform the queryXPathNodeIterator iter = nav.Select(exp);// Loop through the resultswhile (iter.MoveNext()){ Console.WriteLine(iter.Current.Value);}

Page 53: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

5353Copyright © Software Insight 2003Copyright © Software Insight 2003

Advanced TopicsAdvanced Topics

Sorting the node setSorting the node set– Use the AddSort methods of the XPathExpression objectUse the AddSort methods of the XPathExpression object

Specify a sort order or a comparer objectSpecify a sort order or a comparer object

Creating custom navigator objectsCreating custom navigator objects– Can implement IXPathNavigable on any object that you want to Can implement IXPathNavigable on any object that you want to

expose with an XML representationexpose with an XML representation– Need to implement all the move/select/etc. functionality yourselfNeed to implement all the move/select/etc. functionality yourself

Page 54: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

5454Copyright © Software Insight 2003Copyright © Software Insight 2003

Transforming XMLTransforming XML

Simple conceptual processSimple conceptual process– Take one documentTake one document– Apply rules and matching to parts of the documentApply rules and matching to parts of the document– Produce a new document form that is equivalent or a subset or Produce a new document form that is equivalent or a subset or

different representation of the source documentdifferent representation of the source documentXML -> HTMLXML -> HTML

XML Schema A -> XML Schema BXML Schema A -> XML Schema B

XML -> RTF, PDF, text, etc.XML -> RTF, PDF, text, etc.

Page 55: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

5555Copyright © Software Insight 2003Copyright © Software Insight 2003

XslTransform ClassXslTransform Class

XslTransform class provides a powerful XSLT XslTransform class provides a powerful XSLT processing engine for .NETprocessing engine for .NET– Load from multiple kinds of sourcesLoad from multiple kinds of sources

XPath documents, XML readers, URL, FileXPath documents, XML readers, URL, File

– Transforms to multiple output targetsTransforms to multiple output targetsFile, URL, Stream, Text/XML WritersFile, URL, Stream, Text/XML Writers

– Can take argument listsCan take argument lists– Can process embedded scriptsCan process embedded scripts

C#, Visual Basic, JscriptC#, Visual Basic, Jscript

– Can pass managed objects to the template and allow their Can pass managed objects to the template and allow their methods to be calledmethods to be called

Page 56: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

5656Copyright © Software Insight 2003Copyright © Software Insight 2003

Simple XSLT TransformationsSimple XSLT Transformations

Load the XSLT file into an XslTransform instanceLoad the XSLT file into an XslTransform instance

Call Transform(), passing the source document and the Call Transform(), passing the source document and the output targetoutput target

// Load the transform documentXslTransform xslt = new XslTransform();xslt.Load("C:\\demos\\MusicTransform.xslt");// Tranform the source xslt.Transform("C:\\demos\\MusicBase.xml", "C:\\demos\\Music.html",new XmlUrlResolver());

Page 57: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

5757Copyright © Software Insight 2003Copyright © Software Insight 2003

Transforming with ArgumentsTransforming with Arguments

XSLT templates can take parameters that can be XSLT templates can take parameters that can be declared inline or can be passed in when the template is declared inline or can be passed in when the template is calledcalled

XslTransform supports passing in arguments from the XslTransform supports passing in arguments from the calling codecalling code– Must comply with the allowable XSLT types: bool, string, Must comply with the allowable XSLT types: bool, string,

number, XPathNodeIteratornumber, XPathNodeIterator

Process:Process:– Construct an XslArgumentListConstruct an XslArgumentList– Add parameters to it with the AddParams methodAdd parameters to it with the AddParams method– Associate it with the transform in the call to Transform()Associate it with the transform in the call to Transform()

Page 58: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

5858Copyright © Software Insight 2003Copyright © Software Insight 2003

Working with ArgumentsWorking with Arguments

// Load the transform documentXslTransform xslt = new XslTransform();xslt.Load("C:\\demos\\MusicTransform.xslt");// Construct the argument listXsltArgumentList xargs = new XsltArgumentList();xargs.AddParam("ShowTracks",String.Empty,false);// Construct the document to contain the sourceXPathDocument doc = new XPathDocument("C:\\demos\\Music.xml");// Construct the output streamFileStream fso = File.OpenWrite("C:\\demos\\Music.html");// Tranform the source xslt.Transform(doc,xargs,fso,new XmlUrlResolver());fso.Close();

Page 59: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

5959Copyright © Software Insight 2003Copyright © Software Insight 2003

XSLT ExtensibilityXSLT Extensibility

<xsl:eval ><xsl:eval >– Allows jscript or vbscipt to be declared as part of the templateAllows jscript or vbscipt to be declared as part of the template

<msxsl:script><msxsl:script>– Allows embedded .NET language codeAllows embedded .NET language code– C#, VB.NET, JscriptC#, VB.NET, Jscript

Extension ObjectsExtension Objects– Can call methods in managed objects from within the XSLT Can call methods in managed objects from within the XSLT

templatetemplate– Powerful mechanism for extensibilityPowerful mechanism for extensibility– Better maintainability and reuseBetter maintainability and reuse– All of .NET Framework capability available to youAll of .NET Framework capability available to you

Page 60: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

6060Copyright © Software Insight 2003Copyright © Software Insight 2003

Extension ObjectsExtension Objects

// Load the transform documentXslTransform xslt = new XslTransform();xslt.Load("C:\\demos\\ExtensionObject.xslt");// Create the extension objectDocGenerator dg = new DocGenerator();// Construct the argument listXsltArgumentList xargs = new XsltArgumentList();xargs.AddExtensionObject("urn:idesign-net", dg);// Construct the document to contain the sourceXPathDocument doc = new XPathDocument("C:\\demos\\Empty.xml");// Tranform the source xslt.Transform(doc,xargs,fso,new XmlUrlResolver());

Page 61: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

6161Copyright © Software Insight 2003Copyright © Software Insight 2003

Extension ObjectsExtension Objectspublic class DocGenerator{ public XPathNodeIterator GetDoc() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<Employees><Employee>Fred</Employee>” + “<Employee>Alberta</Employee></Employees>"); XPathNavigator nav = doc.CreateNavigator(); return nav.Select("*"); }} <xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:idesign="urn:idesign-net"><xsl:output method="text"/>

<xsl:template match="/"><xsl:apply-templates select="idesign:GetDoc()" />

</xsl:template><xsl:template match="Employees">We found an employees container node<xsl:apply-templates select="Employee" /></xsl:template><xsl:template match="Employee">Name: <xsl:value-of select="." /></xsl:template>

</xsl:stylesheet>

Page 62: Copyright © Software Insight 2003 1 Querying and Transforming XML with.NET

6262Copyright © Software Insight 2003Copyright © Software Insight 2003

SummarySummary

Prefer XPathDocument/XPathNavigator when you don’t need to Prefer XPathDocument/XPathNavigator when you don’t need to modify nodesmodify nodesUse Extension objects to offload transform computationUse Extension objects to offload transform computation

Resources:Resources:– Applied XML Programming for Microsoft .NET, Dino Esposito, Microsoft Applied XML Programming for Microsoft .NET, Dino Esposito, Microsoft

PressPress– Manipulate XML Data Easily with the XPath and XSLT APIs in Manipulate XML Data Easily with the XPath and XSLT APIs in

the .NET Framework, Dino Esposito, MSDN Magazine, July 2003the .NET Framework, Dino Esposito, MSDN Magazine, July 2003http://www.msdn.microsoft.com/msdnmag/issues/03/07/XPathandXSLT/defahttp://www.msdn.microsoft.com/msdnmag/issues/03/07/XPathandXSLT/default.aspxult.aspx

– XSL Transformations: XSLT Alleviates XML Schema Incompatibility XSL Transformations: XSLT Alleviates XML Schema Incompatibility Headaches, Don Box, Aaron Skonnard, John Lam, MSDN Magazine, Headaches, Don Box, Aaron Skonnard, John Lam, MSDN Magazine, August 2000August 2000

http://www.msdn.microsoft.com/msdnmag/issues/0800/XSLT/http://www.msdn.microsoft.com/msdnmag/issues/0800/XSLT/