Download presentation
Presentation is loading. Please wait.
Published byOsborne Briggs Modified over 9 years ago
1
www.monash.edu.au CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2
2
www.monash.edu.au 2 What have we done so far? Using XPath expression in XSLT Basic construct of XSLT script –template –Apply-templates –Conditional statement –Proper usage of conditional statement Creating an HTML output document
3
www.monash.edu.au 3 What next? Understanding context node and its relations to XPATH expression, template, apply-templates and call-templates. Sorting XML to XML transformation –Using copy and copy-of –Creating new elements and attributes –Using variables and parameters –Handling namespace Combining multiple XML documents using XSLT. Using XSLT within a Javascript
4
www.monash.edu.au 4 Invoking a Template – by the Node Location Using Apply-Templates –The apply-templates will cause the template for the node specified in the attribute “select” to be invoked. –The XPATH expression of the “select” attribute in the apply-templates does not have to be equal to the XPATH expression in the attribute “match” of a template. – See the multipleTitles.xsl example. >When we remove the template that match chapter/title. The apply-templates select=“bookshop/book/chapters/chapter/title” will choose the template match=“title” as the template for the title of the chapter.
5
www.monash.edu.au 5 Invoking a Template - by its name Using call-template To enable a template to be called by its name, the template has to be declared using attribute “name=“ instead of “match=“. The named template is invoked by using the Call-templates does not change the context node. See the callTemplate.xsl.
6
www.monash.edu.au 6 Sorting Sorting can be done using The is placed within an –Notes: apply-templates will change the context node, be careful in writing the XPath for the sorting! Example: bookshopSort.xsl
7
www.monash.edu.au 7 XML to XML transformation To change from one XML format to another XML format, the following construct of XSLT can be used: – >To instruct the parser to produce an XML output – >To copy the text node of an element – >To copy a subset of XML document – >To create a new element – >To create a new attribute
8
www.monash.edu.au 8 Making Copies xsl:copy –It does not copy any child nodes that the context node may have. xsl:copy-of –copies all See the copyExample.xsl to see the difference between the xsl:copy and xsl:copy-of. Book Book
9
www.monash.edu.au 9 Copy-of JK Rowling J Rowling <xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/199 9/XSL/Transform'>
10
www.monash.edu.au 10 Variables and Parameters <xsl:variable name= qname select= expression <xsl:param name= qname select= expression
11
www.monash.edu.au 11 Variables/Parameter Values To bind a value to a variable or a parameter can be done in three ways: –Use the attribute select. >The value of the attribute has to be an expression. >The value of the variables is the return object from evaluating the expression. –Use the content of the variable element > 12pt –Without select and empty content.
12
www.monash.edu.au 12 Variables VS Parameter Variable’s value does not change once it has been assigned. Parameter’s value is only a default for the binding. Parameter value may be passed with different value during the invocation of a template –Call template with parameter See the callTemplateParam.xsl
13
www.monash.edu.au 13 Namespace in XSLT Namespace can be used in XSLT. See namespace.xsl
14
www.monash.edu.au 14 Combining Multiple XSLTs Using or Include: –Include allows templates declared externally to be inserted to the host XSLT. Import –Import allows external templates to overwrite the internal templates OR –to use mixed the rules from both external and internal templates. –By default, the rules declared in the imported XSLT has a lower precedence compared to the rules declared in the host (importing) XSLT. –The import templates are invoked using See bookshopWithInclude.xsl and bookshopWithImport.xsl
15
www.monash.edu.au 15 Processing Multiple XML Source Documents There are a number of different ways to combine multiple XML documents using XSLT, see http://www.xmlpitstop.com/Examples/ShowHigh lightedExample.aspx?Example=CombiningXMl http://www.xmlpitstop.com/Examples/ShowHigh lightedExample.aspx?Example=CombiningXMl One of the method is by using the document() function in the XSLT. See the bookshop2Docs.xsl as an example.
16
www.monash.edu.au 16 Attaching XSLT to XML Attaching XSLT to an XML can be done a number of ways: –Using the processing instruction –Using an application or script In a real system, most likely the XSLT is attached to the XML by mean of accessing appropriate library of the language that is used to build the system. In a web-based system, scripting language can be used, eg Javascript (see load.html)
17
www.monash.edu.au 17 MSXML based Javascript – Load.HTML sample function init() { var srcTree = new ActiveXObject("Msxml2.DOMDocument.4.0"); srcTree.async=false; // You can substitute other XML file names here. srcTree.load("iterationTemplate.xml"); var xsltTree= new ActiveXObject("Msxml2.DOMDocument.4.0"); xsltTree.async = false; // You can substitute other XSLT file names here. xsltTree.load("iterationTemplate.xsl"); resTree.innerHTML = srcTree.transformNode(xsltTree); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.