XSLT & XPATH 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]) Your resume should look like: Question • How can copyright & attribution information be maintained in mashups? Using Meta Data in Decisions Or Why bother with structured markup? facilitate (re)usability by others Image an Article: Title creator Featured image? body HTML RDFa • Resource Description Framework in [xhtml] attributes Down Stream Uses of Structure Currently, FB guesses at what the ‘featured’ image might be when you Post a link. Wouldn’t it be better if tumblr told FB? Down Stream Uses of Structure Provenance Provenance :: where something came from … wouldn’t be nice if tumblr style provenance Was inter-system Question • How can copyright & attribution information be maintained in mashups? XSLT & XPATH • XPATH = XML query language • XSLT = Translate XML from one schema to another – Extensible Stylesheet Language Transformations • XSLT is nothing without XPATH XPATH • • • • XSLT CSS (sorta) jQuery (sorta) PHP SimpleXML 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 channel selects 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_functio ns.asp XPATH • Get, run, files from: – http://www.rpi.edu/~gillw3/websys/xslt/files/ • (inspect both, open the RSS file in your browser) XSLT • XSL is XML • Transforms XML into XML • We'll transform XML into XHTML 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 Control Structures • • • • • If Choose / when / otherwise for-each sort templates XSLT if <xsl:if test="creator=‘someone’"> <li> <xsl:value-of select=“title"/> </li> </xsl:if> 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> In Class • Grab this XML feed: http://events.rpi.edu/webcache/v1.0/xmlDays /3/list-xml/no--filter.xml • Using XPATH and XSLT: – Print out a list of events from the feed – Put the summary in an <H3> – Put the description in a <P> – Include a human readable start time & date