140 likes | 298 Views
PCF DATA OUTPUTS. AKIFUMI YAMAMOTO | OMNIUPDATE. Requirements for this Workshop. LET’S MAKE SURE YOU’RE IN THE RIGHT PLACE. Knowledge of basic programming concepts Conditionals (if else), loops (for, foreach), and functions Knowledge of XML and JSON data formats Familiar with XSLT syntax
E N D
PCF DATA OUTPUTS AKIFUMI YAMAMOTO|OMNIUPDATE
Requirements for this Workshop • LET’S MAKE SURE YOU’RE IN THE RIGHT PLACE • Knowledge of basic programming concepts • Conditionals (if else), loops (for, foreach), and functions • Knowledge of XML and JSON data formats • Familiar with XSLT syntax • Basic understanding of XSLT Elements • Basic understanding of XPath axes and expressions • Basic understanding of XPath functions
Workshop Overview • OU Campus and XSLT • Conditionals (if else), loops (for, foreach), and functions • XSLT for data outputs • JSON output methods • fn:xml-to-json() • fn:serialize() • <xsl:map>data object
OU Campus and XSLT • OU Campus uses XSLT • Extensible Stylesheet Language Transformations • Great for transforming A (pcf) into B (anything) • A = OU Campus Templates • PCF – A form of content data in XML format • B = Any output we want • Usually a web output (.html, .php, .aspx, etc) • We can do other outputs (.xml, .json, etc)
Transforming A to B in OU Campus • <?pcf-stylesheet path="/path/to/xsl-file.xsl" title="Home Page" extension="html"?> • The pcf-stylesheet processing instruction determines the stylesheet and the output extension. • Filename cannot be altered. • Multiple outputs can be achieved with multiple pcf-stylesheet declarations. • Visit the Support Site for more information
Using XSLT to Output Data • XML AND JSON • XML Output • Fairly straightforward • XML is similar to HTML, so not much needs to change from web output • JSON Output • A little more work • Essentially an XML to JSON conversion • We will explore this transformation
JSON Output • Use fn:xml-to-json() • Must build an XML data with a specific structure • https://www.w3.org/TR/xpath-functions-31/#json-to-xml-mapping • A bug in 9.7.0.2 for empty objects/arrays: https://saxonica.plan.io/issues/2692 • Use fn:serialize() • fn:xml-to-json()isn’t perfect; has bugs in version 9.7.0.2 • Must construct a map object in XSL • http://www.saxonica.com/documentation/#!functions/fn/serialize
JSON Output Original Data Build Map Convert fn:mapXML Tree xml-to-json() Original Data xsl:mapData Object serialize()
Desired JSON { "updated":"2014-02-04T18:50:45", "uptodate":true, "author":null, "arrayObject": { "arrayOne": [ 299, 322 ],…
fn:xml-to-json() Format • OPTION 1 <map xmlns="http://www.w3.org/2005/xpath-functions"> <string key="updated">2014-02-04T18:50:45</string> <booleankey="uptodate">true</boolean> <null key="author"/> <map key="arrayObject"> <array key="arrayOne"> <number>299</number> <number>322</number> </array>
fn:xml-to-json() Walk Through • Understand the PCF • Add secondary output • Implement in XSLT • Mapping reference: https://www.w3.org/TR/xpath-functions-31/#json-to-xml-mapping • xsl:outputreference: http://www.saxonica.com/documentation/#!xsl-elements/output • xsl:modereference: http://www.saxonica.com/documentation/#!xsl-elements/mode • fn:serializereference: http://www.saxonica.com/documentation/#!functions/fn/serialize
fn:serialize() Format • OPTION 2 <xsl:map> <xsl:map-entrykey="'updated'" select="'2014-02-04T18:50:45'"/> <xsl:map-entrykey="'uptodate'" select="true()"/> <xsl:map-entrykey="'author'"/> <xsl:map-entrykey="'arrayObject'"> <xsl:map> <xsl:map-entrykey="'arrayOne'" select="array{(299, 322)}"/>
<xsl:map> and fn:serialize() Demo • Create an XSL map object using the <xsl:map>construct • http://www.saxonica.com/documentation/#!xsl-elements/map • Utilize the new array data type for arrays • http://www.saxonica.com/documentation/#!expressions/xpath31arrays • Specify the method in the second argument of fn:serialize(): • map{ • 'method': 'json', • 'json-node-output-method': 'html', • 'indent': true() • }
THANK YOU! PLEASE SHARE YOUR FEEDBACK: WWW.SLIDO.COM #OUTC19