Xp Tutorial 6 Working With Xslt And Xpath

Transcription

XPTUTORIAL 6WORKING WITH XSLT AND XPATHNew Perspectives on XML, 2nd EditionTutorial 61

XPTHE HISTORY OF XSL In 1998, the W3C developed the Extensible Stylesheet Language, or XSL XSL is composed of two parts:– XSL-FO (Extensible Style sheet Language –Formatting Objects) - layout of paginated documents– XSLT (Extensible Style sheet LanguageTransformations) – to transform contents of an XMLdocument into another document formatNew Perspectives on XML, 2nd EditionTutorial 62

INTRODUCING XSLT STYLE XPSHEETS AND PROCESSORS An XSLT style sheet contains instructions fortransforming the contents of an XML documentinto another format An XSLT style sheet document is itself an XMLdocument An XSLT style sheet document has an extension.xslNew Perspectives on XML, 2nd EditionTutorial 63

GENERATING A RESULTDOCUMENTXP An XSLT style sheet converts a source documentof XML content into a result document by usingthe XSLT processorNew Perspectives on XML, 2nd EditionTutorial 64

INTRODUCING XSLT STYLE XPSHEETS AND PROCESSORS The transformation can be performed by a server or aclient In a server-side transformation, the server receives arequest from a client, applies the style sheet to the sourcedocument, and returns the result document to the client In a client-side transformation, a client requests retrievalof both the source document and the style sheet from theserver, then performs the transformation, and generatesthe result documentNew Perspectives on XML, 2nd EditionTutorial 65

XPCREATING AN XSLT STYLE SHEET To create an XSLT style sheet, the general structure: ?xml version “1.0” xsl:stylesheet version “1.0”xmlns:xsl “http://www.w3.org/1999/XSL/Transform” Content of the style sheet /xsl:stylesheet The xsl:stylesheet tag can be substituted for the xsl:transform tagNew Perspectives on XML, 2nd EditionTutorial 66

WORKING WITH DOCUMENT XPNODES Under XPath, each component in the document is referredto as a node, and the entire structure of the document is anode tree The node tree consists of the following objects:– the source document itself– comments– processing instructions– namespaces– elements,– element text– element attributesNew Perspectives on XML, 2nd EditionTutorial 67

XPNODE TREE EXAMPLENew Perspectives on XML, 2nd EditionTutorial 68

WORKING WITH DOCUMENT XPNODES At the top of the node is the root node A node that contains other nodes is called aparent node, and the nodes contained in theparent are called child nodes Nodes that share a common parent are calledsibling nodes Any node below another node is referred to as adescendant of that nodeNew Perspectives on XML, 2nd EditionTutorial 69

WORKING WITH DOCUMENT XPNODES Nodes are distinguished based on the object theyrefer to in the document A node for an element is called an element node The node that stores element attributes is calledan attribute nodeNew Perspectives on XML, 2nd EditionTutorial 610

XPUSING XPATH TO REFERENCE ANODE XPath provides the syntax to refer to the variousnodes in the node tree The syntax is used by operation system to specify filepathnames The location of a node can be expressed in eitherabsolute or relative terms XPath also does data extractionNew Perspectives on XML, 2nd EditionTutorial 611

XPRELATIVE PATHS With a relative path, the location of the node isindicated relative to a specific node in the treecalled the context nodeNew Perspectives on XML, 2nd EditionTutorial 612

USING XPATH TO REFERENCEXPA NODE For absolute path, XPath begins with the root node,identified by a forward slash and proceeds downthe levels of the node tree An absolute path: /child1/child2/child3/ To reference an element without regard to itslocation in the node tree, use a double forwardslash with the name of the descendant node A relative path : //descendantNew Perspectives on XML, 2nd EditionTutorial 613

REFERENCING GROUPS OFELEMENTSXP XPath allows you to refer to groups of nodes by usingthe wildcard character (*) To select all of the nodes in the node tree, you can usethe path://*The (*) symbol matches any node, and the (//)symbolmatches any level of the node treeExample: /portfolio/stock/*New Perspectives on XML, 2nd EditionTutorial 614

REFERENCING ATTRIBUTENODESXP XPath uses different notation to refer to attributenodes The syntax for attribute node is:@attributewhere attribute is the name of the attributeExample: /portfolio/stock/name/@symbolNew Perspectives on XML, 2nd EditionTutorial 615

XPWORKING WITH TEXT NODES The text contained in an element node is treated asa text node The syntax for referencing a text node is:text() To match all text nodes in the document, use://text()New Perspectives on XML, 2nd EditionTutorial 616

XPCREATING THE ROOT TEMPLATE A template is a collection of elements that definehow a particular section of the source documentshould be transformed in the result document The root template sets up the initial code for theresult documentNew Perspectives on XML, 2nd EditionTutorial 617

XPCREATING A TEMPLATE To create a template, the syntax is: xsl:template match “node set” styles /xsl:template –where node set is an XPath expression that references anode set from the source document and styles are theXSLT styles applied to those nodesNew Perspectives on XML, 2nd EditionTutorial 618

XPCREATING A ROOT TEMPLATE To create a root template, the syntax is: xsl:template match “/” styles /xsl:template New Perspectives on XML, 2nd EditionTutorial 619

XPCREATING THE ROOT TEMPLATE A template contains two types of content: XSLTelements and literal result elements– XSLT elements are those elements that are part of theXSLT namespace and are used to send commands tothe XSLT processor– A literal result element is text sent to the resultdocument, but not acted upon by the XSLT processorNew Perspectives on XML, 2nd EditionTutorial 620

CREATING THE ROOTTEMPLATE EXAMPLENew Perspectives on XML, 2nd EditionTutorial 6XP21

SPECIFYING THE OUTPUTMETHODXP By default, the XSLT processor will render the resultdocument as an XML file To control how the processor formats the sourcedocument, you can specify the output method using the xsl:output attributes/ element – where attributes isthe list of attributes that define the output format of theresult documentNew Perspectives on XML, 2nd EditionTutorial 622

ATTRIBUTS OF THE XSL:OUTPUT/ ELEMENTNew Perspectives on XML, 2nd EditionTutorial 6XP23

XPTRANSFORMING A DOCUMENT A browser with a built-in XSLT processor allows youto view the result document Alternatively, you can use XML Spy to create theresult document as a separate file, and then view thatfile in your browser Most XSLT processors provide the capability tocreate the result document as a separate fileNew Perspectives on XML, 2nd EditionTutorial 624

VIEWING THE RESULT XPDOCUMENT IN A BROWSER Internet Explorer 6.0 contains built-in XSLTprocessor You can view the results of the transformation byopening the result document in the browserNew Perspectives on XML, 2nd EditionTutorial 625

CREATING AN HTML FILE INXPXML SPYNot used in the course One advantage of creating a separate HTML file isthat it can be viewed in any Web browser You have to regenerate the HTML file every timeyou make a change to the source document, or thestyle sheet The XSLT processor adds one extra line to thedocument that provides additional information to thebrowser about the content of the document and itsencodingNew Perspectives on XML, 2nd EditionTutorial 626

XPEXTRACTING ELEMENT VALUES To insert a node’s value into the result document, thesyntax is:– xsl:value-of select “expression” / – where expression is an expression that identifies the nodefrom the source document’s node tree If the node contains child elements in addition to textcontent, the text in those child nodes appears as wellNew Perspectives on XML, 2nd EditionTutorial 627

INSERTING A NODE VALUE XPEXAMPLENew Perspectives on XML, 2nd EditionTutorial 628

PROCESSING SEVERALELEMENTSXP To process a batch of nodes, the syntax is: xsl:for-each select “expression” / styles /xsl:for-each where expression is an expression that defines thegroup of nodes to which the XSLT and literal resultelements are appliedNew Perspectives on XML, 2nd EditionTutorial 629

PROCESSING SEVERALELEMENTSNew Perspectives on XML, 2nd EditionTutorial 6XP30

XPWORKING WITH TEMPLATES To apply a template in the result document, use theXSLT element– xsl:apply-templates select “expression” / where expression indicates the node template to beappliedNew Perspectives on XML, 2nd EditionTutorial 631

XPCREATING TEMPLATE EXAMPLENew Perspectives on XML, 2nd EditionTutorial 632

USING THE BUILT-INTEMPLATESXP Each node has its own built-in template. The built-in template for element nodes matchesthe document root and all elements in the node tree The built-in template for text nodes matches all textnodes and causes their values to appear in the resultdocument For example, you can add the stock template to thestyle sheetNew Perspectives on XML, 2nd EditionTutorial 633

CREATING THE STOCKTEMPLATE EXAMPLENew Perspectives on XML, 2nd EditionTutorial 6XP34

XPINSERTING ATTRIBUTE VALUES xsl:template match “sName” h3 a href xsl:value-of select “./link” / xsl:value-of select “.” / ( xsl:value-of select “@symbol” / ) /a /h3 /xsl:template Why an error occurs with this type of code?REMEMBER – XML RULES ARE STRICT!New Perspectives on XML, 2nd EditionTutorial 635

XPINSERTING ATTRIBUTE VALUES xsl:template match “sName” h3 a href “{./link}” / xsl:value-of select “.” / ( xsl:value-of select “@symbol” / ) /a /h3 /xsl:template Based on elem attribute “{expresssion}” Where elem name of element; attribute name of element’sattribute; expression Xpath for the value of the attributeNew Perspectives on XML, 2nd EditionTutorial 636

XPSORTING NODE SETS By default, nodes are processed in document order,by their appearance in the document To specify a different order, XSLT provides the xsl:sort element This element can be used with either the xsl:apply-templates or the xsl:for-each elementNew Perspectives on XML, 2nd EditionTutorial 637

XPSORTING NODE SETS The xsl:sort comes with several attributes tocontrol the sort– The select attribute determines the criteria under whichthe context node is sorted– The data-type attribute indicates the type of data– The order attribute indicates the direction of the sorting(ascending or descending)– The case-order – how to handle upper and lower casesNew Perspectives on XML, 2nd EditionTutorial 638

CREATING CONDITIONALNODESXP XSLT supports two kinds of conditional elements:– xsl:if – xsl:choose To apply a format only if a particular condition ismet , use the xsl:if element To test for multiple conditions and display differentoutcomes, use the xsl:choose elementNew Perspectives on XML, 2nd EditionTutorial 639

CREATING CONDITIONALNODES EXAMPLENew Perspectives on XML, 2nd EditionTutorial 6XP40

XPUSING COMPARISONOPERATORS AND FUNCTIONSNew Perspectives on XML, 2nd EditionTutorial 641

XPWORKING WITH PREDICATES Predicates are XPath expressions that test for acondition and create subsets of nodes that fulfillthat condition The predicate can also indicate the position of thenode in the node tree To select a specific position in the sourcedocument, use the position() function combinedwith any XPath expressionNew Perspectives on XML, 2nd EditionTutorial 642

ADDING PREDICATES TO THEXPROOT TEMPLATE EXAMPLENew Perspectives on XML, 2nd EditionTutorial 643

INTRODUCING XSLT STYLE XP SHEETS AND PROCESSORS An XSLT style sheet contains instructions for transforming the contents of an XML document into another format An XSLT style sheet document is itself an XML New Perspectives on XML, 2 nd Edition Tutorial 6 3 document An XSLT style sheet document has an extension .xsl