xsl-list
[Top] [All Lists]

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

2008-02-13 03:05:11
On 13/02/2008, Ravi Roy <ravi(_dot_)aroy(_at_)gmail(_dot_)com> 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/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>