Re: [xsl] Same name Elements in more than one node

Subject: Re: [xsl] Same name Elements in more than one node
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 26 Dec 2001 12:51:28 -0500

You are not getting your attributes in output because the expressions you think should refer to those attributes (e.g. <xsl:value-of select="@ENCODINGANALOG"/>) are not evaluating the way you are expecting. A little study of how XPath expressions are evaluated, in particular the notion of the "context node", would help you a great deal.

When you say

<xsl:for-each select="//C02">
      <FONT size="+3">
        <xsl:value-of select=".//PERSNAME[@ENCODINGANALOG='100$a']"/>
        <xsl:value-of select="@ENCODINGANALOG"/> ...

... the context node for the expression "@ENCODINGANALOG" is the C02 node you have selected in the for-each (i.e. the node you are currently processing among the set of C02 elements in the document, //C02). Since no C02 elements have @ENCODINGANALOG attributes, you get none in your output (the node set selected by the value-of instruction is empty).

Try <xsl:value-of select=".//PERSNAME[@ENCODINGANALOG='100$a']@ENCODINGANALOG"/> and you'll get what you want.

But in my experience a beginner's failure to grasp the notion of the context node for XPath often comes in the context of a soft understanding of XSLT's preferred mode of operating, i.e. through templates -- the famous "push" approach to stylesheets vs. the "pull" method which tends to rely much more heavily on xsl:for-each. Your case is certainly one in which a template-based solution would work way better than what you have, since then the context node changes with each template -- meaning you don't have to force it (a strategy which may be exigent, but will soon fail on you in other cases when your data is less regular).

So try:

  <xsl:apply-templates select="//C02"/>
  <!-- there's probably a better way than to select all C02 components
       from the root, but for now ... -->

<xsl:template match="C02">
    <xsl:apply-templates select=".//PERSNAME[@ENCODINGANALOG='100$a']"/>
    <xsl:apply-templates select=".//PERSNAME[@ENCODINGANALOG='700$a']"/>

<xsl:template match="PERSNAME">
    <FONT size="+3">
      <xsl:value-of select="."/>
    <xsl:value-of select="@ENCODINGANALOG"/>

If you are unclear on why this works, what you need to research is template-based processing, and the concept of the context node for the evaluation of XPaths. Mike Kay's book is excellent; I'm also liking Bob DuCharme's "XSL Quickly" for beginners.

Happy holidays!

Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
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

XSL-List info and archive:

Current Thread