Re: [xsl] Not parsing sub-nodes

Subject: Re: [xsl] Not parsing sub-nodes
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 9 Mar 2001 09:36:27 +0000
Hi Roshan,

> <xslt:transform xmlns:xslt="";
> <xsl:template match="/*">
>     <xsl:value-of select="."/>
>     <xsl:for-each select="company">
>      <xsl:apply-templates select="company"/>
>     </xsl:for-each>
> </xsl:template>
> <xsl:template match="office">
>     <xsl:value-of select="."/>
>     <xsl:apply-templates select="office"/>
> </xsl:template>

The first problem is that the namespace prefix that you've declared
for the XSLT namespace ('xslt') isn't the one that you're using on the
templates in the stylesheet (where you're using 'xsl' instead).  Those
need to be in sync.

The second thing is that you seem to be misunderstanding how
xsl:apply-templates works.  The path that you give in the select
attribute of xsl:apply-templates is resolved relative to the *current

In the xsl:for-each in the first template, then the current node is a
'company' element (a child of the document element, which in your case
seems to be company 'Uno'). The company elements that are iterated
over by the xsl:for-each are therefore the companies 'Duo', 'Tres' and
'Quatro'. The xsl:apply-templates then applies templates to the
company element children of these company elements - there aren't any,
so no templates get applied.

In the xsl:template for the office, again you apply templates to the
'office' children of the current office. You don't appear to have
nested offices inside one another, so again the processor won't find
anything to apply templates to.

What you're after is something along the lines of:

<xsl:template match="company">
   <!-- give the name of the company -->
   <xsl:value-of select="@name" />
   <!-- apply templates to its office child -->
   <xsl:apply-templates select="office" />
   <!-- apply templates to any company children -->
   <xsl:apply-templates select="company" />

<xsl:template match="office">
   <!-- give the location of the office -->
   <xsl:value-of select="@location" />

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread