170 likes | 337 Views
Effective XSLT. Making use of XSLT to better customize your data. Daniel Hazelbaker Information Technology Manager High Desert Church Email: daniel@highdesertchurch.com. What is XSLT?.
E N D
Effective XSLT Making use of XSLT to better customize your data. Daniel Hazelbaker Information Technology Manager High Desert Church Email: daniel@highdesertchurch.com
What is XSLT? • XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML. • XSLT uses XPath to reference the elements inside the XML document.
Common Element Functions • <xsl:attribute /> • <xsl:text /> • <xsl:value-of /> • <xsl:copy />
Full Element Function List • <xsl:decimal-format /> • <xsl:key /> • <xsl:choose /> • <xsl:if /> • <xsl:otherwise /> • <xsl:when /> • <xsl:attribute /> • <xsl:attribute-set /> • <xsl:comment /> • <xsl:copy /> • <xsl:element /> • <xsl:namespace-alias /> • <xsl:number /> • <xsl:processing-instruction /> • <xsl:text /> • <xsl:value-of /> • <xsl:preserve-space /> • <xsl:strip-space /> • <xsl:fallback /> • <xsl:message /> • <xsl:call-template /> • <xsl:output /> • <xsl:for-each /> • <xsl:sort /> • <xsl:import /> • <xsl:include /> • <xsl:stylesheet /> • <xsl:transform /> • <xsl:apply-imports /> • <xsl:template /> • <xsl:copy-of /> • <xsl:param /> • <xsl:variable /> • <xsl:with-param />
XSLT/XPath Basics • “group” • All node elements whose name is “group” • “@name” • The “name” attribute of the current node • “group[@active = 1]” • All node elements whose name is “group” and has an attribute called “active” whose value is 1.
XSLT/XPath Basics (cont.) • “group/member” • All node elements whose name is “member” and whose parent node name is “group” (that is, all group members). • “group[@active = 1]/member[@firstname= ‘Daniel’]” • All small group members whose first name is “Daniel” and is a member of a currently active group. • “group[@active = 1 and contains(@name, ‘Victorville’)]” • All small groups which are active and whose name contains the word “Victorville”
Using ‘if’ statements <xsl:iftest="@meetingstarttime != '12:00 AM'"> <xsl:text> </xsl:text> <xsl:value-ofselect="@meetingstarttime"/> </xsl:if>
Using ‘choose’ statements <xsl:choose> <xsl:whentest="@status = 'Green'"> <imgsrc="ok.png"alt="" /> </xsl:when> <xsl:whentest="'@status = 'Red'"> <imgsrc="error.png"alt="" /> </xsl:when> <xsl:otherwise> <imgsrc="unknown.png"alt="" /> </xsl:otherwise> <xsl:choose>
Using Parameters <xsl:paramname="detailsAsPopup">no</xsl:param> <xsl:iftest="$detailsAsPopup = 'yes'"> <xsl:textdisable-output-escaping="yes"><![CDATA[ <script type="text/javascript"> ... </script> ]]> </xsl:text> </xsl:if>
Looping through a list <xsl:for-eachselect="group"> <xsl:sortselect="@name"/> <divclass="sgl_row"> <spanclass="sglr_name"> <xsl:value-ofselect="@name"/> </span> <spanclass="sglr_info"> <xsl:attributename="data-id"> <xsl:value-ofselect="@id"/> </xsl:attribute> <xsl:text>More Info</xsl:text> </span> </div> </xsl:for-each>
Embedding Javascriptor CSS <xsl:textdisable-output-escaping="yes"><![CDATA[ <script type="text/javascript"> $(document).ready(function() { alert('Document is ready!'); }); </script> ]]></xsl:text>
Comparison Operators • and Logical-and (&&) author[degree and award] • or Logical-or (||) author[degree or award] • = Equality (==) @attribute = ‘yes’ • != Not equal (!=) @attribute != ‘yes’
Comparison Operators (cont.) • < Less than (<) book[@price < 20] • <= Less than or equal (<=) book[@pages <= 400] • > Greater than (>) book[@price > 20] • >= Greater than or equal (>=) book[@pages >= 400]
XPath Functions • count() • id() • last() • local-name() • name() • namespace-uri() • position() • concat() • contains() • normalize-space() • starts-with() • string() • string-length() • substring() • substring-after() • substring-before() • translate() • boolean() • false() • lang() • not() • true() • ceiling() • floor() • number() • round() • sum()
Counting Tiny Small Groups <span> There are <xsl:value-ofselect="count(//group[@membercount< 6])" /> small groups with less than 6 members. </span>
Debugging for-each loops <xsl:for-eachselect="//group"> <xsl:iftest="$debug = 1"> <div> Processing element number <xsl:value-ofselect="position()" /> of <xsl:value-ofselect="count(//group)" /> </div> </xsl:if> ... </xsl:for-each>
Cheat Sheets • http://www.refcards.com/subject/xml • XPath by DeepX Ltd • XSLT 1.0 Quick Reference by DeepX Ltd