Re: [xsl] Parsing nodes with different depths in XML simultaneously

Subject: Re: [xsl] Parsing nodes with different depths in XML simultaneously
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Wed, 13 Feb 2008 10:04:42 +0000
On 13/02/2008, Ravi Roy <ravi.aroy@xxxxxxxxx> wrote:
> Hi,
>
> I want parse nodes with different depths in XML, but I am not sure how
> to achieve this, for example I have following test.xml :
>
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
>    <sub1>
>        <sub2>
>          <sub3>
>                <sub4 name="xyz" />
>                <sub5 name="abc" />
>                <sub6 name="jkl" />
>                <sub7 name="stu" />
>           </sub3>
>       </sub2>
>      </sub1>
>    <suite name="xyz.123" value="20" />
>    <suite name="567.abc" value="10" />
>    <suite name="345.jkl" value="50" />
>    <suite name="stu.578" value="40" />
> </root>
>
> I am able to make the table like the following, but value in the table
> would be based on the match in suite name, like if string sub4 name
> matches the string in suite name then in the following table I want to
> make the entry of 20 against sub4 :
>
>   -------------------------------------
>   | sub4 | sub5 |sub6 |sub7|
>   |---------|---------|--------|-------|
>   |         |         |        |       |
>   |         |         |        |       |
>
>
> Does someone know how to achieve this in XSL ?

One way is to use a key:

<xsl:key name="suite-by-name" match="suite" use="translate(@name,
'1234567890.',''')"/>

with

<xsl:template match="sub4|sub5...">
  <xsl:value-of select="key('suite-by-name', @name)/@value"/>

The translate() part of the key ensures only the a-z characters are
used - you might need to adjust this if your real world XML doesn't
reflect the example.


cheers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread