[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Re: Looping

Subject: Re: [xsl] Re: Looping
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 11 Apr 2012 18:35:28 -0400


To summarize, the key concept you need to master here is "context". This is actually not just an XSLT concept but also an XPath concept. If you do some homework on context in XPath and then in XSLT, what Michele told you will start to seem obvious.

This has all to do with path traversals -- seeing the document as a structure (tree) of nodes, and navigating that structure with a path. What Liam, Ken and Michele have all pointed out is that you need a relative path (which starts from the current context -- each different 'language' element) not an absolute path (which starts from the top of the document and which always gets the same nodes back). "Context" means where you are in the document when you start a path (or indeed along its way) -- something the processor keeps track of for you, but which you need to be aware of and learn to see (in your mind's eye), because it directly affects how you design your paths to work (where they go, how they get there and when they end up nowhere).

It's also key for XSLT, as you'll learn when you instruct yourself in Lesson #2: Templates. For example, in your case the normal thing would be to have this in your stylesheet, or something like it:

<xsl:template match="langmaterial/language">
  <marc:datafield tag="041" ind1=" " ind2=" ">
    <marc:subfield code="a">
      <xsl:value-of select="normalize-space(@langcode)"/>

With this in place, and assuming that other templates are also in place and designed correctly, you don't have to worry about "looping" or even about whether the 'langmaterial' element is there or how many 'language' elements there are (so: no testing for them; that is done implicitly by template matching). If they are there, they will be handled the way the template says, and if they're not, they won't be (and nothing disastrous will happen either).


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

Current Thread