41
Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop Custom XML Integrating data and documents

Embed Size (px)

Citation preview

Page 1: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Custom XMLCustom XMLIntegrating data and documents

Page 2: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

DisclaimerDisclaimerThe information contained in this slide deck represents the current view of Microsoft Corporation on the issues discussed as of the date of

publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication.

This slide deck is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT.

Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this slide deck may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this slide deck. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this slide deck does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo, person, place or event is intended or should be inferred.

© 2006 Microsoft Corporation. All rights reserved.Microsoft, 2007 Microsoft Office System, .NET Framework 3.0, Visual Studio, and Windows Vista are either registered trademarks or

trademarks of Microsoft Corporation in the United States and/or other countries.The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

Page 3: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Why Custom XML?Why Custom XML?

Enables interoperability with other systemsDocuments can provide a rich view of back-end data sourcesDocuments can update back-end data sources

Exposes business data in Open XML documentsHeterogenous systems can easily read data from documentsBusiness-specific semantics can be applied to document data

Separates presentation and dataSimplified programming model for all of the above

Custom XML schema support was a key design objective for Open XML: any schema can be used in Open XML documents.

Page 4: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Custom XML: Developer OptionsCustom XML: Developer OptionsTechnology WML SML PML

Custom XML Parts• Format-agnostic, very clean (only your tags)•May be non-visible

YES YES YES

Structured Document Tags (Content Controls)• Bound to custom XML parts or metadata

YES

CustomXML Markup• Tag block-level content with custom schema• Also available in Word 2003

YES

Custom XML Mapping• WordprocessingML: map content controls to custom Xml parts• SpreadsheetML: map cells to nodes in an external XML file

YES YES

Smart Tags• Simple, familiar, was available in Word 2003

YES YES YES

Page 5: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

CUSTOM XML PARTSCUSTOM XML PARTS

Page 6: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Custom XML PartsCustom XML Parts

Pure custom data: only your tags appear in the partFormat-agnostic

WordprocessingML: relationship from main documentSpreadsheetML: relationship from workbookPresentationML: relationship from presentation or slide

Explicit relationship from a shape on a slide

No content restrictions: any schema (or none)Syntactical restriction: must contain well-formed XML

Open XML Document

Document body – Open XML

Page 7: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Custom XML Part: Properties/MetadataCustom XML Part: Properties/Metadata

Information about a custom XML part is stored in a custom XML properties partStored via an implicit customXmlProps relationship from the custom XML part

Contains two types of information:Part ID

Uniquely identifies a part within a documentMaintained through editing sessionsOffice uses GUIDs, you can use any type of unique identifier

XML Schema referencesDEMO

Page 8: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

DEMODEMO

Page 9: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Adding a custom XML part (using the SDK)Adding a custom XML part (using the SDK)

public void XLInsertCustomXml(string fileName, string customXML){ using (SpreadsheetDocument xlPackage = SpreadsheetDocument.Open(fileName, true)) { // Add a new custom XML part. WorkbookPart workbookPart = xlPackage.WorkbookPart; CustomXmlPart xmlPart = workbookPart.AddNewPart<CustomXmlPart>();

// Copy the XML into the new part... using (Stream outputStream = xmlPart.GetStream()) { using (StreamWriter ts = new StreamWriter(outputStream)) { ts.Write(customXML); } } xlPackage.Save(); }}

Page 10: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

STRUCTURED DOCUMENT TAGSSTRUCTURED DOCUMENT TAGS

Page 11: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Structured Document TagsStructured Document Tags

Smart tags and custom XML markup add semantics, but do not have any effect on presentationSometimes you want to affect presentation

Custom UI behaviors: data-entry restrictions, multi-select, etc.

The solution: the structured document tag <sdt>

Page 12: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Structured Document TagsStructured Document Tags

A structured document tag has two sections:

SDT properties

SDT content

Page 13: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Structured Document Tag Properties <sdtPr>Structured Document Tag Properties <sdtPr>

Generic properties: Type, Title, ID, placholder textType-specific properties

Example: values for populating a dropdown list

Runtime behaviors (locking properties)Whether region is editableWhether region is deletable

<w:sdtPr> <w:id w:val="865823534" /> <w:placeholder> <w:docPart w:val="DefaultPlaceholder_22675703" /> </w:placeholder> <w:showingPlcHdr /> <w:dataBinding w:prefixMappings="xmlns:ns0='http://myschemas/thisone'" w:xpath="/ns0:Customer[1]/ns0:Name" w:storeItemID="myCustomXmlDataIdentifier" /> <w:text /> </w:sdtPr>

Page 14: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Other Control TypesOther Control Types

In addition to plain text:Rich TextDate PickerPictureComboboxDropdown ListDocument Building Block

Page 15: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

DATA BINDINGDATA BINDING

Page 16: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

DEMODEMO

Page 17: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Data Binding: Bringing It All TogetherData Binding: Bringing It All Together

2-way synchronization between:

Content controls (data in structured document tags)Custom XML nodes (data in your schema)

Content

Controls

Custom XML

part

Data Bindin

g

Interactive data-driven

documents

Page 18: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Data Binding BasicsData Binding Basics

To bind a custom Xml node to a structured document tag:Add a <dataBinding> element to the structured document tag properties <sdtPr><dataBinding> specifices a custom Xml part (by Custom XML Data Identifier) and an Xpath to a specific node within that part

Custom XML Data Identifier? What’s that?

The custom XML part has a properties partImplicit relationship in customXmlPart.xml.relsThe properties part specifies a Custom XML Data Identifier

DEMO

Page 19: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

DEMODEMO

Page 20: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Data Binding SyntaxData Binding Syntax

<dataBinding> element inside <sdtPr>

<dat

aBindi

ng>

<datastoreIte

m>

Custom Xml part

Relationship to custom Xml Properties

<w:dataBinding w:prefixMappings="xmlns:ns0='http://myschemas/thisone'" w:xpath="/ns0:Customer[1]/ns0:Name" w:storeItemID="myCustomXmlDataIdentifier"/>

Page 21: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Content Control ToolkitContent Control Toolkit

Open-source developer toolhttp://www.codeplex.com/Wiki/View.aspx?ProjectName=dbe

Automatically generates parts, relationships, and markup to bind custom XML parts to content controls

Use to create data-bound documents with no programmingUse as a learning tool, to generate markup for use in custom solutions

DEMO

Page 22: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Macros and custom XMLMacros and custom XML

Office 2007 has strong object-model support for custom XML parts

VBA macros can be used to enable additional types of data binding beyond built-in capabilities

DEMO

Page 23: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

DEMODEMO

Page 24: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

CUSTOM XML MARKUPCUSTOM XML MARKUP

Page 25: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Custom XML Markup BasicsCustom XML Markup Basics

Allows embedding the structure from any XML schema into a WordprocessingML documentCustom XML elements may have custom attributes

Open XML consumers/producers preserve your attributes

Document Body (start part)

Unstructured content

Tagged content

Unstructured content

Tagged content

etc …

Open XML Document

Sections of contenttagged with yourcustom schema

Page 26: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Where can Custom Xml Markup appear?Where can Custom Xml Markup appear?

Custom XML markup can appear at two levels

Block level (around and/or sibling to paragraphs)Inline (around and/or sibling to runs)

Custom XML markup may be nestedComplex multi-level data semantics may be used

Page 27: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Custom XML Markup ExampleCustom XML Markup Example

DEMO

Page 28: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

DEMODEMO

Page 29: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Custom XML Markup: Why <customXml>?Custom XML Markup: Why <customXml>?

Why not just embed the custom XML directly in the WordprocessingML document?

Answer: this way, your custom schema doesn’t need to support WordprocessingML within it

This abstraction provides the flexibility to support any schema in custom XML markup

Page 30: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

CUSTOM XML MAPPINGCUSTOM XML MAPPING

Page 31: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

XML Mapping OverviewXML Mapping Overview

Your custom schema / XML added to a workbookXML elements & attributes ‘mapped’ to cells & TablesStore a copy of the schema in the workbook

SpreadsheetML document

Open XML parts• Sheet data• Tables

XML data fileImport

Page 32: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

ExampleExample

Page 33: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

How It All Ties TogetherHow It All Ties Together

Map Object

Table1

Schema

Cell 1

Table 2 Cell 2

SchemaId

mapId

mapIdmapId

mapId

Page 34: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Custom XML SourceCustom XML Source

A cache of the user schemas added to the workbook (xmlmaps.xml)Nodes mapped to tables (table1.xml)Nodes mapped to single cells (tableSingleCells1.xml)

Page 35: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

xmlMaps.xmlxmlMaps.xml

………

Page 36: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Table1.xmlTable1.xml

Page 37: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

tableSingleCells.xmltableSingleCells.xml

Page 38: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

DEMODEMO

Page 39: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Custom XML Mapping ExampleCustom XML Mapping Example

Map the SchemaOpen a blank Excel spreadsheet. On the Data ribbon choose From Other Sources / From XML Data Import. Find the XML file you just saved and double-click it.

Import your DataFollowing the prompts to map to the SML schema and import the data to the worksheet.

Export the ResultsRight-click the list under the XML flyout and choose Export…

Page 40: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

Page 41: Open XML Developer Workshop Custom XML Integrating data and documents

Open XML Developer Workshop

LAB: CUSTOM XMLLAB: CUSTOM XML