Www.monash.edu.au CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2.
-
Upload
ralph-cunningham -
Category
Documents
-
view
213 -
download
0
Transcript of Www.monash.edu.au CSE3201/CSE4500 Information Retrieval Systems XSLT – Part 2.
www.monash.edu.au
CSE3201/CSE4500 Information Retrieval Systems
XSLT – Part 2
www.monash.edu.au
2
Invoking a Template – by the Node Location
• A template can be referred to by the ‘node’ location or its name.
• By the node location– 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.
www.monash.edu.au
3
Invoking Template by Name
• 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 <xsl:call-templates>
• Call-templates does not change the context node.
• See the callTemplate.xsl.
www.monash.edu.au
4
XML to XML transformation
• To change from one XML format to another XML format, the following construct of XSLT can be used:
– <xsl:output method=“xml”>> To instruct the parser to produce an XML output
– <xsl:copy>> To copy the text node of an element
– <xsl:copy-of>> To copy a subset of XML document
– <xsl:element>> To create a new element
– <xsl:attribute>> To create a new attribute
www.monash.edu.au
5
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.
<xsl:template match="/bookshop"> <html> <body> <h1>Book</h1> <xsl:copy/> </body> </html></xsl:template>
<html> <body> <h1>Book</h1> <bookshop></bookshop> </body> </html>
www.monash.edu.au
6
Copy-of
<?xml version="1.0" encoding="utf-8"?><Author_List> <author>
<initials>JK</initials><surname> Rowling</surname>
</author> <author>
<initials>J</initials><surname> Rowling</surname>
</author></Author_List>
<?xml version="1.0"?><xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:output method="xml"/>
<xsl:template match="/"><xsl:element name="Author_List"><xsl:apply-templates select=“bookshop/book”/></xsl:element></xsl:template>
<xsl:template match="bookshop/book"><xsl:copy-of select="author"/></xsl:template>
</xsl:stylesheet>
www.monash.edu.au
7
Sorting
• Sorting can be done using <xsl:sort>
<xsl:sort select = string-expression lang = { nmtoken } data-type = { "text" | "number" | qname-but-not-ncname } order = { "ascending" | "descending" }
case-order = { "upper-first" | "lower-first" } /> • Example: bookshopSort.xsl
www.monash.edu.au
8
Variables and Parameters
<xsl:variable
name= qname
select= expression
<!–- Content:template -->
</xsl:variable>
<xsl:param
name= qname
select= expression
<!–- Content:template -->
</xsl:param>
www.monash.edu.au
9
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> <xsl:variable name=“Font_Size”>12pt</xsl:variable>
– Without select and empty content.
www.monash.edu.au
10
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
www.monash.edu.au
11
Namespace in XSLT
• Namespace can be used in XSLT.• See namespace.xsl
www.monash.edu.au
12
Combining Multiple XSLTs
• Using <xsl:include> or <xsl:import>• 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 <xsl:apply-imports>• See bookshopWithInclude.xsl and bookshopWithImport.xsl
www.monash.edu.au
13
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/ShowHighlightedExample.aspx?Example=CombiningXMl
• One of the method is to use the <xsl:include>.
www.monash.edu.au
14
Attaching XSLT to XML
• Attaching XSLT to an XML can be done a number of ways:
– Using the processing instruction <?xml stylesheet …/>– 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)
www.monash.edu.au
15
MSXML based Javascript – Load.HTML
<HTML><HEAD> <TITLE>sample</TITLE> <SCRIPT language = "javascript"> 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); } </SCRIPT></HEAD>
<BODY onload = "init()" > <div id="resTree"></div></BODY>
</HTML>