New Perspectives on XML, 2nd Edition - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

New Perspectives on XML, 2nd Edition

Description:

Title: Tutorial 8 Author: Jamahl Black Last modified by: Ben Setzer Created Date: 6/9/2003 7:37:23 PM Document presentation format: On-screen Show (4:3) – PowerPoint PPT presentation

Number of Views:158
Avg rating:3.0/5.0
Slides: 50
Provided by: Jama71
Category:
Tags: 2nd | xml | edition | new | perspectives

less

Transcript and Presenter's Notes

Title: New Perspectives on XML, 2nd Edition


1
TUTORIAL 8
CREATING ELEMENT GROUPS
2
OBJECTIVES
  • Work with step patterns to create complex node
    sets
  • Create moded templates so that different code can
    be applied to the same nodes
  • Access node sets using ID attributes and keys
  • Organize elements using Muenchian grouping
  • Access secondary source documents

3
WORKING WITH LOCATION PATHS
  • Location path
  • Expression that defines a path for the processor
    to navigate
  • Default navigation direction
  • Descendants only

4
STEP PATTERNS
  • Allow processor to navigate node tree in
    different directions
  • Syntax axisnode-testpredicate
  • Sample childpropertycityCutler

5
WORKING WITH AXES
  • XPath supports 13 possible values for the axis
    part of the step pattern
  • Default is child

6
STEP PATTERN AXES
7
STEP PATTERN AXES CHARTS
8
ELIMINATING DUPLICATES USING STEP PATTERNS
  • Selecting duplicates
  • listings/propertycityprecedingproperty/city
  • Excluding duplicates
  • listings/propertynot(cityprecedingproperty/cit
    y)
  • ltxslapply-templates
  • selectlistings/propertynot(cityprecedingpro
    perty/city)gt
  • ltxslsort selectcity/gt
  • lt/xslapply-templatesgt

9
SELECTING DUPLICATE CITIES
10
SELECTING FIRST OCCURANCE OF EACH CITY
11
Practice
  • P. 453
  • P. 462-463

12
CREATING MODED TEMPLATES
  • Apply different styles to the same node set in
    the source document
  • Syntax
  • ltxsltemplate matchnodemodemodegt
  • styles
  • lt/xsltemplategt
  • Sample ltxsltemplate matchproperty
    modecityListgt

13
CALLING A MODED TEMPLATE
  • Syntax ltxslapply-templates selectnode-setmode
    modegt
  • Sample ltxslapply-templates
  • selectlistings/propertynot(cityprecedingpro
    perty/city) modecityListgt

14
USING A MODED TEMPLATE
15
Practice
  • P. 464-465

16
WORKING WITH IDS
  • Using predicates to match data
  • Sample //property_at_rlnr317087
  • Can be inefficient
  • Processor searches document for matching node
    named property with specified rln attribute
  • Result not stored anywhere
  • Using IDs and keys results in more efficient
    searches

17
WORKING WITH IDS
  • ID is declared in DTD
  • Syntax
  • lt!ATTLIST element attribute ID REQUIREDgt or
    lt!ATTLIST element attribute ID IMPLIEDgt
  • Sample lt!ATTLIST property rln ID REQUIREDgt
  • Requires the processor to verify that all
    attributes declared as IDs have unique values
  • All ID values must be unique even if they belong
    to different elements

18
WORKING WITH IDS
  • Processor creates an index of IDs
  • Using the id function
  • Syntax id(value)
  • Sample id(r317087)

19
PROBLEMS WITH IDS
  • When using non-validating parser, id() function
    returns empty result
  • IDs can only be attributes
  • IDs must be unique across all elements
  • ID values must be valid XML names without spaces
    or special characters

20
WORKING WITH KEYS
  • Keys
  • Are declared in the style sheet, not in the DTD
    of the source document
  • Have names as well as values, allowing the style
    sheet author to create multiple distinct keys
  • Can be associated with node sets that contain
    attribute and element values
  • Can have values that are not limited to XML names

21
CREATING A KEY
  • Syntax
  • ltxslkey namenamematchnode-setuseexpressi
    on/gt
  • Sample
  • ltxslkey namerlnsmatch//propertyuse_at_rln/
    gt

22
USING KEY() FUNCTION
  • Syntax key(name,value)
  • Sample key(rlns,r317087)
  • Keys can point to more than one node

23
Practice
  • P. 469
  • P. 470-471

24
GENERATING IDS
  • Create a unique id for a node
  • Syntax generate-id(node-set)
  • Generated ids are constrained to be
  • Same for the same node set
  • Different for different node sets
  • Can be used to test for equality
  • generate-id(nodes1)generate-id(nodes2)

25
ORGANIZING NODES WITH MUENCHIAN GROUPING
  • First formulated by Steve Muench of the Oracle
    Corporation
  • Uses key() and generate-id() to create groups of
    nodes
  • Usually more efficient than the use of step
    patterns
  • Worth considering when you need to organize data
    from a large source document

26
MUENCHIAN GROUPING STEP-BY-STEP
  • Node set consists of the intersection of node1
    and node2
  • node1generate-id()generate-id(node2)
  • propertygenerate-id()
  • generate-id(//propertycityCutler)

27
MUENCHIAN GROUPING STEP-BY-STEP
28
MUENCHIAN GROUPING STEP-BY-STEP
  • Substitute key() function for node2
  • propertygenerate-id()
  • generate-id(key(cityNames,Cutler))

29
MUENCHIAN GROUPING STEP-BY-STEP
30
MUENCHIAN GROUPING STEP-BY-STEP
  • Limit node2 to one element by selecting first
    node only
  • key(cityNames,Cutler)1
  • propertygenerate-id()
  • generate-id(key(cityNames,Cutler)1)

31
MUENCHIAN GROUPING STEP-BY-STEP
32
MUENCHIAN GROUPING STEP-BY-STEP
  • Define group for all elements, not just one
  • propertygenerate-id()
  • generate-id(key(cityNames,city)1)

33
MUENCHIAN GROUPING STEP-BY-STEP
34
USING MUENCHIAN GROUPING WITH ltXSLFOR-EACHgt
  • ltxslfor-each
  • select"//propertygenerate-id()generate-id
  • (key('cityNames', city)1)"gt
  • ltxslsort select"city" /gt
  • lth1gtltxslvalue-of select"city" /gtlt/h1gt
  • lt/xslfor-eachgt

35
Practice
  • P. 476-477
  • P. 477-478

36
CREATING LINKS WITH GENERATED IDs
  • Can be used to create anchor tags
  • Syntax
  • lta hrefgenerate-id(node)gtlinked textlt/agt
  • ...
  • ltelem idgenerate-id(node)gttarget of
    linklt/elemgt
  • Sample
  • lta href"AE1804Z"gtCutlerlt/agt
  • ...
  • lth1 id"AE1804Z"gtCutlerlt/h1gt

37
Practice
  • P. 480-481

38
WORKING WITH MULTIPLE SOURCES
  • Create a reference to another source document
    Within a style sheet
  • Syntax document(object,base)
  • Sample document(firms.xml)
  • Object is either
  • Xml file name
  • Xml element containing file name
  • Base
  • Defines base URI for resolving relative references

39
WORKING WITH MULTIPLE SOURCES
40
WORKING WITH MULTIPLE SOURCES
  • Referencing elements
  • document(firms.xml)/firms/city
  • Good practice
  • Create a variable for external document
  • Keep track of context node
  • Store values to be matched between documents in
    variables

41
WORKING WITH MULTIPLE SOURCES
42
Practice
  • P. 485
  • P. 485
  • P. 486-487
  • P. 488
  • P. 488-489
  • P. 490-491

43
PLACING DATA INTO A STYLE SHEET
  • Data can be placed directly in style sheet
  • Easier to manage a single file
  • Data should be placed in its own namespace
  • Data must be direct child of ltxslstylesheetgt

44
PLACING DATA INTO A STYLE SHEET
  • Syntax
  • ltxslstylesheet version1.0
  • xmlnsxslhttp/www.w3.org/1999/XSL/Transform
  • xmlnsprefixdata_namespacegt

45
PLACING DATA INTO A STYLE SHEET
  • Sample
  • ltxslstylesheet version"1.0"
  • xmlnsxsl"http/www.w3.org/1999/XSL/Transfom
    xmlnsdata"http//www.data_elements.com"gt

46
PLACING DATA INTO A STYLE SHEET
  • To access stylesheet data
  • Syntax document()
  • Sample document('')/xslstylesheet/dataagents/da
    taagent

47
INSERTING CODE SNIPPETS
  • Can be used to contain standard heading or banner
  • HTML code placed in XHTML file
  • To use
  • ltxslcopy-of selectdocument('heading.html') /gt

48
SUMMARY
  • Step patterns can be used with location paths to
    search the document in different orders
  • Moded templates are used to define different
    instructions to be used with the same node
    pattern
  • IDs and keys are used to create more efficient
    searches
  • IDs can be generated using generate-id

49
SUMMARY
  • Muenchian grouping uses key() and generate-id()
    to efficiently group nodes
  • Multiple XML documents can be used by a
    stylesheet and opened with the document()
    function
  • Data can be inserted directly into the stylesheet
  • Code snippets can be placed in XHTML files and
    imported using document()
Write a Comment
User Comments (0)
About PowerShow.com