63
Tool Development Chapter 04: XML Nick Prühs June 12, 2014

Tool Development 04 - XML

Embed Size (px)

DESCRIPTION

Chapter 04 of the lecture Tool Development taught at SAE Institute Hamburg. Introduction to XML, XPath and XSLT.

Citation preview

Page 1: Tool Development 04 - XML

Tool DevelopmentChapter 04: XML

Nick Prühs

June 12, 2014

Page 2: Tool Development 04 - XML

5 Minute Review Session

• Which different WPF data controls do you know?• What are the three fundamental operations provided

by streams?• Point out the difference between streams and files!• Which namespaces and classes does .NET provide for

stream and file handling?• What’s the point of closing a stream, and how do you

do that?• Which types of file access rights do you know?• Why and how are streams composited in .NET?• What are the most common I/O exceptions in .NET?

2 / 58

Page 3: Tool Development 04 - XML

Assignment Solution #3

DEMO

3 / 58

Page 4: Tool Development 04 - XML

Objectives

• To get an overview of the structure of XML documents in general

• To learn how to read and write XML documents in .NET

• To understand advanced concepts of XML processing such as XPath and XSLT

4 / 58

Page 5: Tool Development 04 - XML

XML Fundamentals

• Short for Extensible Markup Language

• Logical structuring of data• Domain-specific languages

• Content-oriented markup (in contrast to HTML)

• Self-descriptive structure• Tags

• XML Schema

• Structural variations (e.g. variable child node count)

5 / 58

Page 6: Tool Development 04 - XML

XML Benefits

• Human-readable

• Automated document validation

• Used in machine-machine communication (e.g. web services)

6 / 58

Page 7: Tool Development 04 - XML

XML Processing

7 / 58

Page 8: Tool Development 04 - XML

XML Document Example

XML (GraphML)

8 / 58

<?xml version="1.0" encoding="UTF-8"?>

<graphml xmlns="http://graphml.graphdrawing.org/xmlns"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">

<graph id="G" edgedefault="undirected">

<node id="n0"/>

<node id="n1"/>

<node id="n2"/>

<node id="n3"/>

<edge source="n0" target="n2"/>

<edge source="n1" target="n2"/>

<edge source="n2" target="n3"/>

</graph>

</graphml>

Page 9: Tool Development 04 - XML

XML Document Example

XML (Collada)

9 / 58

<node id="here">

<translate sid="trans">1.0 2.0 3.0</translate>

<rotate sid="rot">1.0 2.0 3.0 4.0</rotate>

<matrix sid="mat">

1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0

9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0

</matrix>

</node>

Page 10: Tool Development 04 - XML

XML Document Example

XML (XAML)

10 / 58

<Window x:Class="LevelEditor.View.NewMapWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="New Map" Height="300" Width="300">

<StackPanel><DockPanel>

<TextBlock DockPanel.Dock="Left" Width="75" Margin="5">Map Width:</TextBlock><TextBox Name="TextBoxMapWidth" Margin="5">32</TextBox>

</DockPanel><DockPanel>

<TextBlock DockPanel.Dock="Left" Width="75" Margin="5">Map Height:</TextBlock><TextBox Name="TextBoxMapHeight" Margin="5">32</TextBox>

</DockPanel><DockPanel>

<TextBlock DockPanel.Dock="Left" Width="75" Margin="5">Default Tile:</TextBlock><StackPanel Margin="5" Name="DefaultTilePanel" />

</DockPanel><Button Click="OnCreateMapClicked">Create New Map</Button>

</StackPanel></Window>

Page 11: Tool Development 04 - XML

XML Tree Structure

11 / 58

Window

StackPanel

DockPanel

TextBlock TextBox

DockPanel

TextBlock TextBox

DockPanel

TextBlock StackPanel

Button

Page 12: Tool Development 04 - XML

XML Node Types

• Elements

• Document Element

• Content

• Attributes

• Comment

• Namespaces

• Processing Instructions

12 / 58

Page 13: Tool Development 04 - XML

XML Elements

• Main parts of XML documents

• Boundaries defined by start and end tags

• Consist of name, attributes and content

• Can be nested• Root element is called document element

• Always exactly one document element

13 / 58

<graph id="G" edgedefault="undirected">

<node id="n0"/>

<node id="n1"/>

<edge source="n0" target="n1"/>

</graph>

Page 14: Tool Development 04 - XML

XML Content

• Text between start and end tags of an element

• Either simple, complex or mixed

14 / 58

<graph id="G" edgedefault="undirected">

<node id="n0"/>

<node id="n1"/>

<edge source="n0" target="n1"/>

</graph>

Page 15: Tool Development 04 - XML

XML Attributes

• Associate name-value pairs with elements

• May appear within start tags, only

• Order doesn’t matter

• Unique per element

15 / 58

<graph id="G" edgedefault="undirected">

<node id="n0"/>

<node id="n1"/>

<edge source="n0" target="n1"/>

</graph>

Page 16: Tool Development 04 - XML

XML Comments

• Not part of document data

• XML processors may (but don’t need to) retrieve text comments

• Double-hyphen (“- -”) must not occur within comments

16 / 58

<!-- Connect both nodes. -->

<edge source="n0" target="n1"/>

Page 17: Tool Development 04 - XML

XML Namespaces

• Used for distinguishing nodes with same names

• Bound to an URI by the xmlns attribute

17 / 58

<?xml version="1.0" encoding="UTF-8"?>

<graphml xmlns="http://graphml.graphdrawing.org/xmlns">

<graph id="G" edgedefault="undirected">

<node id="n0"/>

<node id="n1"/>

<edge source="n0" target="n1"/>

</graph>

</graphml>

Page 18: Tool Development 04 - XML

Writing XML in .NET

• Abstract base class XmlWriter• Non-cached

• Forward-only

• Write-only

• Writes to stream or file

• Verifies that the characters are legal XML characters and that element and attribute names are valid XML names

• Verifies that the XML document is well formed

18 / 58

Page 19: Tool Development 04 - XML

Creating an XmlWriter

• XmlWriter instances are created using the static Create method

• XmlWriterSettings class is used to specify the set of features you want to enable on the XmlWriter object

• XmlWriterSettings can be reused to create multiple writer objects

• Allows adding features to an existing writer• Create method can accept another XmlWriter object• Underlying XmlWriter object can be another XmlWriter

instance that you want to add additional features to

19 / 58

Page 20: Tool Development 04 - XML

Creating an XmlWriter

C#

20 / 58

XmlWriterSettings settings = new XmlWriterSettings

{

Indent = true,

IndentChars = "\t"

};

XmlWriter writer = XmlWriter.Create("books.xml", settings);

Page 21: Tool Development 04 - XML

XmlWriterSettings Defaults

Property Description Default Value

CheckCharacters Whether to do character checking.

true

Encoding Type of text encoding to use. Encoding.UTF8

Indent Whether to indent elements. false

IndentCharsCharacter string to use when indenting.

Two whitespaces

NewLineCharsCharacter string to use for line breaks.

\r\n

NewLineOnAttributesWhether to write attributes on a new line.

false

21 / 58

Page 22: Tool Development 04 - XML

Writing XMLwith the XmlWriter

22 / 58

Member Name Description

WriteElementString Writes an entire element node, including a string value.

WriteStartElement Writes the specified start tag.

WriteEndElement Closes one element and pops the corresponding namespace scope.

WriteElementString Writes an element containing a string value.

WriteValue

Takes a CLR object and converts the input value to the desired output type using the XML Schema definition language (XSD) data type conversion rules. If the CLR object is a list type, such as IEnumerable, IList, or ICollection, it is treated as an array of the value type.

WriteAttributeString Writes an attribute with the specified value.

WriteStartAttribute Writes the start of an attribute.

WriteEndAttribute Closes the previous WriteStartAttribute call.

WriteNode Copies everything from the source object to the current writer instance.

WriteAttributes Writes out all the attributes found at the current position in the XmlReader.

Page 23: Tool Development 04 - XML

Writing XML Elements

C#

23 / 58

XmlWriterSettings settings = new XmlWriterSettings

{

Indent = true

};

using (XmlWriter writer = XmlWriter.Create("books.xml", settings))

{

// Write XML data.

writer.WriteStartElement("book");

writer.WriteElementString("title", "Awesome book");

writer.WriteElementString("price", "19.95");

writer.WriteEndElement();

}

Page 24: Tool Development 04 - XML

Writing XML Attributes

C#

24 / 58

XmlWriterSettings settings = new XmlWriterSettings

{

Indent = true

};

using (XmlWriter writer = XmlWriter.Create("books.xml", settings))

{

// Write XML data.

writer.WriteStartElement("book");

// Write the genre attribute.

writer.WriteAttributeString("genre", "novel");

writer.WriteElementString("title", "Awesome book");

writer.WriteElementString("price", "19.95");

writer.WriteEndElement();

}

Page 25: Tool Development 04 - XML

Namespace Handling

• XmlWriter maintains a namespace stack corresponding to all the namespaces defined in the current namespace scope

• WriteElementString, WriteStartElement, WriteAttributeString and WriteStartAttributemethods have overloads that allow you to specify a namespace URI

25 / 58

Page 26: Tool Development 04 - XML

Namespace Handling

C#

26 / 58

XmlWriterSettings settings = new XmlWriterSettings{

Indent = true,IndentChars = "\t"

};

using (XmlWriter writer = XmlWriter.Create("books.xml", settings)){

// Write XML data.writer.WriteStartElement("book");

// Write the namespace declaration.writer.WriteAttributeString("xmlns", "bk", null, "http://www.example.com/book");

// Write the genre attribute.writer.WriteAttributeString("genre", "novel");

writer.WriteElementString("title", "Awesome book");writer.WriteElementString("price", "19.95");

writer.WriteEndElement();}

Page 27: Tool Development 04 - XML

Reading XML in .NET

• Abstract base class XmlReader• Non-cached

• Forward-only

• Read-only

• Reads from stream or file

• Verifies that the XML document is well formed

• Validates the data against a DTD or schema

27 / 58

Page 28: Tool Development 04 - XML

Creating an XmlReader

• XmlReader instances are created using the static Create method

• XmlReaderSettings class is used to specify the set of features you want to enable on the XmlReaderobject

• XmlReaderSettings can be reused to create multiple reader objects

• Allows adding features to an existing reader• Create method can accept another XmlReader object• Underlying XmlReader object can be another XmlReader

instance that you want to add additional features to

28 / 58

Page 29: Tool Development 04 - XML

Creating an XmlReader

C#

29 / 58

XmlReaderSettings settings = new XmlReaderSettings

{

IgnoreWhitespace = true,

IgnoreComments = true

};

XmlReader reader = XmlReader.Create("books.xml", settings);

Page 30: Tool Development 04 - XML

XmlReaderSettings Defaults

Property Description Default Value

CheckCharacters Whether to do character checking.

true

IgnoreComments Whether to ignore comments. false

IgnoreProcessingInstructions Whether to ignore processing instructions.

false

IgnoreWhitespace Whether to ignore insignificant white space.

false

Schemas XmlSchemaSet to use when performing schema validation.

Empty XmlSchemaSet

ValidationType Whether to perform validation or type assignment when reading.

ValidationType.None

30 / 58

Page 31: Tool Development 04 - XML

Working with XmlReader

• Current node refers to the node on which the reader is positioned

• Move through the data and read the contents of a node

• Reader is advanced using any of the Read methods

31 / 58

Page 32: Tool Development 04 - XML

Reading XML Elements

32 / 58

Member Name Description

IsStartElement Checks if the current node is a start tag or an empty element tag.

ReadStartElement Checks that the current node is an element and advances the reader to the next node.

ReadEndElement Checks that the current node is an end tag and advances the reader to the next node.

ReadElementString Reads a text-only element.

ReadToDescendant Advances the XmlReader to the next descendant element with the specified name.

ReadToNextSibling Advances the XmlReader to the next sibling element with the specified name.

IsEmptyElement Checks if the current element has an empty element tag.

After the XmlReader is positioned on an element, the node properties, such as Name, reflect the element values.

Page 33: Tool Development 04 - XML

Reading XML Elements

C#

33 / 58

using (XmlReader reader = XmlReader.Create("books.xml"))

{

reader.Read();

reader.ReadStartElement("book");

reader.ReadStartElement("title");

Console.Write("The content of the title element: ");

Console.WriteLine(reader.ReadString());

reader.ReadEndElement();

reader.ReadStartElement("price");

Console.Write("The content of the price element: ");

Console.WriteLine(reader.ReadString());

reader.ReadEndElement();

reader.ReadEndElement();

}

Page 34: Tool Development 04 - XML

Reading XML Attributes

34 / 58

After MoveToAttribute has been called, the node properties, such as Name, reflect the properties of that attribute, and not the containing element it belongs to.

Member Name Description

AttributeCount Gets the number of attributes on the element.

GetAttribute Gets the value of the attribute.

HasAttributes Gets a value indicating whether the current node has any attributes.

Item Gets the value of the specified attribute.

MoveToAttribute Moves to the specified attribute.

MoveToElement Moves to the element that owns the current attribute node.

MoveToFirstAttribute Moves to the first attribute.

MoveToNextAttribute Moves to the next attribute.

Page 35: Tool Development 04 - XML

Reading XML Attributes

C#

35 / 58

XmlReaderSettings settings = new XmlReaderSettings{

IgnoreWhitespace = true};

using (XmlReader reader = XmlReader.Create("books.xml", settings)){

while (reader.Read()){

if (reader.HasAttributes){

Console.WriteLine("Attributes of <" + reader.Name + ">");

while (reader.MoveToNextAttribute()){

Console.WriteLine(" {0}={1}", reader.Name, reader.Value);}

// Move the reader back to the element node.reader.MoveToElement();

}}

}

Page 36: Tool Development 04 - XML

Reading Typed Data

• XmlReader class permits callers to read XML data and return values as simple-typed CLR values rather than strings

• Gets values in the representation that is most appropriate for the coding job without having to manually perform value conversions and parsing• ReadContentAsBoolean, ReadContentAsDateTime, ReadContentAsDouble, ReadContentAsLong, ReadContentAsInt, and ReadContentAsString methods are used to return a specific CLR object

• ReadElementContentAs method is used to read element content and return an object of the type specified

36 / 58

Page 37: Tool Development 04 - XML

DOM vs. SAX Parsing

• Simple API for XML (SAX )• Sequential access

• Required memory is proportional to maximum depth of the input document

• Document validation requires keeping track of id attributes, open elements, etc.

• Document Object Model (DOM)• Operates on the document as a whole

• Required memory is proportional to the entire document length

37 / 58

Page 38: Tool Development 04 - XML

Working with XmlDocument

• In-memory tree representation of an XML document

• Enables navigation and editing of parsed documents, including adding and removing nodes

• XmlNode object is the basic object in the DOM tree

38 / 58

Page 39: Tool Development 04 - XML

Working with XmlDocument

C#

39 / 58

// Load XML document.

XmlDocument document = new XmlDocument();

document.Load("books.xml");

// Find book element.

XmlNode bookNode = document["book"];

// Add author element.

XmlElement element = document.CreateElement("author");

XmlText text = document.CreateTextNode("Nick Prühs");

bookNode.AppendChild(element);

element.AppendChild(text);

// Write document to console.

XmlWriter writer = new XmlTextWriter(Console.Out);

document.WriteTo(writer);

Page 40: Tool Development 04 - XML

Navigating XML Documents

• XML Path Language (XPath) selects nodes from an XML document

• Each expression is evaluated with respect to a context node (e.g. “child”)

• Location path consists of a sequence of location steps

40 / 58

Page 41: Tool Development 04 - XML

XPath Location Steps

• Axis• Specifies the tree relationship between the nodes

selected by the location step and the context node

• Node test• Specifies the node type and expanded-name of the

nodes selected by the location step

• Predicate(s)• Further refine the set of nodes selected by the location

step

41 / 58

Page 42: Tool Development 04 - XML

XPath AxesAxis

Ancestor Selects all ancestors (parent, grandparent, etc.) of the current node.

Ancestor-or-self Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself.

Attribute Selects all attributes of the current node.

Child Selects all children of the current node.

Descendant Selects all descendants (children, grandchildren, etc.) of the current node.

Descendant-or-self Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself.

42 / 58

Page 43: Tool Development 04 - XML

XPath AxesAxis

Following Selects everything in the document after the closing tag of the current node.

Following-sibling Selects all siblings after the current node.

Namespace Selects all namespace nodes of the current node

Parent Selects the parent of the current node.

Preceding Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes.

Preceding-sibling Selects all siblings before the current node.

Self Selects the current node.

43 / 58

Page 44: Tool Development 04 - XML

XPath Node Tests

• node() is true for any node of any type• * is true for any node of the principal node type.

• child::* will select all element children of the context node

• attribute::* will select all attributes of the context node

• comment() is true for any comment node

• text() is true for any text node

• processing-instruction() is true for any processing instruction

44 / 58

Page 45: Tool Development 04 - XML

XPath Predicates

Restrict a node-set to select only those nodes for which some condition is true.

Example:

book[@genre=‘scifi’]

45 / 58

Page 46: Tool Development 04 - XML

XPath Location Paths

Location step syntax:

axis::nodetest[predicate]

Example:

/books/book/attribute::*

/books/book[@genre=‘scifi’]

46 / 58

Page 47: Tool Development 04 - XML

XSLT Fundamentals

• Short for Extensible Stylesheet Language Transformation

• Transforms XML documents into other formats such as other XML documents, HTML or plain text

• Turing-complete

47 / 58

Page 48: Tool Development 04 - XML

XSLT Motivation

48 / 58

Page 49: Tool Development 04 - XML

XSLT Processing

1. Read stylesheet file.

2. Build source tree from input XML document.

3. For each node in the source tree:1. Process source tree node.

2. Find best-matching template in the stylesheet.

3. Evaluate the template contents.

4. Create node(s) in the result tree.

49 / 58

Page 50: Tool Development 04 - XML

Common XSLT Elements

Node Description

xsl:apply-templates Specifies that other matches may exist within that node. If “select” is specified, only the templates that specify a match that fits the selected node or attribute type will be applied.

xsl:choose Contains xsl:when blocks and up to one xsl:otherwise block.

xsl:for-each Creates a loop which repeats for every match.

xsl:stylesheet Top-level element. Occurs only once in a stylesheet document.

xsl:template Specifies processing templates.

xsl:value-of Outputs a variable.

50 / 58

Page 51: Tool Development 04 - XML

XSLT Example

XML

51 / 58

<?xml version="1.0" encoding="utf-8"?>

<?xml-stylesheet href="books.xsl" type="text/xsl" ?>

<books>

<book>

<title>Awesome book</title>

<price>19.95</price>

</book>

<book>

<title>Another book</title>

<price>9.95</price>

</book>

</books>

Page 52: Tool Development 04 - XML

XSLT Example

XSLT

52 / 58

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet

version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

Page 53: Tool Development 04 - XML

XSLT Example

XSLT

53 / 58

<xsl:template match="/books"><html><head>

<title>Transforming books.xml with XSLT</title></head>

<body><h1>Books</h1><table>

<tr><th>Title</th><th>Price</th>

</tr>

<xsl:apply-templates select="book"><xsl:sort select="title" />

</xsl:apply-templates></table>

</body></html>

</xsl:template>

Page 54: Tool Development 04 - XML

XSLT Example

XSLT

54 / 58

<xsl:template match="book">

<tr>

<td>

<xsl:value-of select="title"/>

</td>

<td>

<xsl:value-of select="price"/>

</td>

</tr>

</xsl:template>

Page 55: Tool Development 04 - XML

XSLT Example

Rendered HTML

55 / 58

Page 56: Tool Development 04 - XML

OpenFileDialog Control

Common dialog box that allows a user to specify a filename for one or more files to open.

Rendered View

56 / 58

Page 57: Tool Development 04 - XML

OpenFileDialog Control

C#

57 / 58

// Show open file dialog box.OpenFileDialog openFileDialog = new OpenFileDialog

{AddExtension = true, CheckFileExists = true, CheckPathExists = true, DefaultExt = ".txt",Filter = "Text files (.txt)|*.txt", ValidateNames = true

};

var result = openFileDialog.ShowDialog();

if (result != true){

return;}

// Open text file.using (var stream = openFileDialog.OpenFile()){

// Do something.}

Page 58: Tool Development 04 - XML

SaveFileDialog Control

Common dialog that allows the user to specify a filename to save a file as.

Rendered View

58 / 58

Page 59: Tool Development 04 - XML

SaveFileDialog Control

C#

59 / 58

// Show save file dialog box.SaveFileDialog saveFileDialog = new SaveFileDialog

{AddExtension = true, CheckPathExists = true,DefaultExt = ".txt",FileName = "TextFile",Filter = "Text files (.txt)|*.txt",ValidateNames = true

};

var result = saveFileDialog.ShowDialog();

if (result != true){

return;}

// Open file stream.using (var stream = saveFileDialog.OpenFile()){

// Do something.}

Page 60: Tool Development 04 - XML

Assignment #4

1. Save Map

1. Add a new MenuItem “Save As” to your MainWindow, along with a ToolBar button with tooltip.

2. Allow saving a map file by showing a Save File Dialog when the Save As command is executed.

1. Map files should be well-formed, valid XML files.

2. Each map file should contain the width and height of the map, as well as the types of all map tiles.

3. It should not be possible to save a map if there is none.

60 / 58

Page 61: Tool Development 04 - XML

Assignment #4

2. Load Map

1. Add a new MenuItem “Open” to your MainWindow, along with a ToolBar button with tooltip.

2. Add separators to both your toolbar and menu.

3. Allow loading a map file by showing an Open File Dialog when the Open command is executed.

4. Opening a corrupt or invalid XML file should result in an error message. The current map should only be discarded of the map file could be successfully opened.

61 / 58

Page 62: Tool Development 04 - XML

References

• MSDN. XML Documents and Data. http://msdn.microsoft.com/en-us/library/2bcctyt8%28v=vs.110%29.aspx, June 2014.

• Luttenberger. Internet Applications – Web Services Primer. Department for Computer Science, CAU Kiel, 2008.

• Brandes, Eiglsperger, Lerner. GraphML Primer. http://graphml.graphdrawing.org/primer/graphml-primer.html, June 2014.

• Barnes, Finch. COLLADA – Digital Asset Schema Release 1.5.0 Specification. Sony Computer Entertainment Inc., April 2008.

• Clark, DeRose. XML Path Language (XPath). http://www.w3.org/TR/xpath/, November 16, 1999.

• w3schools.com. XPath Axes. http://www.w3schools.com/xpath/xpath_axes.asp, June 2014.

62 / 58

Page 63: Tool Development 04 - XML

Thank you for your attention!

Contact

Mail

[email protected]

Blog

http://www.npruehs.de

Twitter

@npruehs

Github

https://github.com/npruehs

63 / 58