Re: [xsl] xsl: parsing through specific child nodes

Subject: Re: [xsl] xsl: parsing through specific child nodes
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sun, 7 Sep 2008 22:47:52 +0530
I believe, Mike has suggested quite useful points, which are useful to
think about.

My reply are mentioned below ..

On Sun, Sep 7, 2008 at 5:53 AM, Mohit Anchlia <mohitanchlia@xxxxxxxxx> wrote:
> Will this work if I add one more node inside DEF. Something like:
>
> <ZZ>
>  <ABC>XXXXX</ABC>
>  <DEF>
>      <HIJ>YYYYYY<HIJ>
>  </DEF>
>  </ZZ>

I wrote the template,

<xsl:template match="ZZ">
   <ZZ>
     <xsl:apply-templates select="*" />
   </ZZ>
</xsl:template>

The xsl:apply-template above will cause all child elements of ZZ to be
processed.

I wrote another template after that,

<xsl:template match="*">  [1]
  ....

This template handles any element node. But this template will get
called for element children of, ZZ (for the problem you posted).

Now let's say the template [1] above is handling the element, DEF. Now
if my context node is, DEF and I do, <xsl:when test=". = 'YYYYYY'">
then I am actually checking is, if the string value of DEF is 'YYYYYY'
or not. For your new example above, the string value of element node
DEF is YYYYYY plus some whitespaces. So you should ideally do,
<xsl:when test="normalize-space(.) = 'YYYYYY'">

PS: you should read what the string value of element nodes mean in
XPath data model.

> One more question regarding "<xsl:apply-templates select="*" />". Is
> this going to be applied for each node and attribute also? So for eg:
> if I have <ABC xmnls:ns2="http://";> would "<template match=*>" be
> called for ABC and then xmnls:ns2 also. Reason I am asking is that I
> have scenario where I want to get rid of xmnls:ns2.

If my element, ABC is defined as:

<ABC xmnls:ns2="http://";>

and the node, ABC is supplied to the template rule:

<xsl:template match="*">

then this template has in it's context the element ABC. Now what you
need to do with this element is up to you .. You can choose to copy
this element as it is, or copy just the element and not the namespaces
(or, attributes). Mike has answered how to copy namespaces in XSLT 1.0
and XSLT 2.0.


-- 
Regards,
Mukul Gandhi

Current Thread