XML Material
XML Material
Xml
Xml stands for extensible markup language. Xml is designed to transport and store data. Xml is important to know, and very easy to learn. Html was designed to display data.
Html Javascript
What is xml?
y y y y y y
Xml stands for extensible markup language Xml is a markup language much like html Xml was designed to carry data, not to display data Xml tags are not predefined. You must define your own tags Xml is designed to be self-descriptive Xml is a w3c recommendation
Xml was designed to transport and store data, with focus on what data is Html was designed to display data, with focus on how data looks
Xml is everywhere
Xml is now as important for the web as html was to the foundation of the web. Xml is the most common tool for data transmissions between all sorts of applications.
One of the most time-consuming challenges for developers is to exchange data between incompatible systems over the internet. Exchanging data as xml greatly reduces this complexity, since the data can be read by different incompatible applications.
Xhtml Wsdl for describing available web services Wap and wml as markup languages for handheld devices Rss languages for news feeds Rdf and owl for describing resources and ontology Smil for describing multimedia for the web
All elements can have sub elements (child elements): <root> <child> <subchild>.....</subchild> </child> </root> The terms parent, child, and sibling are used to describe the relationships between elements. Parent elements have children. Children on the same level are called siblings (brothers or sisters). All elements can have text content and attributes (just like in html).
Example:
The image above represents one book in the xml below: <bookstore> <book category="cooking"> <title lang="en">everyday italian</title> <author>giada de laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">harry potter</title> <author>j k. Rowling</author> <year>2005</year> <price>29.99</price> </book>
<book category="web"> <title lang="en">learning xml</title> <author>erik t. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> The root element in the example is <bookstore>. All <book> elements in the document are contained within <bookstore>. The <book> element has 4 children: <title>,< author>, <year>, <price>.
Entity references
Some characters have a special meaning in xml. If you place a character like "<" inside an xml element, it will generate an error because the parser interprets it as the start of a new element. This will generate an xml error: <message>if salary < 1000 then</message> To avoid this error, replace the "<" character with an entity reference: <message>if salary < 1000 then</message> There are 5 predefined entity references in xml: < > & ' " < > & ' " Less than Greater than Ampersand Apostrophe Quotation mark
Note: only the characters "<" and "&" are strictly illegal in xml. The greater than character is legal, but it is a good habit to replace it.
Comments in xml
The syntax for writing comments in xml is similar to that of html. <!-- this is a comment -->
Xml elements
An xml document contains xml elements.
<bookstore> <book category="children"> <title>harry potter</title> <author>j k. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title>learning xml</title> <author>erik t. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> In the example above, <bookstore> and <book> have element contents, because they contain other elements. <book> also has an attribute (category="children"). <title>, <author>, <year>, and <price> have text content because they contain text.
Names can contain letters, numbers, and other characters Names cannot start with a number or punctuation character Names cannot start with the letters xml (or xml, or xml, etc) Names cannot contain spaces
Let's imagine that we created an application that extracted the <to>, <from>, and <body> elements from the xml document to produce this output: Message To: tove from: jani Don't forget me this weekend! Imagine that the author of the xml document added some extra information to it: <note> <date>2008-01-10</date> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>don't forget me this weekend!</body> </note> Should the application break or crash? No. The application should still be able to find the <to>, <from>, and <body> elements in the xml document and produce the same output.
One of the beauties of xml, is that it can be extended without breaking applications.
Xml attributes
Xml elements can have attributes, just like html. Attributes provide additional information about an element.
Xml attributes
In html, attributes provide additional information about elements: <img src="computer.gif"> <a href="demo.asp"> Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but can be important to the software that wants to manipulate the element:<file type="gif">computer.gif</file>
My favorite way
The following three xml documents contain exactly the same information: A date attribute is used in the first example:
<note date="10/01/2008"> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>don't forget me this weekend!</body> </note> A date element is used in the second example: <note> <date>10/01/2008</date> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>don't forget me this weekend!</body> </note> An expanded date element is used in the third: (this is my favorite): <note> <date> <day>10</day> <month>01</month> <year>2008</year> </date> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>don't forget me this weekend!</body> </note>
Attributes cannot contain multiple values (elements can) Attributes cannot contain tree structures (elements can) Attributes are not easily expandable (for future changes)
Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data. Don't end up like this: <note day="10" month="01" year="2008" to="tove" from="jani" heading="reminder"
Xml validation
Xml with correct syntax is "well formed" xml. Xml validated against a dtd is "valid" xml.
Xml documents must have a root element Xml elements must have a closing tag Xml tags are case sensitive Xml elements must be properly nested Xml attribute values must be quoted
SOA SOLUTIONS, Near S.R Nagar E- Seva
<?Xml version="1.0" encoding="iso-8859-1"?> <note> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>don't forget me this weekend!</body> </note>
Xml dtd
The purpose of a dtd is to define the structure of an xml document. It defines the structure with a list of legal elements: <!Doctype note [ <!element note (to,from,heading,body)> <!element to (#pcdata)> <!element from (#pcdata)> <!element heading (#pcdata)> <!element body (#pcdata)> ]>
Xml schema
W3c supports an xml-based alternative to dtd, called xml schema: <xs:element name="note"> <xs:complextype> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complextype> </xs:element>
Xml namespaces
Xml namespaces provide a method to avoid element name conflicts.
Name conflicts
In xml, element names are defined by the developer. This often results in a conflict when trying to mix xml documents from different xml applications. This xml carries html table information: <table> <tr> <td>apples</td> <td>bananas</td> </tr> </table> This xml carries information about a table (a piece of furniture): <table> <name>african coffee table</name> <width>80</width> <length>120</length> </table> If these xml fragments were added together, there would be a name conflict. Both contain a <table> element, but the elements have different content and meaning. An xml parser will not know how to handle these differences.
<h:table> <h:tr> <h:td>apples</h:td> <h:td>bananas</h:td> </h:tr> </h:table> <f:table> <f:name>african coffee table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> In the example above, there will be no conflict because the two <table> elements have different names.
<root> <h:table xmlns:h="http://www.w3.org/tr/html4/"> <h:tr> <h:td>apples</h:td> <h:td>bananas</h:td> </h:tr> </h:table> <f:table xmlns:f="http://www.w3schools.com/furniture"> <f:name>african coffee table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root> In the example above, the xmlns attribute in the <table> tag give the h: and f: prefixes a qualified namespace. When a namespace is defined for an element, all child elements with the same prefix are associated with the same namespace.
Namespaces can be declared in the elements where they are used or in the xml root element: <root xmlns:h="http://www.w3.org/tr/html4/" xmlns:f="http://www.w3schools.com/furniture"> <h:table> <h:tr> <h:td>apples</h:td> <h:td>bananas</h:td> </h:tr> </h:table> <f:table> <f:name>african coffee table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root> Note: the namespace uri is not used by the parser to look up information. The purpose is to give the namespace a unique name. However, often companies use the namespace as a pointer to a web page containing namespace information.
Default namespaces
Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax: Xmlns="namespaceuri"
This xml carries html table information: <table xmlns="http://www.w3.org/tr/html4/"> <tr> <td>apples</td> <td>bananas</td> </tr> </table> This xml carries information about a piece of furniture: <table xmlns="http://www.w3schools.com/furniture"> <name>african coffee table</name> <width>80</width> <length>120</length> </table>
Xml cdata
All text in an xml document will be parsed by the parser. But text inside a cdata section will be ignored by the parser.
"&" will generate an error because the parser interprets it as the start of an character entity. Some text, like javascript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as cdata. Everything inside a cdata section is ignored by the parser. A cdata section starts with "<![cdata[" and ends with "]]>": <script> <![cdata[ function matchwo(a,b) { if (a < b && a < 0) then { return 1; } else { return 0; } } ]]> </script> In the example above, everything inside the cdata section is ignored by the parser. Notes on cdata sections: A cdata section cannot contain the string "]]>". Nested cdata sections are not allowed. The "]]>" that marks the end of the cdata section cannot contain spaces or line breaks.
Xml encoding
Ml documents can contain non ascii characters, like norwegian , or french . To avoid errors, specify the xml encoding, or save xml files as unicode.
You get this error if your xml contains non ascii characters, and the file was saved as single-byte ansi (or ascii) with no encoding specified. Switch from current encoding to specified encoding not supported. You get this error if your xml file was saved as double-byte unicode (or utf-16) with a singlebyte encoding (windows-1252, iso-8859-1, utf-8) specified. You also get this error if your xml file was saved with single-byte ansi (or ascii), with doublebyte encoding (utf-16) specified.
Windows notepad
Windows notepad save files as single-byte ansi (ascii) by default. If you select "save as...", you can specify double-byte unicode (utf-16). Save the xml file below as unicode (note that the document does not contain any encoding attribute): <?Xml version="1.0"?> <note> <from>jani</from> <to>tove</to> <message>norwegian: . French: </message> </note> The file above, note_encode_none_u.xml will not generate an error. But if you specify a singlebyte encoding it will. The following encoding (open it), will give an error message: <?Xml version="1.0" encoding="windows-1252"?> The following encoding (open it), will give an error message: <?Xml version="1.0" encoding="iso-8859-1"?> The following encoding (open it), will give an error message: <?Xml version="1.0" encoding="utf-8"?> The following encoding (open it), will not give an error: <?Xml version="1.0" encoding="utf-16"?>
Conclusion
y y y y
Always use the encoding attribute Use an editor that supports encoding Make sure you know what encoding the editor uses Use the same encoding in your encoding attribute
Web services
web services:
Web services can convert your application into a web-application, which can publish its function or message to the rest of the world. The basic web services platform is xml + http. Web services can convert your applications into web-applications. Web services are published, found, and used through the web.
Html Xml
Web services are application components Web services communicate using open protocols Web services are self-contained and self-describing Web services can be discovered using uddi Web services can be used by other applications Xml is the basis for web services
y y y
Soap (simple object access protocol) Uddi (universal description, discovery and integration) Wsdl (web services description language)
What is soap?
Soap is an xml-based protocol to let applications exchange information over http. Or more simple: soap is a protocol for accessing a web service.
y y y y y y y y y y
Soap stands for simple object access protocol Soap is a communication protocol Soap is a format for sending messages Soap is designed to communicate via internet Soap is platform independent Soap is language independent Soap is based on xml Soap is simple and extensible Soap allows you to get around firewalls Soap is a w3c standard
What is wsdl?
Wsdl is an xml-based language for locating and describing web services.
y y y y y
Wsdl stands for web services description language Wsdl is based on xml Wsdl is used to describe web services Wsdl is used to locate web services Wsdl is a w3c standard
What is uddi?
Uddi is a directory service where companies can register and search for web services.
y y y y y
Uddi stands for universal description, discovery and integration Uddi is a directory for storing information about web services Uddi is a directory of web service interfaces described by wsdl Uddi communicates via soap Uddi is built into the microsoft .net platform
Any application can have a web service component. Web services can be created regardless of programming language.
Wsdl (web services description language) is an xml-based language for describing web services and how to access them
Wsdl is an xml-based language for describing web services and how to access them.
What is wsdl?
y y y y y y
Wsdl stands for web services description language Wsdl is written in xml Wsdl is an xml document Wsdl is used to describe web services Wsdl is also used to locate web services Wsdl is a w3c recommendation
The messages used by the web service The operations performed by the web service The communication protocols used by the web service
The main structure of a wsdl document looks like this: <definitions> <types> definition of types........ </types> <message> definition of a message.... </message> <porttype> definition of a port....... </porttype> <binding> definition of a binding.... </binding> </definitions> A wsdl document can also contain other elements, like extension elements, and a service element that makes it possible to group together the definitions of several web services in one single wsdl document.
Wsdl ports
The <porttype> element is the most important wsdl element. It describes a web service, the operations that can be performed, and the messages that are involved. The <porttype> element can be compared to a function library (or a module, or a class) in a traditional programming language.
Wsdl messages
The <message> element defines the data elements of an operation.
Each message can consist of one or more parts. The parts can be compared to the parameters of a function call in a traditional programming language.
Wsdl types
The <types> element defines the data types that are used by the web service. For maximum platform neutrality, wsdl uses xml schema syntax to define data types.
Wsdl bindings
The <binding> element defines the message format and protocol details for each port. Wsdl example This is a simplified fraction of a wsdl document: <message name="gettermrequest"> <part name="term" type="xs:string"/> </message> <message name="gettermresponse"> <part name="value" type="xs:string"/> </message> <porttype name="glossaryterms"> <operation name="getterm"> <input message="gettermrequest"/> <output message="gettermresponse"/> </operation> </porttype> In this example the <porttype> element defines "glossaryterms" as the name of a port, and "getterm" as the name of an operation. The "getterm" operation has an input message called "gettermrequest" and an output message called "gettermresponse". The <message> elements define the parts of each message and the associated data types. Compared to traditional programming, glossaryterms is a function library, "getterm" is a function with "gettermrequest" as the input parameter, and gettermresponse as the return parameter.
A wsdl port describes the interfaces (legal operations) exposed by a web service.
SOA SOLUTIONS, Near S.R Nagar E- Seva
Wsdl ports
The <porttype> element is the most important wsdl element. It defines a web service, the operations that can be performed, and the messages that are involved. The port defines the connection point to a web service. It can be compared to a function library (or a module, or a class) in a traditional programming language. Each operation can be compared to a function in a traditional programming language.
Operation types
The request-response type is the most common operation type, but wsdl defines four types: Type One-way Request-response Solicit-response Notification Definition The operation can receive a message but will not return a response The operation can receive a request and will return a response The operation can send a request and will wait for a response The operation can send a message but will not wait for a response
One-way operation
A one-way operation example: <message name="newtermvalues"> <part name="term" type="xs:string"/> <part name="value" type="xs:string"/> </message> <porttype name="glossaryterms"> <operation name="setterm"> <input name="newterm" message="newtermvalues"/> </operation> </porttype > In the example above, the port "glossaryterms" defines a one-way operation called "setterm". The "setterm" operation allows input of new glossary terms messages using a "newtermvalues" message with the input parameters "term" and "value". However, no output is defined for the operation.
Request-response operation
A request-response operation example: <message name="gettermrequest"> <part name="term" type="xs:string"/> </message> <message name="gettermresponse"> <part name="value" type="xs:string"/> </message> <porttype name="glossaryterms"> <operation name="getterm"> <input message="gettermrequest"/> <output message="gettermresponse"/> </operation> </porttype> In the example above, the port "glossaryterms" defines a request-response operation called "getterm". The "getterm" operation requires an input message called "gettermrequest" with a parameter called "term", and will return an output message called "gettermresponse" with a parameter called "value". The binding element has two attributes - name and type. The name attribute (you can use any name you want) defines the name of the binding, and the type attribute points to the port for the binding, in this case the "glossaryterms" port. The soap: binding element has two attributes - style and transport. The style attribute can be "rpc" or "document". In this case we use document. The transport attribute defines the soap protocol to use. In this case we use http. The operation element defines each operation that the port exposes. For each operation the corresponding soap action has to be defined. You must also specify how the input and output are encoded. In this case we use "literal". Universal description, discovery and integration (uddi) is a directory service where businesses can register and search for web services.
Uddi
What is uddi
Uddi is a platform-independent framework for describing services, discovering businesses, and integrating business services by using the internet.
y y y y y
Uddi stands for universal description, discovery and integration Uddi is a directory for storing information about web services Uddi is a directory of web service interfaces described by wsdl Uddi communicates via soap Uddi is built into the microsoft .net platform
What is uddi based on? Uddi uses world wide web consortium (w3c) and internet engineering task force (ietf) internet standards such as xml, http, and dns protocols. Uddi uses wsdl to describe interfaces to web services Additionally, cross platform programming features are addressed by adopting soap, known as xml protocol messaging specifications found at the w3c web site.
Uddi benefits
Any industry or businesses of all sizes can benefit from uddi. Before uddi, there was no internet standard for businesses to reach their customers and partners with information about their products and services. Nor was there a method of how to integrate into each other's systems and processes. Problems the uddi specification can help to solve:
y y y y y y
Making it possible to discover the right business from the millions currently online Defining how to enable commerce once the preferred business is discovered Reaching new customers and increasing access to current customers Expanding offerings and extending market reach Solving customer-driven need to remove barriers to allow for rapid participation in the global internet economy Describing services and business processes programmatically in a single, open, and secure environment
If the industry published an uddi standard for flight rate checking and reservation, airlines could register their services into an uddi directory. Travel agencies could then search the uddi directory to find the airline's reservation interface. When the interface is found, the travel agency can communicate with the service immediately because it uses a welldefined reservation interface.
Soap tutorial
y y
Soap is a simple xml-based protocol to let applications exchange information over http. In our soap tutorial, you will learn what soap is, and how it uses xml to exchange information between applications.
Soap is a simple xml-based protocol to let applications exchange information over http. Or more simply: soap is a protocol for accessing a web service.
What is soap?
y y y y y y y y y y y
Soap stands for simple object access protocol Soap is a communication protocol Soap is for communication between applications Soap is a format for sending messages Soap communicates via internet Soap is platform independent Soap is language independent Soap is based on xml Soap is simple and extensible Soap allows you to get around firewalls Soap is a w3c recommendation
An envelope element that identifies the xml document as a soap message A header element that contains header information A body element that contains call and response information A fault element containing errors and status information
y y y y y y y y
A soap message must use the soap envelope namespace A soap message must use the soap encoding namespace A soap message must not contain a dtd reference A soap message must not contain xml processing instructions The soap envelope element is the root element of a soap message.
Description A code for identifying the fault A human readable explanation of the fault Information about who caused the fault to happen Holds application specific error information related to the body element
The faultcode values defined below must be used in the faultcode element when describing faults: Error Versionmismatch Mustunderstand Description Found an invalid namespace for the soap envelope element An immediate child element of the header element, with the mustunderstand attribute set to "1", was not understood
Client Server
The message was incorrectly formed or contained incorrect information There was a problem with the server so the message could not proceed
Xsd
An xml schema describes the structure of an xml document. In this tutorial you will learn how to create xml schemas, why xml schemas are more powerful than dtds, and how to use xml schema in your application.
An xml schema:
y y y y y y
Defines elements that can appear in a document Defines attributes that can appear in a document Defines which elements are child elements Defines the order of child elements Defines the number of child elements Defines whether an element is empty or can include text
y y
Defines data types for elements and attributes Defines default and fixed values for elements and attributes
y y y y y
Xml schemas are extensible to future additions Xml schemas are richer and more powerful than dtds Xml schemas are written in xml Xml schemas support data types Xml schemas support namespaces
It is easier to describe allowable document content It is easier to validate the correctness of data It is easier to work with data from a database It is easier to define data facets (restrictions on data) It is easier to define data patterns (data formats) It is easier to convert data between different data types
y y y y y
You don't have to learn a new language You can use your xml editor to edit your schema files You can use your xml parser to parse your schema files You can manipulate your schema with the xml dom You can transform your schema with xslt
Reuse your schema in other schemas Create your own data types derived from the standard types Reference multiple schemas in the same document
It must begin with the xml declaration It must have one unique root element Start-tags must have matching end-tags Elements are case sensitive All elements must be closed All elements must be properly nested All attribute values must be quoted Entities must be used for special characters
Even if documents are well-formed they can still contain errors, and those errors can have serious consequences. Think of the following situation: you order 5 gross of laser printers, instead of 5 laser printers. With xml schemas, most of these errors can be caught by your validating software.
A dtd file
The following example is a dtd file called "note.dtd" that defines the elements of the xml document above ("note.xml"): <!Element note (to, from, heading, body)> <!element to (#pcdata)> <!element from (#pcdata)> <!element heading (#pcdata)> <!element body (#pcdata)> The first line defines the note element to have four child elements: "to, from, heading, body". Line 2-5 defines the to, from, heading, body elements to be of type "#pcdata".
An xml schema
The following example is an xml schema file called "note.xsd" that defines the elements of the xml document above ("note.xml"):
<?Xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementformdefault="qualified"> <xs:element name="note"> <xs:complextype> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complextype> </xs:element> </xs:schema> The note element is a complex type because it contains other elements. The other elements (to, from, heading, body) are simple types because they do not contain other elements. You will learn more about simple and complex types in the following chapters.
A reference to a dtd
This xml document has a reference to a dtd: <?Xml version="1.0"?> <!Doctype note system "http://www.w3schools.com/dtd/note.dtd"> <note> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>don't forget me this weekend!</body> </note>
<?Xml version="1.0"?> <note xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.w3schools.com note.xsd"> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>don't forget me this weekend!</body> </note>
Xsd - the <schema> element The <schema> element is the root element of every xml schema.
The following fragment: Xmlns:xs="http://www.w3.org/2001/xmlschema" Indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/xmlschema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/xmlschema" namespace should be prefixed with xs: This fragment: Targetnamespace="http://www.w3schools.com" Indicates that the elements defined by this schema (note, to, from, heading, body.) Come from the "http://www.w3schools.com" namespace. This fragment: Xmlns="http://www.w3schools.com" Indicates that the default namespace is "http://www.w3schools.com". This fragment: Elementformdefault="qualified" Indicates that any elements used by the xml instance document which were declared in this schema must be namespace qualified.
The following fragment: Xmlns="http://www.w3schools.com" Specifies the default namespace declaration. This declaration tells the schema-validator that all the elements used in this xml document are declared in the "http://www.w3schools.com" namespace. Once you have the xml schema instance namespace available: Xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" You can use the schemalocation attribute. This attribute has two values. The first value is the namespace to use. The second value is the location of the xml schema to use for that namespace: Xsi:schemalocation="http://www.w3schools.com note.xsd" Xsd simple elements Xml schemas define the elements of your xml files. A simple element is an xml element that contains only text. It cannot contain any other elements or attributes.
y y y y y y
Example Here are some xml elements: <lastname>refsnes</lastname> <age>36</age> <dateborn>1970-03-27</dateborn> And here are the corresponding simple element definitions: <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/>
<xs:element name="color" type="xs:string" default="red"/> A fixed value is also automatically assigned to the element, and you cannot specify another value. In the following example the fixed value is "red": <xs:element name="color" type="xs:string" fixed="red"/> Xsd attributes All attributes are declared as simple types.
What is an attribute?
Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type.
SOA SOLUTIONS, Near S.R Nagar E- Seva
Example Here is an xml element with an attribute: <lastname lang="en">smith</lastname> And here is the corresponding attribute definition: <xs:attribute name="lang" type="xs:string"/>
Attributes are optional by default. To specify that the attribute is required, use the "use" attribute: <xs:attribute name="lang" type="xs:string" use="required"/>
Restrictions on content
When an xml element or attribute has a data type defined, it puts restrictions on the element's or attribute's content. If an xml element is of type "xs:date" and contains a string like "hello world", the element will not validate. With xml schemas, you can also add your own restrictions to your xml elements and attributes. These restrictions are called facets. You can read more about facets in the next chapter.
Empty elements Elements that contain only other elements Elements that contain only text Elements that contain both other elements and text
A complex xml element, "food", which contains only text: <food type="dessert">ice cream</food> A complex xml element, "description", which contains both elements and text: <description> it happened on <date lang="norwegian">03.03.99</date> .... </description>
<xs:complextype name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complextype> If you use the method described above, several elements can refer to the same complex type, like this: <xs:element name="employee" type="personinfo"/> <xs:element name="student" type="personinfo"/> <xs:element name="member" type="personinfo"/> <xs:complextype name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complextype> You can also base a complex element on an existing complex element and add some elements, like this: <xs:element name="employee" type="fullpersoninfo"/> <xs:complextype name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complextype> <xs:complextype name="fullpersoninfo"> <xs:complexcontent> <xs:extension base="personinfo"> <xs:sequence> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence>
Xsd indicators
We can control how elements are to be used in documents with indicators.
Indicators
There are seven indicators: Order indicators:
y y y
Occurrence indicators:
y y
Maxoccurs Minoccurs
Group indicators:
y y
Order indicators
Order indicators are used to define the order of the elements. All indicator The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once: <xs:element name="person"> <xs:complextype> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all>
</xs:complextype> </xs:element> Note: when using the <all> indicator you can set the <minoccurs> indicator to 0 or 1 and the <maxoccurs> indicator can only be set to 1 (the <minoccurs> and <maxoccurs> are described later). Choice indicator The <choice> indicator specifies that either one child element or another can occur: <xs:element name="person"> <xs:complextype> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complextype> </xs:element> Sequence indicator The <sequence> indicator specifies that the child elements must appear in a specific order: <xs:element name="person"> <xs:complextype> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complextype> </xs:element>
Occurrence indicators
Occurrence indicators are used to define how often an element can occur. Note: for all "order" and "group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxoccurs and minoccurs is 1.
Maxoccurs indicator
The <maxoccurs> indicator specifies the maximum number of times an element can occur: <xs:element name="person"> <xs:complextype> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxoccurs="10"/> </xs:sequence> </xs:complextype> </xs:element> The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minoccurs is 1) and a maximum of ten times in the "person" element. Minoccurs indicator The <minoccurs> indicator specifies the minimum number of times an element can occur: <xs:element name="person"> <xs:complextype> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxoccurs="10" minoccurs="0"/> </xs:sequence> </xs:complextype> </xs:element> The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element. Tip: to allow an element to appear an unlimited number of times, use the maxoccurs="unbounded" statement: A working example: An xml file called "myfamily.xml": <?Xml version="1.0" encoding="iso-8859-1"?> <persons xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:nonamespaceschemalocation="family.xsd"> <person> <full_name>hege refsnes</full_name> <child_name>cecilie</child_name> </person> <person> <full_name>tove refsnes</full_name> <child_name>hege</child_name> <child_name>stale</child_name> <child_name>jim</child_name> <child_name>borge</child_name> </person> <person> <full_name>stale refsnes</full_name> </person> </persons> The xml file above contains a root element named "persons". Inside this root element we have defined three "person" elements. Each "person" element must contain a "full_name" element and it can contain up to five "child_name" elements. Here is the schema file "family.xsd": <?Xml version="1.0" encoding="iso-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" elementformdefault="qualified"> <xs:element name="persons"> <xs:complextype> <xs:sequence> <xs:element name="person" maxoccurs="unbounded"> <xs:complextype> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" minoccurs="0" maxoccurs="5"/> </xs:sequence>
Group indicators
Group indicators are used to define related sets of elements.
Element groups
Element groups are defined with the group declaration, like this: <xs:group name="groupname"> ... </xs:group> You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", that defines a group of elements that must occur in an exact sequence: <xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group> After you have defined a group, you can reference it in another definition, like this: <xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence>
SOA SOLUTIONS, Near S.R Nagar E- Seva
</xs:group> <xs:element name="person" type="personinfo"/> <xs:complextype name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complextype>
Attribute groups
Attribute groups are defined with the attributegroup declaration, like this: <xs:attributegroup name="groupname"> ... </xs:attributegroup> The following example defines an attribute group named "personattrgroup": <xs:attributegroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> </xs:attributegroup> After you have defined an attribute group, you can reference it in another definition, like this: <xs:attributegroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> </xs:attributegroup> <xs:element name="person"> <xs:complextype> <xs:attributegroup ref="personattrgroup"/> </xs:complextype> </xs:element>
</xs:schema> The xml file below (called "myfamily.xml"), uses components from two different schemas; "family.xsd" and "children.xsd": <?Xml version="1.0" encoding="iso-8859-1"?> <persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.microsoft.com family.xsd http://www.w3schools.com children.xsd"> <person> <firstname>hege</firstname> <lastname>refsnes</lastname> <children> <childname>cecilie</childname> </children> </person> <person> <firstname>stale</firstname> <lastname>refsnes</lastname> </person> </persons> The xml file above is valid because the schema "family.xsd" allows us to extend the "person" element with an optional element after the "lastname" element. The <any> and <anyattribute> elements are used to make extensible documents! They allow documents to contain additional elements that are not declared in the main xml schema.
The <anyattribute> element enables us to extend the xml document with attributes not specified by the schema. The following example is a fragment from an xml schema called "family.xsd". It shows a declaration for the "person" element. By using the <anyattribute> element we can add any number of attributes to the "person" element: <xs:element name="person"> <xs:complextype> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:anyattribute/> </xs:complextype> </xs:element> Now we want to extend the "person" element with a "gender" attribute. In this case we can do so, even if the author of the schema above never declared any "gender" attribute. Look at this schema file, called "attribute.xsd": <?Xml version="1.0" encoding="iso-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementformdefault="qualified"> <xs:attribute name="gender"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpletype> </xs:attribute> </xs:schema> The xml file below (called "myfamily.xml"), uses components from two different schemas; "family.xsd" and "attribute.xsd": <?Xml version="1.0" encoding="iso-8859-1"?>
<persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.microsoft.com family.xsd http://www.w3schools.com attribute.xsd"> <person gender="female"> <firstname>hege</firstname> <lastname>refsnes</lastname> </person> <person gender="male"> <firstname>stale</firstname> <lastname>refsnes</lastname> </person> </persons> The xml file above is valid because the schema "family.xsd" allows us to add an attribute to the "person" element. The <any> and <anyattribute> elements are used to make extensible documents! They allow documents to contain additional elements that are not declared in the main xml schema.
Element substitution
Let's say that we have users from two different countries: england and norway. We would like the ability to let the user choose whether he or she would like to use the norwegian element names or the english element names in the xml document. To solve this problem, we could define a substitutiongroup in the xml schema. First, we declare a head element and then we declare the other elements which state that they are substitutable for the head element. <xs:element name="name" type="xs:string"/> <xs:element name="navn" substitutiongroup="name"/>
In the example above, the "name" element is the head element and the "navn" element is substitutable for "name". Look at this fragment of an xml schema: <xs:element name="name" type="xs:string"/> <xs:element name="navn" substitutiongroup="name"/> <xs:complextype name="custinfo"> <xs:sequence> <xs:element ref="name"/> </xs:sequence> </xs:complextype> <xs:element name="customer" type="custinfo"/> <xs:element name="kunde" substitutiongroup="customer"/> A valid xml document (according to the schema above) could look like this: <customer> <name>john smith</name> </customer> Or like this: <kunde> <navn>john smith</navn> </kunde>
<xs:sequence> <xs:element ref="name"/> </xs:sequence> </xs:complextype> <xs:element name="customer" type="custinfo" block="substitution"/> <xs:element name="kunde" substitutiongroup="customer"/> A valid xml document (according to the schema above) looks like this: <customer> <name>john smith</name> </customer> But this is no longer valid: <kunde> <navn>john smith</navn> </kunde>
Using substitutiongroup
The type of the substitutable elements must be the same as, or derived from, the type of the head element. If the type of the substitutable element is the same as the type of the head element you will not have to specify the type of the substitutable element. Note that all elements in the substitutiongroup (the head element and the substitutable elements) must be declared as global elements, otherwise it will not work!
An xsd example
This chapter will demonstrate how to write an xml schema. You will also learn that a schema can be written in different ways.
An xsd example This chapter will demonstrate how to write an xml schema. You will also learn that a schema can be written in different ways. An xml document Let's have a look at this xml document called "shiporder.xml": <?Xml version="1.0" encoding="iso-8859-1"?> <shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:nonamespaceschemalocation="shiporder.xsd"> <orderperson>john smith</orderperson> <shipto> <name>ola nordmann</name> <address>langgt 23</address> <city>4000 stavanger</city> <country>norway</country> </shipto> <item> <title>empire burlesque</title> <note>special edition</note> <quantity>1</quantity> <price>10.90</price> </item> <item> <title>hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item> </shiporder> The xml document above consists of a root element, "shiporder", that contains a required attribute called "orderid". The "shiporder" element contains three different child elements: "orderperson", "shipto" and "item". The "item" element appears twice, and it contains a "title", an optional "note" element, a "quantity", and a "price" element. The line above: xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" tells the xml parser that this document should be validated against a schema. The line: xsi:nonamespaceschemalocation="shiporder.xsd" specifies where the schema resides (here it is in the same folder as "shiporder.xml").
SOA SOLUTIONS, Near S.R Nagar E- Seva
Create an xml schema Now we want to create a schema for the xml document above. We start by opening a new file that we will call "shiporder.xsd". To create the schema we could simply follow the structure in the xml document and define each element as we find it. We will start with the standard xml declaration followed by the xs:schema element that defines a schema: <?Xml version="1.0" encoding="iso-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"> ... </xs:schema> In the schema above we use the standard namespace (xs), and the uri associated with this namespace is the schema language definition, which has the standard value of http://www.w3.org/2001/xmlschema. Next, we have to define the "shiporder" element. This element has an attribute and it contains other elements, therefore we consider it as a complex type. The child elements of the "shiporder" element is surrounded by a xs:sequence element that defines an ordered sequence of sub elements: <xs:element name="shiporder"> <xs:complextype> <xs:sequence> ... </xs:sequence> </xs:complextype> </xs:element> Then we have to define the "orderperson" element as a simple type (because it does not contain any attributes or other elements). The type (xs:string) is prefixed with the namespace prefix associated with xml schema that indicates a predefined schema data type: <xs:element name="orderperson" type="xs:string"/> Next, we have to define two elements that are of the complex type: "shipto" and "item". We start by defining the "shipto" element: <xs:element name="shipto"> <xs:complextype> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complextype> </xs:element> With schemas we can define the number of possible occurrences for an element with the maxoccurs and minoccurs attributes. Maxoccurs specifies the maximum number of occurrences for an element and minoccurs specifies the minimum number of occurrences for an element. The default value for both maxoccurs and minoccurs is 1! Now we can define the "item" element. This element can appear multiple times inside a "shiporder" element. This is specified by setting the maxoccurs attribute of the "item" element to "unbounded" which means that there can be as many occurrences of the "item" element as the author wishes. Notice that the "note" element is optional. We have specified this by setting the minoccurs attribute to zero: <xs:element name="item" maxoccurs="unbounded"> <xs:complextype> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minoccurs="0"/> <xs:element name="quantity" type="xs:positiveinteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complextype> </xs:element> We can now declare the attribute of the "shiporder" element. Since this is a required attribute we specify use="required". Note: the attribute declarations must always come last: <xs:attribute name="orderid" type="xs:string" use="required"/> Here is the complete listing of the schema file called "shiporder.xsd": <?Xml version="1.0" encoding="iso-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:element name="shiporder"> <xs:complextype> <xs:sequence>
<xs:element name="orderperson" type="xs:string"/> <xs:element name="shipto"> <xs:complextype> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complextype> </xs:element> <xs:element name="item" maxoccurs="unbounded"> <xs:complextype> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minoccurs="0"/> <xs:element name="quantity" type="xs:positiveinteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> </xs:complextype> </xs:element> </xs:schema>
Divide the schema The previous design method is very simple, but can be difficult to read and maintain when documents are complex. The next design method is based on defining all elements and attributes first, and then referring to them using the ref attribute. Here is the new design of the schema file ("shiporder.xsd"): <?Xml version="1.0" encoding="iso-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema">
<!-- definition of simple elements --> <xs:element name="orderperson" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string"/> <xs:element name="quantity" type="xs:positiveinteger"/> <xs:element name="price" type="xs:decimal"/> <!-- definition of attributes --> <xs:attribute name="orderid" type="xs:string"/> <!-- definition of complex elements --> <xs:element name="shipto"> <xs:complextype> <xs:sequence> <xs:element ref="name"/> <xs:element ref="address"/> <xs:element ref="city"/> <xs:element ref="country"/> </xs:sequence> </xs:complextype> </xs:element> <xs:element name="item"> <xs:complextype> <xs:sequence> <xs:element ref="title"/> <xs:element ref="note" minoccurs="0"/> <xs:element ref="quantity"/> <xs:element ref="price"/> </xs:sequence> </xs:complextype> </xs:element> <xs:element name="shiporder"> <xs:complextype>
<xs:sequence> <xs:element ref="orderperson"/> <xs:element ref="shipto"/> <xs:element ref="item" maxoccurs="unbounded"/> </xs:sequence> <xs:attribute ref="orderid" use="required"/> </xs:complextype> </xs:element> </xs:schema>
Note: the xml processor will not modify the value if you use the string data type.
<xs:element name="customer" type="xs:normalizedstring"/> An element in your document might look like this: <customer>john smith</customer> Or it might look like this: <customer> john smith </customer>
Note: in the example above the xml processor will replace the tabs with spaces.
Note: in the example above the xml processor will remove the tabs.
Id
A string that represents the id attribute in xml (only used with schema attributes) A string that represents the idref attribute in xml (only used with schema attributes)
Idref
Idrefs Language Name Ncname Nmtoken A string that represents the nmtoken attribute in xml (only used with schema attributes) A string that contains a valid language id A string that contains a valid xml name
Nmtokens Normalizedstring Qname String Token A string A string that does not contain line feeds, carriage returns, tabs, leading or trailing spaces, or multiple spaces A string that does not contain line feeds, carriage returns, or tabs
Enumeration Length Maxlength Minlength Pattern (nmtokens, idrefs, and entities cannot use this constraint) Whitespace
Date and time data types are used for values that contain date and time.
Yyyy indicates the year Mm indicates the month Dd indicates the day
Note: all components are required! The following is an example of a date declaration in a schema: <xs:element name="start" type="xs:date"/> An element in your document might look like this: <start>2002-09-24</start> Time zones To specify a time zone, you can either enter a date in utc time by adding a "z" behind the date like this: <start>2002-09-24z</start> Or you can specify an offset from the utc time by adding a positive or negative time behind the date - like this: <start>2002-09-24-06:00</start> or <start>2002-09-24+06:00</start>
y y y
Note: all components are required! The following is an example of a time declaration in a schema: <xs:element name="start" type="xs:time"/> An element in your document might look like this: <start>09:00:00</start> Or it might look like this: <start>09:30:10.5</start> Time zones To specify a time zone, you can either enter a time in utc time by adding a "z" behind the time like this: <start>09:30:10z</start> Or you can specify an offset from the utc time by adding a positive or negative time behind the time - like this: <start>09:30:10-06:00</start> or <start>09:30:10+06:00</start>
Yyyy indicates the year Mm indicates the month Dd indicates the day T indicates the start of the required time section Hh indicates the hour Mm indicates the minute Ss indicates the second
Note: all components are required! The following is an example of a datetime declaration in a schema: <xs:element name="startdate" type="xs:datetime"/> An element in your document might look like this: <startdate>2002-05-30t09:00:00</startdate> Or it might look like this: <startdate>2002-05-30t09:30:10.5</startdate> Time zones To specify a time zone, you can either enter a datetime in utc time by adding a "z" behind the time - like this: <startdate>2002-05-30t09:30:10z</startdate> Or you can specify an offset from the utc time by adding a positive or negative time behind the time - like this: <startdate>2002-05-30t09:30:10-06:00</startdate> or <startdate>2002-05-30t09:30:10+06:00</startdate>
P indicates the period (required) Ny indicates the number of years Nm indicates the number of months Nd indicates the number of days T indicates the start of a time section (required if you are going to specify hours, minutes, or seconds) Nh indicates the number of hours
y y
The following is an example of a duration declaration in a schema: <xs:element name="period" type="xs:duration"/> An element in your document might look like this: <period>p5y</period> The example above indicates a period of five years. Or it might look like this: <period>p5y2m10d</period> The example above indicates a period of five years, two months, and 10 days. Or it might look like this: <period>p5y2m10dt15h</period> The example above indicates a period of five years, two months, 10 days, and 15 hours. Or it might look like this: <period>pt15h</period> The example above indicates a period of 15 hours. Negative duration To specify a negative duration, enter a minus sign before the p: <period>-p10d</period> The example above indicates a period of minus 10 days.
Defines a date value Defines a date and time value Defines a time interval Defines a part of a date - the day (dd) Defines a part of a date - the month (mm) Defines a part of a date - the month and day (mm-dd) Defines a part of a date - the year (yyyy) Defines a part of a date - the year and month (yyyy-mm) Defines a time value
An element in your document might look like this: <prize>999.50</prize> Or it might look like this: <prize>+999.5450</prize> Or it might look like this: <prize>-999.5230</prize> Or it might look like this: <prize>0</prize> Or it might look like this: <prize>14</prize> Note: the maximum number of decimal digits you can specify is 18.
y y y y y y y y y
Enumeration (a boolean data type cannot use this constraint) Length (a boolean data type cannot use this constraint) Maxlength (a boolean data type cannot use this constraint)
y y y
Minlength (a boolean data type cannot use this constraint) Pattern Whitespace
Xslt
Xsl stands for extensible stylesheet language, and is a style sheet language for xml documents. Xslt stands for xsl transformations. In this tutorial you will learn how to use xslt to transform xml documents into other formats, like xhtml.
Xsl languages
It started with xsl and ended up with xslt, xpath, and xsl-fo.
Xslt - a language for transforming xml documents Xpath - a language for navigating in xml documents Xsl-fo - a language for formatting xml documents
Xslt introduction
Xslt is a language for transforming xml documents into xhtml documents or to other xml documents. Xpath is a language for navigating in xml documents.
What is xslt?
y y y y y
Xslt stands for xsl transformations Xslt is the most important part of xsl Xslt transforms an xml document into another xml document Xslt uses xpath to navigate in xml documents Xslt is a w3c recommendation
Xslt transformation
Example study: how to transform xml into xhtml using xslt. The details of this example will be explained in the next chapter.
The xmlns:xsl="http://www.w3.org/1999/xsl/transform" points to the official w3c xslt namespace. If you use this namespace, you must also include the attribute version="1.0".
<?Xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <body> <h2>my cd collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>title</th> <th>artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
</cd> . . </catalog> If you have an xslt compliant browser it will nicely transform your xml into xhtml.
Example explained
Since an xsl style sheet is an xml document, it always begins with the xml declaration: <?xml version="1.0" encoding="iso-8859-1"?>. The next element, <xsl:stylesheet>, defines that this document is an xslt style sheet document (along with the version number and xslt namespace attributes). The <xsl:template> element defines a template. The match="/" attribute associates the template with the root of the xml source document. The content inside the <xsl:template> element defines some html to write to the output. The last two lines define the end of the template and the end of the style sheet. The result from this example was a little disappointing, because no data was copied from the xml document to the output. In the next chapter you will learn how to use the <xsl:value-of> element to select values from the xml elements
<body> <h2>my cd collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>title</th> <th>artist</th> </tr> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet>
<xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Note: the value of the select attribute is an xpath expression. An xpath expression works like navigating a file system; where a forward slash (/) selects subdirectories.
Take a look at the adjusted xsl style sheet: Example <?Xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <body>
<h2>my cd collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>title</th> <th>artist</th> </tr> <xsl:for-each select="catalog/cd[artist='bob dylan']"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
<th>artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
The <xsl:if> element is used to put a conditional test against the content of the xml file.
Where to put the <xsl:if> element To add a conditional test, add the <xsl:if> element inside the <xsl:for-each> element in the xsl file: Example <?Xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html>
SOA SOLUTIONS, Near S.R Nagar E- Seva
<body> <h2>my cd collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>title</th> <th>artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
Example <?Xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <body> <h2>my cd collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>title</th> <th>artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template>
</xsl:stylesheet>
<xsl:template match="artist"> artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /> </xsl:template> </xsl:stylesheet>
Xpath
Xpath is used to navigate through elements and attributes in an xml document. Xpath is a major element in w3c's xslt standard - and xquery and xpointer are both built on xpath expressions. Xpath is a language for finding information in an xml document.
Xpath syntax
Xpath uses path expressions to select nodes or node-sets in an xml document. The node is selected by following a path or steps.
Xpath examples
Let's try to learn some basic xpath syntax by looking at some examples.
<book category="web"> <title lang="en">learning xml</title> <author>erik t. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
Selecting nodes
Unfortunately, there are different ways of dealing with xpath in internet explorer and other browsers. In our examples we have included code that should work with most major browsers. Internet explorer uses the selectnodes() method to select nodes from the xml document:
Xmldoc.selectnodes(xpath);
Firefox, chrome, opera and safari use the evaluate() method to select nodes from the xml document:
Xmldoc.evaluate(xpath, xmldoc, null, xpathresult.any_type,null);
The following example selects the title of the first book node under the bookstore element:
Example /bookstore/book[1]/title there is a problem with this. The example above shows different results in ie and other browsers.
Ie5 and later has implemented that [0] should be the first node, but according to the w3c standard it should have been [1]!!
A workaround!
To solve the [0] and [1] problem in ie5+, you can set the selectionlanguage to xpath. The following example selects the title of the first book node under the bookstore element:
Example
Xml.setproperty("selectionlanguage","xpath"); xml.selectnodes("/bookstore/book[1]/title");
The following example selects the text from all the price nodes:
Example
/bookstore/book/price/text() Select price nodes with price>35
The following example selects all the price nodes with a price higher than 35:
Example
/bookstore/book[price>35]/price
Example
/bookstore/book[price>35]/title
Xquery
Xquery is to xml what sql is to database tables. Xquery was designed to query xml data.
Introduction to xquery
Xquery is to xml what sql is to database tables. Xquery is designed to query xml data - not just xml files, but anything that can appear as xml, including databases.
Xquery example
Let's try to learn some basic xquery syntax by looking at an example.
<price>30.00</price> </book> <book category="children"> <title lang="en">harry potter</title> <author>j k. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">xquery kick start</title> <author>james mcgovern</author> <author>per bothner</author> <author>kurt cagle</author> <author>james linn</author> <author>vaidyanathan nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web"> <title lang="en">learning xml</title> <author>erik t. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
Path expressions Xquery uses path expressions to navigate through elements in an xml document. The following path expression is used to select all the title elements in the "books.xml" file:
SOA SOLUTIONS, Near S.R Nagar E- Seva
Doc("books.xml")/bookstore/book/title
(/bookstore selects the bookstore element, /book selects all the book elements under the bookstore element, and /title selects all the title elements under each book element) The xquery above will extract the following:
<title lang="en">everyday italian</title> <title lang="en">harry potter</title> <title lang="en">xquery kick start</title> <title lang="en">learning xml</title>
Predicates Xquery uses predicates to limit the extracted data from xml documents. The following predicate is used to select all the book elements under the bookstore element that have a price element with a value that is less than 30:
Doc("books.xml")/bookstore/book[price<30]
Xquery terms
In xquery, there are seven kinds of nodes: element, attribute, text, namespace, processinginstruction, comment, and document (root) nodes.
Xquery terminology
Nodes In xquery, there are seven kinds of nodes: element, attribute, text, namespace, processinginstruction, comment, and document (root) nodes. Xml documents are treated as trees of nodes. The root of the tree is called the document node (or root node). Look at the following xml document:
<?Xml version="1.0" encoding="iso-8859-1"?> <bookstore> <book> <title lang="en">harry potter</title> <author>j k. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> Example of nodes in the xml document above: <bookstore> (document node) <author>j k. Rowling</author> (element node) lang="en" (attribute node) Atomic values Atomic values are nodes with no children or parent. Example of atomic values:
J k. Rowling "en"
Items
Items are atomic values or nodes.
<book> <title>harry potter</title> <author>j k. Rowling</author> <year>2005</year> <price>29.99</price> </book> Children Element nodes may have zero, one or more children. In the following example; the title, author, year, and price elements are all children of the book element: <book> <title>harry potter</title> <author>j k. Rowling</author> <year>2005</year> <price>29.99</price> </book> Siblings Nodes that have the same parent. In the following example; the title, author, year, and price elements are all siblings: <book> <title>harry potter</title> <author>j k. Rowling</author> <year>2005</year> <price>29.99</price> </book> Ancestors A node's parent, parent's parent, etc. In the following example; the ancestors of the title element are the book element and the bookstore element: <bookstore> <book>
SOA SOLUTIONS, Near S.R Nagar E- Seva
<title>harry potter</title> <author>j k. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> Descendants A node's children, children's children, etc. In the following example; descendants of the bookstore element are the book, title, author, year, and price elements: <bookstore> <book> <title>harry potter</title> <author>j k. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> Xquery syntax Xquery is case-sensitive and xquery elements, attributes, and variables must be valid xml names.
Xquery is case-sensitive Xquery elements, attributes, and variables must be valid xml names An xquery string value can be in single or double quotes An xquery variable is defined with a $ followed by a name, e.g. $bookstore Xquery comments are delimited by (: and :), e.g. (: xquery comment :)
Look at the following example: For $x in doc("books.xml")/bookstore/book return if ($x/@category="children") then <child>{data($x/title)}</child> else <adult>{data($x/title)}</adult> Notes on the "if-then-else" syntax: parentheses around the if expression are required. Else is required, but it can be just else (). The result of the example above will be: <adult>everyday italian</adult> <child>harry potter</child> <adult>learning xml</adult> <adult>xquery kick start</adult> Xquery comparisons In xquery there are two ways of comparing values. 1. General comparisons: =, !=, <, <=, >, >= 2. Value comparisons: eq, ne, lt, le, gt, ge The difference between the two comparison methods are shown below. The following expression returns true if any q attributes have a value greater than 10: $bookstore//book/@q > 10 The following expression returns true if there is only one q attribute returned by the expression, and its value is greater than 10. If more than one q is returned, an error occurs.