Presentation is loading. Please wait.

Presentation is loading. Please wait.

Open XML Packages, Parts and Relationships

Similar presentations


Presentation on theme: "Open XML Packages, Parts and Relationships"— Presentation transcript:

1 Open XML Packages, Parts and Relationships

2 Disclaimer The 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, addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, 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.

3 Overview Introduction to the Open Packaging Convention
Working with System.IO.Packaging

4 Lesson: Introduction to the Open Packaging Convention
Open XML Formats Architecture Components of the Open Packaging Convention

5 Elements of the Open Packaging Convention
Common Package Parts Package Relationships Core Properties Digital Signatures Specific Format Parts Office Document Part Relationships Binary Part XML Part Part Rels Etc… Content Types Stream Package – The container Document Parts – The components of the document Content Types Stream – defines content type of parts Relationships – references to other parts of the document “Start Part” – The root of the document

6 The Package A package is
A logical entity that holds a collection of parts An aggregation of all pieces of a document in a single object Not tied to a physical representation ZIP, Database, loose files Typically a ZIP file e.g. Rename *.docx to *.zip Special features Core properties Thumbnail parts Digital signatures

7 Document Parts A part is Content type is enforced
analogous to a file on the file system stored inside the package in a specific location reachable via a URI stored with a specific content type mainly XML but other native types as well Images, sounds, video, OLE objects Content type is enforced Example: cannot tag JPEG part as GIF A part can use growth hints to allow uninterrupted content growth

8 Content Types Define the type of media stored in the part.
The content types stream Defines mappings from part to content type Is not directly addressable Default values and part specific overrides [Content_Types].xml <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <Types> <Default Extension="jpeg" ContentType="image/jpeg" /> <Default Extension="rels" ContentType= "application/vnd.openxmlformats-package.relationships+xml" /> <Default Extension="xml" ContentType="application/xml" /> <Override PartName="/word/document.xml" ContentType= "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> </Types> Defaults Overrides

9 Relationships Relationships are stored in XML streams in the package
Ties elements inside the package to each other Allows navigation of document without parsing parts Package relationships stream URI: /_rels/.rels Part relationships stream URI: _rels/[partname].rels <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <Relationships xmlns=" <Relationship Id=“rId1“ Type=" /2006/relationships/officeDocument" Target="document.xml"/> </Relationships> Relationship ID Relationship Type Target URI

10 Relationships A relationship has its own type
The type gives meaning to the content in a part for a given context A way to say “The styles part is stored at that location” /document.xml …wordprocessingml.document.main+xml Part URI Part content type _rels/document.xml.rels Rel. type /styles.xml Target URI /styles.xml …wordprocessingml.styles+xml Part URI Part content type

11 Inside an Open XML Package
demo For this demo use MinimalImage.docx

12 Identifying the OfficeDocument “start part”
The OPC Package Your Code Content Type Stream [Content_Types].xml Package Relationships /_rels/.rels Office Document Part e.g. /document.xml GetRelationship (Rel.Type) : “ PartUri : “/document.xml” GetContentType (PartUri) : “/documents.xml” Default extension or URI override ContentType : “application/vnd…wordprocessingml.document.main+xml” GetPart (PartUri) : “/document.xml” Part: XML

13 Identifying implicitly related parts
The OPC Package Your Code Content Type Stream [Content_Types].xml Doc.Part document.xml Part Relationships /_rels/document.xml.rels Related Part /styles.xml GetRelationship (Rel.Type): “ GetRelationship (Rel.Type): “ PartUri : “/styles.xml” PartUri : “/styles.xml” GetContentType (PartUri): “/styles.xml” Default extension or URI override ContentType: “application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml” GetPart (PartUri): “/styles.xml” Part: XML

14 Identifying explicitly related parts
The OPC Package Your Code Content Type Stream [Content_Types].xml Doc.Part document.xml Part Relationships /_rels/document.xml.rels Related Part /pict.png GetRelationship (Rel.ID): “rId1” GetRelationship (Rel.ID): “rId1” PartUri : “/pict.png” PartUri : “/pict.png” GetPartType (PartUri): “/pict.png” Default extension or URI override ContentType (Default extension or Override Uri): “image/png” GetPart (PartUri): “/pict.png” Part: Binary

15 demo Package Explorer Open source tool written by Wouter van Vugt

16 Lesson: Working with System.IO.Packaging
Components of System.IO.Packaging Traversing a Package Manipulating a Package Open XML Password Hashing

17 Components of System.IO.Packaging The library
Part of Windows Presentation Foundation Installed with .NET 3.0+ Requires .NET 2.0 Runtime Enables package manipulation for Open XML File Formats XML Paper Specification Files Any Open Packaging Convention files Open XML SDK 2.0 provides more advanced OPC manipulation tooling

18 Components of System.IO.Packaging The Package
Common Package Parts Package Relationships Core Properties Digital Signatures Specific Format Parts Office Document Part Relationships XML Part Part Rels Etc… Package class provides methods to create, enumerate and delete the following entities Package Package Properties PackageRelationships PackageParts

19 Components of System.IO.Packaging The PackageRelationship
Common Package Parts Package Relationships Core Properties Digital Signatures Specific Format Parts Office Document Part Relationships XML Part Part Rels Etc… Required to find parts (part names are not guaranteed) Iterate through a RelationshipCollection by type or ID Relationship Properties ID Package RelationshipType SourceUri TargetMode TargetUri

20 Components of System.IO.Packaging The PackagePart
Common Package Parts Package Relationships Core Properties Digital Signatures Specific Format Parts Office Document Part Relationships XML Part Part Rels Etc… A PackagePart is the object of data within the Package It provides support to create, enumerate and delete part relationships Get data as a System.IO.Stream PackagePart properties: CompressionOption ContentType Package Uri

21 Components of System.IO.Packaging The PackUriHelper
Find a related PackagePart by searching relationships, either by relationship type or relationship ID This returns a list of PackageRelationship objects A PackageRelationship defines two relative URIs Source URI, pointing to the source PackagePart Target URI, pointing to the target PackagePart Retrieve a PackagePart by using a URI relative to the root of the Package Translation of Source and Target URIs is required Use the PackUriHelper class to aid in the translation

22 Traversing a Package Opening the “Start-Part”
<Relationships xmlns=“ <Relationship Id="rId2" Type=" relationships/metadata/core-properties" Target="docProps/core.xml" />   <Relationship Id="rId1" Type=" officeDocument/2006/relationships/officeDocument" Target="word/document.xml" />   </Relationships>

23 Traversing a Package Opening the “Start-Part”
<Relationships xmlns=“ <Relationship Id="rId2" Type=" relationships/metadata/core-properties" Target="docProps/core.xml" />   <Relationship Id="rId1" Type=" officeDocument/2006/relationships/officeDocument" Target="word/document.xml" />   </Relationships> <w:document xmlns:w=" wordprocessingml/2006/main"> <w:body> <w:p> <w:r> <w:t>Hello World!</w:t> </w:r> </w:p> </w:body> </w:document>

24 Traversing a Package Opening the “Start-Part”
Now using the Packaging API string relationshipType = " using (Package package = Package.Open("mydocument.docx")) { PackageRelationship relationship = null; foreach (PackageRelationship searchRelation in package.GetRelationshipsByType(relationshipType)) relationship = searchRelation; break; } Uri partUri = PackUriHelper.ResolvePartUri( relationship.SourceUri, relationship.TargetUri); PackagePart mainPart = package.GetPart(partUri); // Do something with mainPart

25 Traversing a Package Opening a related document part
<Relationships xmlns="   <Relationship Id="rId5" Type=" relationships/theme" Target="theme/theme1.xml" />   </Relationships>

26 Traversing a Package Opening a related document part
<Relationships xmlns="   <Relationship Id="rId5" Type=" relationships/theme" Target="theme/theme1.xml" />   </Relationships> <a:theme xmlns:a=" name="Office Theme"> <a:themeElements> <a:clrScheme name="Office"> <a:dk1> <a:sysClr val="windowText" lastClr="000000" /> </a:dk1>

27 Traversing a Package Opening a related document part
You can retrieve a Part using a relationshipID Or using the relationshipType string relID= “rId1”; PackageRelationship imagerelationship = mainPart.GetRelationship(relID); Uri imagePartUri = PackUriHelper.ResolvePartUri( imagerelationship.SourceUri, imagerelationship.TargetUri); PackagePart imagepart = mainPart.Package.GetPart(imagePartUri); string RT_Image = “ int imageCount = 0; foreach (PackageRelationship relation in mainPart.GetRelationshipsByType(RT_Image)) { imageCount++; } Console.WriteLine("The part references {0} unique images", imageCount);

28 Traversing a Package demo

29 Creating a New Package The Packaging API allows you to create new Packages based on a file or stream Package.Open creates the Package when using the correct parameters using (Package package = Package.Open("newFile.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { // the Package is now on disk, 0 bytes in size }

30 Creating a Part Create a PackagePart using
Location (Uri) Content type (String) You can not alter the content type later on Make sure the location is not in use Use a Stream to modify the PackagePart contents string contentType = “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml”; PackagePart part = package.CreatePart( new Uri(“workbook.xml”, UriKind.Relative), contentType); using (StreamWriter writer = new StreamWriter( part.GetStream(FileMode.Create, FileAccess.Write))) { using (StreamReader reader = new StreamReader(path)) writer.Write(reader.ReadToEnd()); }

31 Creating Part Relationships
Each PackagePart needs to be related to its child parts Relationship ID, RelationshipType, TargetMode using (Package package = Package.Open("newFile.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { PackagePart workbook = package.CreatePart( new Uri(“/workbook.xml”, UriKind.Relative), “application/vnd...sheet.main+xml”); PackagePart sheet = package.CreatePart( new Uri(“/sheet1.xml”, UriKind.Relative), “application/vnd...worksheet.main+xml”, package); package.CreateRelationship( new Uri("/workbook.xml", UriKind.Relative), TargetMode.Internal, " "rId1"); workbook.CreateRelationship( new Uri("/sheet1.xml", UriKind.Relative), TargetMode.Internal, " "rId1"); } Start Part Related Part

32 Digital Signatures Digital Signatures in the Package
Allows the validation of package integrity using X.509 certificates You can sign All parts, including digital signatures All relationships, or a subset of a set of relationships Navigate signatures with the Digital Signature Origin Part

33 Digital Signatures Relationship Relationship URI URI URI URI
Digital Signature Origin Part Relationship Relationship Digital Signature XML Signature Part Digital Signature XML Signature Part URI URI URI X.509 Signed Part URI Signed Part Signed Part Signed Part X.509 Certificate Part

34 Where to get the Open XML SDK 2.0
Find links on homepage of Open XML Developer: MSDN documentation: MSDN support forum:

35 How to think about OPC packages
Files and folders – NO! Parts and relationships – YES The specific part names and folder structure are arbitrary implementation details that could change. (e.g., Corel, OpenOffice) The relationship structure is the true logical structure of the document.

36 Resources OpenXMLDeveloper.org
Brian Jones’s and Zeyad Rajabi’s Office Solutions blog Doug Mahugh’s blog XPS Blog:

37


Download ppt "Open XML Packages, Parts and Relationships"

Similar presentations


Ads by Google