Tải bản đầy đủ - 0 (trang)
Appendix B. The XSLT You Need for Office

Appendix B. The XSLT You Need for Office

Tải bản đầy đủ - 0trang

AfterabriefoverviewofwhatXSL-FO,XSLT,andXPathare,

we'lllookatthreeexamplestylesheets.Thefirsttwoillustrate

themostcommonusecaseforXSLT:transformingXML

documentsintoHTML.Thelastexampleconvertsbetweenone

XMLformatandanotherXMLformat.



TheexamplesinthisappendixdonotpertainspecificallytoOffice.For

Office-specificexamplesofXSLTstylesheets,seethemaincontentof

thebook(specificallyChapter3,Chapter4,Chapter5,Chapter7,

Chapter8,andChapter10).ThehighestconcentrationofXSLT

examplesisinChapter3.



B.1SortingOuttheAcronyms

WhenlearningXSLT,therearethreeprimaryspecificationstobe

awareof:XSL(sometimesreferredtoasXSL-FO),XSLT,and

XPath.Originally,thesethreelanguageswerejustpartsofa

singlelanguage,XSL,or"ExtensibleStylesheetLanguage".But

beforebeingreleasedasW3Crecommendations,theywererefactoredintothreeseparatespecifications.Theyessentially

haveasubsetrelationship,asdepictedinFigureB-1.XPathis

usedbyXSLT,whichisinturnusedbyXSL.



FigureB-1.ThesubsetrelationshipbetweenXSL,

XSLT,andXPath



Thisappendixisconcernedonlywiththeinnertwocirclesin

FigureB-1,XSLTandXPath.We'llseehowXSLTandXPath

relatetoeachotherandwhattheyactuallylooklikeinthe

tutoriallateron.First,let'sbrieflylookattheroleeach

languageplays.



B.1.1WhatIsXSL?

Aswe'veseen,XSLstandsfor"ExtensibleStylesheet

Language."ItisalanguageforexpressingstylesheetsforXML.



Itconsistsoftwoprimaryparts:

AnXMLformattingvocabulary

AnXMLtransformationlanguage

ThisappendixisconcernedonlywithXSLT,whichisthe

transformationcomponentofXSL.Theothercomponentthe

formattingvocabularyiscommonlycalledXSLFormatting

Objects,orXSL-FO.Ittheoreticallycanfunctionapartfrom

XSLTasastandaloneformattingvocabulary,butitisusually

usedasapartofXSL(i.e.,withXSLT).Themostcommonuse

caseforXSL-FOistransformingXMLdocumentsintodocuments

suitableforprinting,particularlyinPDFformat.

TheXSL1.0recommendationislocatedat

http://www.w3.org/TR/xsl.



B.1.2WhatIsXSLT?

XSLTstandsfor"ExtensibleStylesheetLanguage

Transformations."ItisalanguagefortransformingXML

documentsintootherXMLdocumentsorotherformats,suchas

HTMLandplaintext.AnXSLTstylesheetisaprogramthat

declarativelydefinesthetransformationfromasourcetree

(input)toaresulttree(output).SinceXSLTstylesheets

themselvesarerepresentedinXMLformat,thatmeansthere

arethreeessentialXMLdocuments,or"trees,"involvedinany

XSLTtransformation.FigureB-2showsadiagramdepictingthe

relationshipsofthesethreetrees.



FigureB-2.ThethreetreesinvolvedineveryXSLT

transformation



TheinputtotheXSLTprocessor(depictedbytherectanglein

FigureB-2)consistsofthesourceXMLdocument(thesource

tree)andtheXSLTprogramitself(thestylesheettree).The

outputofthetransformationisanewXMLdocument(theresult

tree).

TheXSLT1.0recommendationislocatedat

http://www.w3.org/TR/xslt.



TheXSLTandXPathsupportedbyOffice2003arestrictlylimitedto

version1.0.Asofthetimeofthiswriting,XSLT2.0andXPath2.0are

stillworksinprogress.

WhenMicrosoftwasfirstgettingstartedwithXML,itreleaseda

preliminaryversionofXSL(beforeitwasre-factoredasXSLT)asapart

ofInternetExplorer5.Microsofthasdeprecatedthatversionof"XSL,"

andyoushouldavoidit.AnytimeyouseethenamespaceURI

http://www.w3.org/TR/WD-xsl,thedeveloperwasusingthisolder

version,andyou'llhavedifficultiesintegratingthatcodewithnewer

projects.AlwayscheckXSLTcodeanddocumentationtomakesureit

usesthecorrectXSLTnamespaceURI:

http://www.w3.org/1999/XSL/Transform.Formoreinformation,seethe

"UnofficialMSXMLXSLTFAQ"at

http://www.netcrucible.com/xslt/msxml-faq.htm.



B.1.3WhatIsXPath?



XPathisshortfor"XMLPathLanguage."Itisanexpression

languageforaddressingpartsofanXMLdocument.XPathisan

essentialpartofXSLTandisusedtoselect"nodes"intheXML

sourcetreeforfurtherprocessing.

TheXPath"datamodel"isfundamentaltoXSLT.Mathematically

speaking,itdefinestheentiredomainandrangeofXSLT

"functions"inotherwords,theinputtoandoutputfromXSLT

stylesheets.Itdefineswhata"tree"is,thesevenkindsof

nodesthatcanoccurinatree(root,element,attribute,

comment,processinginstruction,namespace,andtextnodes),

andhowtheyrelatetoXML.

BeforewegetintosomeXSLTexamples,let'stakealookat

someexampleXPathexpressions.EachofthefollowingXPath

expressionsiscalledalocationpathandreturnsanobject

calledanode-set.Ratherthanpreciselydefiningitsbehavior,

we'llprovideadescriptionofwhateachexpressionselects.In

thatway,youcanbegintolearnsomeoftheXPathlanguageby

example.



/child::article/child::heading

Selectstheheadingelementchildrenoftherootarticle

element.



/article/heading

Equivalentto/child::article/child::heading.



/article/para[position()=1]

Selectsthefirstparaelementchildoftherootarticle



element.



/article/para[1]

Equivalentto/article/para[position()=1].



/article/para[position()=last()]

Selectsthelastparaelementchildoftherootarticle

element.



/article/para[last()]

Equivalentto/article/para[position()=last()].



self::node()

Selectsthecontextnode.



.

Equivalenttoself::node().



./order

Selectstheorderelementchildrenofthecontextnode.



order

Equivalentto./order.



order/attribute::price

Selectsthepriceattributeofeachorderelementchildof

thecontextnode.



order/@price

Equivalenttoorder/attribute::price.



order[@price>30]/shipTo

SelectstheshipToelementchildrenofeachorderelement

childofthecontextnodewhosepriceattribute'svalueis

greaterthan30.

TheseexamplesonlyillustrateafewofXPath'soperatorsand

functions.Asidefromlocationpaths,XPathalsosupportsthe

operatorsthatyou'dexpecttofindinaprogramminglanguage,

suchasarithmetic(+,-,*,div,andmod)andlogic(and,or).

YoucanuseXPathtodomathandtomanipulatestrings,as

wellastoselectnodes.Foramorethoroughinvestigation(as

wellasanexplanationofhowexactlysuchexpressionsare

evaluated),seeoneofthebookscitedatthebeginningofthis

appendix.We'llcomeacrossafewmoreXPathexpressionsin

thisappendix(inthecontextofXSLT).

XPathexpressionsappearasthevaluesofvariousattributesin

XSLT.Forexample,theselectattributeofanumberofXSLT

instructions(includingthexsl:value-of,xsl:for-each,and



xsl:apply-templateselements)containsanXPathexpression.

TheXPath1.0recommendationislocatedat

http://www.w3.org/TR/xpath.



B.2ASimpleTemplateApproach

Allthreeoftheapproachesinthisappendixincludeasource

document,stylesheet,andresultdocument.



Toexecutetheexamplestylesheetsinthisappendix,youwillneedan

XSLTprocessor.SeethesidebarinChapter3called"Command-Line

Tools"formoreinformationonobtainingandusingacommand-line

XSLTprocessor.



ExampleB-1showsthesourcedocumentforourfirstexample

transformation.



ExampleB-1.AnXMLsourcedocumentcontaining

people'snames





Joe

Johnson





Jane

Johnson







Jim

Johannson





Jody

Johannson







ThestylesheetinExampleB-2looksmuchliketheresult

documentthatitcreates.Specifically,thecontentofthe

elementisessentiallyanHTML

templateoftheresult,alongwithsomeplaceholdersfor

dynamiccontent.Thedynamicpartsofthestylesheetbeloware

highlighted.



ExampleB-2.Averysimplestylesheetfor

combiningpeople'snameswithHTML


xmlns:xsl="http://www.w3.org/1999/XSL/Transform">















Namelist





















Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Appendix B. The XSLT You Need for Office

Tải bản đầy đủ ngay(0 tr)

×
x
GivenNameFamilyName