Welcome To XML Session
Welcome To XML Session
Intended Audience : Developers who are beginning with XML and willing to reach to intermediary level
of expertise.
1
Take away from the presentation.
• Evolution of XML.
• Ability to create a welformed and valid XML.
• Work with XSLT and XPath.
• Work with Namspaces.
• Understand and Work with SAX and DOM parser.
• Understand and build Schemas.
• Patterns for XML schemas.
2
Introduction
• Life of XML.
• XML is here to stay. This is backed by the W3C World Wide Web Consortium.
3
Introduction Continued…
• What is an XML?
• Sample shown.
• Practical Applications.
• Some kind of DTO. Client Interface development(XHTML/SVG), Process
Definitions, Deplyment descriptors, RPC evolved to SOAP.
4
Well-formed
• Attribute
• Attributes are simple name/value pairs associated with an element.
5
Well-formed continued…
• Comments.
• <!– do we need to do this everytime-->
• Empty elements.
• Does it make a lot of sense.
• XML declaration.
• Complete XML declaration is <?xml version=‘1.0’ encoding=‘UTF-8’ standalone=‘yes’?>
• Escape charaters.
• This is required to make an xml well-formed. Believe me this is not as easy as it looks.
• CDATA SECTION.
• <![CDATA[This is a life saver :->>>]]>
• Finally Errors.
– Error
– FatalErrors
6
XSLT and XPath
• What is XSLT?
• This is the template which defines the transformation rules and defines the target forrmat. This is
a variant of XSL language.
7
XPath
• Whats a Node?
• Xpath uses the term “Node” to refer to any part of a document. It may be
an element, attribute or otherwise. “node-set” is a collection of nodes.
• Basics of Xpath.
– The document root is not the root element. It is the root of the document hierarchy.
– The xpath elements can be relative or absolute.
– @symbol is used to address attributes in the XML.
– // is used for recursive search. Example //SECT will return the SECT nodes irrespective
of where the current cursor in the document is.
– The location paths are not just on elements but can also match specific values using [].
Example : //PARA[SECT] or value based such as //PARA[@sensitive = ‘high’]
• Complex example :
select title from document where article/sect/para/sensitivity is high and
article/sect/sect/para/sensitivity is high.
8
Xpath Functions
• Node Functions
– name()
– node()
– processing-instruction()
– comment()
– text()
9
Xpath Functions
• Positional Functions
– position()
– last()
– count()
• Numeric functions
– number()
– sum()
• Boolean Functions
– boolean()
– not()
– True()
– False()
10
Xpath Functions
• String Functions
– String()
– String-length()
– Concat()
– Contains()
– Starts-with()
– Substring()
– Substring-after()
– Substring-before()
– Translate()
11
XSLT -Detail
• Default Templates.
– <xsl:template=“*|/”> this is for root element in case no root element template is defined.
– <xsl:template match=“text()|@”>
12
XSLT Elements
• <xslt:stylesheet> is the root element of the stylesheet and is used like this
– <xsl:stylesheet version=“1.0” xmlns:xsl=http://www.wrc.org/1999/XSL/transformation>
• <xsl-value-of> it searches the context node for the value specified in the select
attribute xpath expression and inserts it into the result tree.
• <xsl:output> element allows to specify the method used and also gives a better
control over the way of the output.
– <xsl:output method=“xml” version=“1.0” encoding=“UTF-8” standalone=“yes”
13
XSLT Elements
• <xsl:if> It evaluates the expression in the test attribute, and if true this
element contents are executed.
– Example : <xsl:if test=“age[. <20]”>Boy</xsl:if>
• <xsl:choose>
• <xsl:for-each>
14
Namespaces
• Default Namespace
– <student xmlns=“http://www.mycollege/student” >
15
Schemas
• Properties of Schema.
– Syntax XML
– Tools any XML tools
– DOMSupport Yes
– contentModels Strong- supports mixed content, can specify exact occurrences
– Datatypes Strong – Support all the commonly used datatypes
– Name scope Global and local namescope
– Inheritance Yes
– Extensibility Yes
– Multiple vocabulary Yes, supported with multiple schemas and namespaces.
– Dynamic Schemas Yes
16
Schemas
• Datatypes
– Primitive datatypes
– Derived datatypes
• Primitive types can be used for element or attribute values, they don’t have
child elements or attributes. Primitive types are built in
17
Schemas
– String
– boolean
– float
– double
– decimal
– timeduration
– recurringDuration
– binary
– uriReference
– ID
– IDREF
– ENTITY
– NOTATION
– QName
18
Schemas
19
Schemas
• Atomic datatype.
– An atomic type is one that has a value that cannot be divided when used without
extending.
• Example : <specialDay>10-10-2001</specialDay>
<account>10103453</account>
• List Datatype
– A list type has a value that’s comprised of a finite length sequence of atomic values.
• Example : <simpleType name=“height” base=“decimal” derivedBy=“List”>
<StudentsHeights>4 4.5 5 5.5 6 6.5</StudentHeights>
20
Aspects of Datatype
• Example : In the <Title> element the lexical and name representation is the
same.
• In case of number the lexical and value representation are different. The
lexical representation represents “12345” and the value representation is
equal to 12345.00.
• Facets are basically fundamental facets such as comparision and/or
constraining facets. A>B or A minIclusive=3
21
• Constraining Facets.
– Length, minLength,maxLength
– Pattern
– Enumeration
– minExclusive, maxInclusive, maxInclusive, maxExclusive
– Precision, scale
– Encoding
– Duration, period
22
Schemas
• Declaring a schema
– <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
23
Datatype Definitions
• Simple type
– Simple definition are how to create derived datatypes. Including those that were built
into the schema specifications
• Complex definitions
– Complex definitions are primarily used to describe content models.
24
Schemas
• Syntax
– Name
– Base – optional
– Abstract – boolean – optional
– Derivedby – (list | restriction ) – OPTIONAL (default is restriction)
• Example
– <simpleType name=“course” base=“xsi:string”>
– <minLength value = “3”/>
– <maxLength value = “25”/>
– </simpleType>
25
Schema
– <ListofNOS>1 5 10 15 20 25 </ListofNOS>
26
Schema
27
Schema
• Syntax
– Name - OPTIONAL
– Base - OPTIONAL
– Abstract – boolean – OPTIONAL
– derivedBy – (extension | restriction)
– Content – (elementOnly | empty | mixed | textOnly )
– Block – “ ” or (#all | (extension | restriction)) – Optional
– Final – “ “ or (#all | (extension | restriction)) – Optional
• Cardinality
– Minoccurs maxOccurs
• <Choice> and <Sequence>
• <all> this element can be used to specify which child element can be present, with
out any specific sequence
• <any> This element can allow any content model with namespace such as other XML
or HTML.
28
Schemas
• XSD Patterns
• Collection Element
– Multiple elements of the same element type need to appear in the document as siblings
of each other. Often metadata about the container needs to be expressed as well. The
Collection Element is a logical place to put this metadata. Sometimes, the elements
need to be grouped into different categories. Multiple Collection Elements can appear in
the document, with each container having elements from one of the sub categories.
• Domain Element
– Every document has a domain, and every domain has unique concepts.
29
schemas
30