XSLT and XPath(Ch.8) - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

XSLT and XPath(Ch.8)

Description:

In the DOM. Every node has a nodeValue property. The nodeValue of an element node is null ... root of the tree (corresponding to the Document node in the DOM) ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 29
Provided by: pike5
Category:
Tags: xslt | dom | xpath

less

Transcript and Presenter's Notes

Title: XSLT and XPath(Ch.8)


1
XSLT and XPath(Ch.8)
  • PIKE Lab.
  • ?? 2??
  • ???

2
Contents
  • Overview of the XSLT
  • XPath query language
  • The role of template rules
  • Match patterns
  • The top-level elements
  • Some working examples
  • Summary
  • References

3
What is XSLT?
  • eXtensible Stylesheet Language Transformation
  • A high-level language for defining XML
    transformation
  • A versatile tool applied in many different ways

input
output
4
Transformation Process
5
Transformation Process
  • An XML parser takes the source XML document and
    turns it into a tree representation
  • The XSLT processor transforms this tree into
    another tree
  • A serializer takes the result tree and turns it
    into a XML document

6
XSLT as a Programming Language
  • XML Syntax
  • XSLT stylesheet XML document
  • Use the angle brackets and tags of XML

In a XML language
In a conventional language
7
XSLT as a Programming Language
  • Rule-based
  • The rules of XSLT Template rules
  • ltxsltemplategt elements in the stylesheet
  • Each rule has a match pattern template body
  • Example of a simple template rule

ltxsltemplate matchpricegt
ltbgtltxslvalue-of selectformat-number(. ,
0.00) /gtlt/bgt lt/xsltemplategt
  • The pattern is price, which matches any
    ltpricegtelement.
  • To output the value of the ltpricegt element,
    formatted as a number with two decimal digits

8
XSLT as a Programming Language
  • XPath Queries
  • To fetch the data you need from the source
    document
  • Cf.) to describe the logic of the transformation
    process
  • ? use XSLT itself
  • The syntax of XPath is not at all like SQL
  • Designed to process hierarchic data(trees)
  • Example XPath expressions

/invoice/billing-address/postcode . .
/_at_title /book/chapter3/section2/para1
9
XSLT as a Programming Language
  • Functional Programming
  • XSLT instructions can be executed in any order
  • Not depend on how the previous node was handled
  • Each piece of the output is defined as a function
    of a particular piece of the input
  • Data Type
  • Defined by the query language, XPath
  • Node-set
  • like a mathematical set ( no intrinsic order,
    no duplicates )
  • Character strings, numbers(0,1..), Boolean
    values(T/F)
  • Variables
  • write-once variables, just names for values
  • hold the result of any XPath query
  • ? node-set, string, number,Boolean

10
XSLT as a Programming Language
  • Examples of variables of each kind

ltxslvariable namex select//item
/gt ltxslvariable namey selectcount(_at_)
/gt ltxslvariable namez select_at_typeT
/gt ltxslvariable nametreegt lttablegt lttrgt lttd
gt ltimg srcbutton1.gif /gt lt/tdgt lttdgt
ltimg srcbutton2.gif /gt lt/tdgt lt/trgt lt/tablegt
lt/xslvariablegt
  • XSLT
  • is a
  • dynamically
  • typed language
  • Automatically
  • conversion

11
The XPath Data Model
  • The tree structure in XSLT XPath is similar in
    many ways to the DOM
  • Difference
  • In the DOM
  • Every node has a nodeValue property
  • The nodeValue of an element node is null
  • In the XSLT
  • Every node has a string-value property
  • The string-value of an element node is the
    concatenation of all its descendant text nodes

12
The XPath Data Model
  • Seven types of node in an XSLT tree
  • Root represent the root of the tree
    (corresponding to the Document node in the DOM)
  • Element each element node corresponds to an
    element in the source document
  • Attribute cf.) xmlns attributes
  • Text represent the textual content of the
    document
  • Processing Instruction written as
    lt?targetdata?gt
  • Comment
  • Namespace

13
The XPath Data Model
  • Names Namespaces
  • Element or attribute name
  • namespace prefix local name ? qualified
    name(Qname)
  • Ex) Qname foblock ? fo prefix, block
    local name
  • The namespace declaration defines a namespace URI
  • Ex) ltfoblockgt ? xmlnsfohttp//www.w3.org.XSL/
  • In the XML rules, the expanded names of the
    elements and attributes are different

lttemplate matchpara xmlnshttp//www.w3.org/19
99/XSL/Transform/gt
lttemplate matchmypara xmlnshttp//www.w3.
org/1999/XSL/Transform Xmlnsmyurnsome-namesp
ace /gt
14
XPath Expressions
  • Context
  • The result of an XPath expression may depend on
    the context in which it is used
  • Context node the node on the source tree for
    which the expression is being evaluated ( access
    by . )
  • Context position the position of that node in
    the list of nodes being processed ( access by
    position() )
  • Context size the number of nodes in that list(
    access by last() )
  • Others from the containing XSLT stylesheet

15
XPath Expressions
  • Primaries
  • The basic building blocks of an XPath expression

Construct Examples
String literal London Paris
Number 12 0.0001 5000000
Variable Reference x myvariable-name
Function call Position() Contains(x, ) Msextension(Bill, Gates)
Parenthesized expression 3 (x 1) (//item)1
16
XPath Expressions
  • Operators be familiar but not all of them
  • Comparing Node-set
  • Either or both of the operands is a node-set when
    you use the comparison operators ( , !, lt, gt,
    lt, gt)
  • Example,
  • //_at_secureyes with the name secure and the
    value yes
  • //_at_secure!yes with the name secure a value
    other than yes
  • //author//artist with the same string-value
  • Exceptions with an empty set, !, ./

17
Location Paths
  • The construct gave the language its name with
    abbreviations
  • Syntax Rules for Location Paths
  • Step
  • Defines a route through the tree representation
    of the source document
  • 3 components Axis, Node test, Predicate
  • Axis-name ltltgtgt node-test ( ltltgtgt predicate
    ltltgtgt )

/ Selects the root node
/ step Ex) /comment() Selects nodes that can be reached from the root by the step
E / step Selects nodes that can be reached from nodes in E by the step
Step Ex) descendantfigure Selects nodes that can be reached from the context node by the step
18
Location Paths
  • Axis
  • defines the relationship of the required nodes
    to the starting nodes ( ancestor, child,
    following-sibling )
  • Node test
  • defines the type of nodes and the names of the
    nodes that are to be selected
  • Predicates
  • expressions that further restrict the set of
    nodes selected by the step , like a filter on the
    node-set
  • Cf.) In a filter expression, the nodes are
    considered in forward document order, while in
    a step, the nodes are taken in axis order

19
Location Paths
  • Abbreviation
  • use some shortcut notations for location paths
  • . selfnode() ? context node itself
  • Ex) ltparagt ? ltxslif testselfparagt
  • . . parentnode()
  • _at_ attribute ? to indicate the attribute
    axis
  • // /descendant-or-selfnode()
  • ? Search for all the descendants of a node
  • Child axis is the default axis ? child
  • Ex) /section/item ? /childsection/childitem

20
XPath Functions
Boolean(arg1) Ceiling(arg1) Concat(arg1, arg2, ) Contains(arg1, agr2) Count(arg1) Current() Document(arg1,arg2) Element-available(arg1) False() Floor(arg1) Format-number(arg1, arg2, arg3) Function-available(arg1) Generate-id(arg1) Id(arg1) Key(arg1, arg2) Lang(arg1) Last() Local-name(arg1) name(arg1) Namespace-uri(arg1) Normalize-space(arg1) Not(arg1) Number(arg1) Position() Round(arg1) Start-with(arg1, arg2) String(arg1) String-length(arg1) Substring(arg1, arg2, arg3) Substring-after(arg1, arg2) Substring-before(arg1, arg2) Sum(arg1) System-property(arg1) Translate(arg1, arg2, arg3) True() Unparsed-entity-uri(arg1)
21
Sylesheets, Tmpaltes, Patterns
  • ltxslstylesheetgt element
  • ltxsltemplategt element like the procedures
    functions

ltxslstylesheet xmlnsxslhttp//www.w3.org/1999
/XSL/Tramsform version1.0gt lt/xslstylesheetgt
ltxsltemplate matchauthorgt ltparagtby
ltxslvalue-of select . /gtlt/paragt lt/xsltemplat
egt
22
A example of transformation
  • Steps
  • Download the sample files from http//www.wrox.com
  • Download Instant Saxon
  • from http//users.iclway.co.uk/mhkay/saxon
  • Extract the executable saxon.exe
  • into a suitable directory, say c\saxon
  • Open an MS-DOS console
  • Change directory, and then run the processor
  • C\saxon\saxon.exe product.xml product.xsl

lt?xml version"1.0"?gt lt?xml-stylesheet
type"text/xsl" href"products.xsl"?gt ltproductsgt lt
product code"Z123-888" category"tools"gt
ltdescriptiongtLarge claw hammerlt/descriptiongt
ltweight units"gms"gt850lt/weightgt
ltpricegt12.99lt/pricegt lt/productgt ltproduct
code"X853-122" category"books"gt
lttitlegtPlumbing for beginnerslt/titlegt
ltISBNgt0-123-456-9876lt/ISBNgt ltpricegt10.95lt/price
gt lt/productgt ltproduct code"S14-8532"
category"tools"gt ltdescriptiongtAdjustable
spannerlt/descriptiongt ltweight
units"gms"gt330lt/weightgt ltpricegt5.25lt/pricegt lt/
productgt lt/productsgt
lt?xml version"1.0"?gt ltxslstylesheet
xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
version"1.0"gt ltxsloutput method"xml"/gt ltxsltem
plate match""gt ltxslcopygt ltxslcopy-of
select"_at_"/gt ltxslapply-templates/gt lt/xslcopygt
lt/xsltemplategt ltxsltemplate
match"productprice lt 10.00"/gt ltxsltemplate
match"price"/gt lt/xslstylesheetgt
23
The contents of a template
  • Contents of anltxsltemplategt form a template body
  • Attribute value templates
  • Parameterize the value by XPath expressions
    within curly brackets
  • ltltnamedescriptiongtgt ? nameprefixlocalna
    me
  • Restricted allowed in only a few specific
    places
  • Ex) ltxslcall-template nametnamegt wont
    work
  • XSLT instructions a subset of XSLT elements

ltxslapply-importsgt ltxslapply-templatesgt ltxslattributegt ltxslcall-tempaltegt ltxslchoosegt ltxslcommentgt ltxslcopygt ltxslcopy-ofgt ltxslelementgt ltxslfallbackgt ltxslfor-eachgt ltxslifgt ltxslmessagegt ltxslnumbergt ltxslltprocessing-instructiongt ltxsltextgt ltxslvalue-ofgt ltxslvariablegt
24
The contents of a template
  • Literal Result Elements
  • Any element found within a template body that is
    not recognized as an instruction
  • Copied to the result tree with its attributes
    namespaces
  • Ex) A literal lttdgt result element
  • lttd valigntopgtltxslvalue-of select.
    /gtlt/tdgt
  • Generated using XPath expressions
  • use ltxslattributegt or xsluse-attribute-sets
    attributes
  • Sorting by specifying ltxslsortgt elements
  • ltxslsortgt element specifies a sort key and has a
    number of attributes to control sorting

25
Top-level Elements
  • Can be used as children of the ltxslstylesheetgt
    or ltxsltransformgt element
  • Appear in any order, except ltxslimportgt elements
    have to come before any others

ltxslattribute-setgt ltxsldecimal-formatgt ltxslimportgt ltxslincludegt ltxslkeygt ltxslnamespace-aliasgt ltxsloutputgt ltxslparamgt ltxslpreserve-spacegt ltxslstrip-spacegt ltxsltemplategt ltxslvariablegt
26
Example displaying soccer results
  • Source soccer.xml
  • The first stylesheet soccer1.xsl
  • Run
  • C\saxon\saxon.exe soccer.xml soccer1.xsl

27
summary
  • XSLT as transformation language for XML documents
  • XPath queries within an XSLT stylesheet
  • How XSLT SQL database can work together
  • ? an application gets XML out of the database,
    and then puts that XML through XSLT stylesheet
    for display

28
references
  • Microsoft(MSXML3)
  • http//msdn.microsoft.com/xml
  • Oracle(Oracle XML parser)
  • http//technet.orcale.com/
  • Saxon
  • http//users.iclway.co.uk/mhkay/saxon
  • Xalan
  • http//xml.apache.org./xalan/overview.html
  • Http//www.xslinfo.com
Write a Comment
User Comments (0)
About PowerShow.com