World XML Schema Notes

来源:互联网 发布:js事件代码 编辑:程序博客网 时间:2024/04/30 09:01

1)
XML Support in Word 2003
XML support in Word 2003 is new exciting feature. Word 2003 now supports native XML vocabulary called Word Markup Language (WordML). Each document has the following basic structure:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument
    xmlns:w=http://schemas.microsoft.com/office/word/2003/wordml>
   <!-- WordML structure goes here -->
</w:wordDocument>
XML support in Word 2003 gives the end-user ability to add custom tags (and namespaces) to the document without corrupting its structure.


2)
Define data structure
(-----)
1. XML Data File
2. Generate the XSD file with the tool
In order to make Word generate XSD file for us - we will use Word Xml Toolbox, which should be downloaded from Microsoft site and installed.

Now we can try to open our XML file using word. We will see nothing special - just XML structure and data. Our goal is to create XSD file now and it can be achieved by clicking "Generate Inferred Schema" From XML Toolbox menu (see image below).


(-----)
or with Visual Studio XSD Editor//

XML Schema SimpleType vs ComplexType
Overview
An XML Schema is an XML-based representation of the structure of an XML document.
XML Schema supports data types and namespaces; a DTD does not.

A simpleType is represented with a <xs:simple Type/> declaration.

SimpleType with Restriction Element.
<xs:restriction base="xs:decimal">

Declaration <A>1.0</A> or <A>25.0</A> would not validate with the Schema.


ComplexType Declaration
A complexType is used to constrain elements and attributes in an XML document. It is represented with <xs:complex Type/>.


Simple Types versus Complex Types
Simple types and complex types differ in this way: simple types cannot have element children or attributes; complex types may have element children and attributes.

Tracing the type hierarchy down the branch of simple types, we see that the first simple type is anySimpleType, which is also type that you could actually use. W3C XML Schemas has 44 built-in simple types, each of which derives from anySimpleType, and all but three of which derive by restriction.
Not a single one derives by extension. To extend a simple type would mean to add element children or an attribute. This contradicts the definition of simple type in W3C Schemas and is thus prohibited.


3) Word XM Define with XSD

 

4) Coding

----1) define namespace manager

private static XmlNamespaceManager nsmgr =
new XmlNamespaceManager(new NameTable());


-----2)Opening XmlDocument is easy as well:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XmlReader.Create(template));

----3)Next thing is to fill namespace manager with our namespaces:

nsmgr.AddNamespace("w",
"
http://schemas.microsoft.com/office/word/2003/wordml");
nsmgr.AddNamespace("ns0", "test-customer");


register two namespaces within manager

In this case WordML namespace should always have "w" prefix, while custom namespaces may vary.

原创粉丝点击