XSLT & XPATH. From Friday Everything in XML is machine readable Attributes describe elements, so...
-
date post
19-Dec-2015 -
Category
Documents
-
view
227 -
download
0
Transcript of XSLT & XPATH. From Friday Everything in XML is machine readable Attributes describe elements, so...
From Friday
• Everything in XML is machine readable• Attributes describe elements, so does an
element’s place in the tree (context) • XML must be
– Well formed (following XML syntax rules)– Valid (following the rules of the specified DTD[s])
Using Meta Data in Decisions
Or
Why bother with structured markup?
facilitate (re)usability by others
Down Stream Uses of Structure
Currently, FB guesses at what the ‘featured’ image might be when youPost a link. Wouldn’t it be better if tumblr told FB?
Provenance
Provenance :: where something came from… wouldn’t be nice if tumblr style provenance Was inter-system
XSLT & XPATH
• XPATH = XML query language• XSLT = Translate XML from one schema to
another – Extensible Stylesheet Language Transformations
• XSLT is nothing without XPATH
Get an RSS File
• Example: nytimes.com• Click on RSS button
– (see the rss being transformed into HTML by the browser)
• View Source
XPATH Selectors
// selects the root element
channelselects the channel element and all of its children
//channel/title / moves down the tree and selects the title element
XPATH Selectors
title/.. selects channel
/. selects the current element (if you're in a loop or something)
XPATH Selectors (attributes)
@ selects nodes attributes
channel/link/@type selects the type attribute in link
XPATH Conditionals
go in []
//channel/item[1] selects the first item
[last()]
[position()<15]
//a[@href="http://www.rpi.edu"]
XPATH Operators & Comparators
+ - * div = < > >= <= or and mod
| is not OR it is used to concatenate sets of elements
XPATH functions
• position()• last()• abs()• floor()• string-length()• name()• http://www.w3.org/TR/xpath-functions/ • http://www.w3schools.com/Xpath/
xpath_functions.asp
XPATH
• Get, run, files from:– http://www.rpi.edu/~gillw3/websys/xslt/files/
• (inspect both, open the RSS file in your browser)
XSLT
• Where does XSLT happen?– server side:
http://us2.php.net/manual/en/book.xsl.php– Client side: FF / Safari / IE
add to the top of your xml document
<?xml-stylesheet type="text/xsl" href="trasnform.xsl"?>
XSLT
• Use XPATH to select (and display) element values
• XSLT value-of• <xsl:value-of select=" XPATH HERE "/>• <xsl:value-of select="channel/item[1]/title"/>
XSLT Choose
<xsl:choose> <xsl:when test=“XPATH"> output
</xsl:when> <xsl:otherwise>
output </xsl:otherwise> </xsl:choose>
May stack many tests / conditions
XSLT for-each
<xsl:for-each select="xpath here">
<xsl:for-each select="channel/item/"> <li> <xsl:value-of select="title"/> </li></xsl:for-each>
XSLT Sort
<xsl:sort select="title"/><xsl:for-each select="channel/item/"> <xsl:sort select="title"/> <xsl:value-of select="title"/></xsl:for-each>
• Datatype & order attributes:http://www.xml.com/pub/a/2002/07/03/transform.html
XSLT Templates
• Template is a container for a chunk of XSLT• XPATH inside of the template starts where the
match attribute ends
<xsl:template match=“XPATH"> stuff: output & control</xsl:template>
XSLT Templates
• Templates As Functions
<xsl:template match=“channel/item"> <xsl:call-template name=“item-display"/>
<xsl:param name="title"> value </xsl:param>
</xsl:call-template></xsl:template>