PREV
XML XSLT:
- Transformation is a process of taking an XML document and turning it into a document in another form
- The process of applying rules to the source document is called transformation
- XSLT is a declarative language
- Programmer has control over the general course of transformation but not on the step-by-step processing of the XSLT processor
- XSLT stylesheet consists of a series of templates
- A template consists of an expression describing the form of some XML fragment and one or more elements specifying what output to generate when the form is matched in the source document.
- Each stylesheet contains a template that matches the root of a document – root template. The XSLT processor begins from this template
- XSLT works its way through the structures in the source document and the context is the node of the parse tree on which the processor if focussing
- An XSLT processor will start from the document element (root node in the tree) but the context changes throughout the course of processing
- Node tests: comment(), text(), node(), processing-instruction()
- XSLT has 37 elements and attributes
- Commonly used elements are categorized as
- Building stylesheets
- Controlling transformation output
- Generating simple output
- Generating XML
- Iteration
- Conditional tests
- Programming constructs
- Modularity
- Building stylesheets:
or
: This is the top-most element. This element must have two attributes: namespace and version
: This element has four optional attributes – match, mode, priority, name.
: Two child elements of this element are –
and
. If this element is used with no attributes, it tells the processor to match the templates in the stylesheet against the current context node.
- Controlling transformation output
attribute is – “elements”
attribute is – “elements”
This element has 10 optional attributes – method, version, encoding, standalone, indent, omit-xml-declaration, doctype-public, doctype-system, media-type, cdata-section-elements
is an empty element and is the child of the parent element –
- Generating simple output:
element. This element has an attribute – “select” (required attribute). Value of the “select” attribute is an XPath expression. If the expression returns a collection of nodes, only first node is returned to the output.
- Making copies:
and
elements.
element copies the current context node to the output. It does not bring along any attributes or child nodes.
element copies current context node and all the nodes under it.
- Generating XML constructs directly: Five elements for creating major constructs of XML are:
- Iteration:
element. This element operates on a collection of nodes designated by the value of its “select” attribute. While the element is executed, the nodes in the collection become the current context node.
- Conditional tests:
element. This element does not have any “else” part. This element takes one attribute – “test” which is an XPAth expression
element. This element has two child elements:
and
has no attributes.
is the default part of the choose statement.
- Programming constructs:
- Declaring Variables:
element This element has one required attribute – “name” It has one optional attribute – “select” (value of this attribute is an XPath expression) An XSLT variable cannot have its value changed (XSLT is free of sede-effects) Variables can be local or global
- Calling templates: A named template uses
element to declare parameters and optionally give them the default values It is not an error to call a template without the required parameters, although processing may be affected Parameters can be local or global Templates can be called from
or
elements
- Declaring Variables:
- Modularity:
and
elements These elements have one required attribute: “href” Both are child elements of
Both are empty elements Stylesheet included by
has same priority as native template Templates imported by
have lower priority To apply both native and imported templates, use
statement
- Some things that CSS cannot do:
- CSS cannot do computations
- CSS cannot change the order of elements
- CSS cannot combine multiple documents
- XSLT
- XSLT stylesheet should be an XML document
- XSLT is based on pattern-matching
- XSLT is free of side effects
- XSLT uses iteration and recursion instead of loops
- Scenarios where XSLT is preferable:
- Information is to be rendered on different devices (web, handheld-devices etc)
- To exchange data between different partners who use different database systems
- When redesign of the site (say) is done very frequently
- To combine documents that are in different formats
- XSLT stylesheets use namespace prefixes to process the XML elements they contain. Anything that doesnt use the xsl namespace prefix is not processed, its written to the result tree.
- DOM, XSLT and XPath all use tree structures to represent data from an XML document.
- Stylesheet is itself an XML document
- The nodes to be processed are represented by context. Initially the context is the root of the XML document but it changes throughout the stylesheet
- Process of transformation is:
- XSLT stylesheet is parsed and converted into a tree structure
- XML document is parsed and converted into a tree structure
- XSLT processor is now at the root of the XML document
matches the document root
- Process begins inside xsl:template where the element is matched with the value of “match” attribute
- Built-in templates have lower priority than any other templates.
- Following are built-in templates:
- for element and root nodes –
- for modes –
- for text and attribute nodes –
- for comment and processing-instruction nodes –
This does not do anything
- for namespace nodes –
This does not do anything.
- for element and root nodes –
- Top level elements – elements whose parent is
element
and
and
- Other elements are:
, and
can appear only at the beginning of a stylesheet, while
can appear anywhere
- XSLT uses XPath expression in the “match” and “select” attributes
- XPath works with parsed version of the XML document, means all the entities are resolved first.
- XSLT processor handles all
elements before it does anything and then it processes
element
- Attributes of
element are:
- Select
- Data-type : It can be text, number or QName
- Order : It can be ascending, descending
- Case-order : It can be upper-first, lower-first. This attribute is used only when data-type=”text”
- lang
element can be used within
or
elements.
Post Your Thoughts