Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 24th, 2008, 07:23 AM
Authorized User
 
Join Date: Oct 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cant get the complete nodepath of the child nodes

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>

<a name = "first">
    <b name ="one"> 5 </b>
    <b name ="two"> 6 </b>
    <a name = "second">
         <b name ="first one"> 1 </b>
         <b name ="second one"> 2 </b>
         <a name = "third">
             <a name = "four">
                <b name= "third one"> 3 </b>
            </a>
            <b name= "fourth one"> 4 </b>
         </a>
    </a>

</a>

------------------------------------------------
I want to display the values in node path hierarchy
First->one=5
First->two=6
First->second->first one = 1
First->second->second one = 2
First->second->third->four->third one = 3
First->second->third->fourth one = 4

------------------------------------------------------
test.xsl



<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:for-each select="a">
        <br/>
        <xsl:value-of select="@name"/>->
        <xsl:call-template name="asearch"/>
        <br/>
</xsl:for-each>
<xsl:call-template name="bsearch"/>

<xsl:template name="asearch">

    <xsl:for-each select="a">

                    <xsl:value-of select="@name"/>->
                    <br/>
                    <xsl:call-template name="asearch"/>
        </xsl:for-each>
        <xsl:call-template name="bsearch"/>

    </xsl:template>

    <xsl:template name="bsearch">
    <xsl:for-each select="b">
            <tr>
                <td>
                    <xsl:value-of select="@name"/>
                </td>
                <td>
                     <xsl:value-of select="."/>
                </td>
            </tr>
    </xsl:for-each>
    </xsl:template>



I wrote a template which yields the values .. but I cant get it in hierarchy as I mentioned before.

any help will be highly appreciated.

kind regards.
rex

 
Old October 24th, 2008, 07:50 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a sample XSLT 1.0 stylesheet:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <html lang="en">
      <head>
        <title>Example</title>
      </head>
      <body>
       [list]
          <xsl:for-each select="*/descendant::*[@name and not(*)]">
            <li>
              <xsl:for-each select="ancestor::*">
                <xsl:value-of select="concat(@name, '->')"/>
              </xsl:for-each>
              <xsl:value-of select="concat(@name, ' = ', .)"/>
            </li>
          </xsl:for-each>
        </ul>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>
When run against your sample input it outputs the following HTML document:
Code:
<html lang="en">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

      <title>Example</title>
   </head>
   <body>
      [list]
         <li>first-&gt;one =  5 </li>
         <li>first-&gt;two =  6 </li>
         <li>first-&gt;second-&gt;first one =  1 </li>
         <li>first-&gt;second-&gt;second one =  2 </li>
         <li>first-&gt;second-&gt;third-&gt;four-&gt;third one =  3 </li>
         <li>first-&gt;second-&gt;third-&gt;fourth one =  4 </li>
      </ul>
   </body>
</html>
--
  Martin Honnen
  Microsoft MVP - XML
 
Old October 24th, 2008, 08:21 AM
Authorized User
 
Join Date: Oct 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks martin for your quick reply.

I learned a bit of ancestor and descendats here. Thanks.

Another question. How can I restrict the ancestors ( sample xml file is a basic one.. I have more complicated original one )..

Lets say .. tags from a->b->c->d->e->f=5 ... I want like from d->e->f=5

Thank you once again.


 
Old October 24th, 2008, 08:32 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I am not sure I understand what you want to achieve now.
Assuming you want to process only 'd' and 'e' ancestor elements you could use
Code:
<xsl:for-each select="ancestor::*[self::d or self::e]">
--
Martin Honnen
Microsoft MVP - XML
 
Old October 24th, 2008, 08:37 AM
Authorized User
 
Join Date: Oct 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What I meant was ..
I dont want to write upto the root .. sondern ..
only upto specific tags.

lets say the hierarchy...-> root->tag1->tag2->tag3->tag4->tag5->value=5
I want it from tag4->tag5->value=5

previous code writes completely ..

I will try the 'self' method and let u know ..

thanks

 
Old October 24th, 2008, 09:16 AM
Authorized User
 
Join Date: Oct 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ancestor with self not working for this task.

So I tried a workaround ( i am c++ guy)

Code:
    <xsl:variable name="temp" select="boolean(false)"/>
    <xsl:for-each select="ancestor::*">
    <xsl:if test="boolean($temp) = boolean(true)">
       <xsl:value-of select="concat(@name, '::')"/>
    </xsl:if>                            
    <xsl:if test="@name = 'tag3'">                            
       <xsl:value-of  select="$temp = boolean(true)"/>
        </xsl:if>
     </xsl:for-each>
but I dont know how to declare a variable .. that is $temp to set as false .. and when the loop reaches the tag3, I set it as true .. so from Tag4.. it will display the result I wanted ..
But my if condition failed.


 
Old October 24th, 2008, 09:53 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Maybe you want
Code:
<xsl:for-each select="ancestor::*[@name = 'tag3' or @name = 'tag4']">
--
  Martin Honnen
  Microsoft MVP - XML
 
Old October 24th, 2008, 10:08 AM
Authorized User
 
Join Date: Oct 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That also not fetching the answer..

I might have confused u ..

The thing is .. I know only 'Tag3'- name,after that- others(Tag4 & Tag5) I dont know ..

I believe, the workaround will work fine ..
but I dont know how to assign a value to a variable or change it to check it in if statement.

sorry to bug u mate.





 
Old October 24th, 2008, 10:16 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think you will need to show us the XML you have and explain clearly how you want to identify the nodes you want to output, then we should be able to solve the problem with XSLT and XPath.
Otherwise we can only guess, last guess:
Code:
<xsl:for-each select="ancestor::*[@name = 'tag3' or ancestor::*/@name = 'tag3']">
--
  Martin Honnen
  Microsoft MVP - XML
 
Old October 24th, 2008, 10:20 AM
Authorized User
 
Join Date: Oct 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you martin for help and time.

Code:
<xsl:for-each select="ancestor::*">    
 <xsl:variable name= "icount" select="position()"/>                         <xsl:if test="$icount &gt; 5">
This workaround works.

I will try ur method now.

anyway .. Thanks for ur time and effort







Similar Threads
Thread Thread Starter Forum Replies Last Post
not able to print out the child nodes eruditionist XSLT 7 October 30th, 2008 10:33 AM
text and child nodes eepyoga XSLT 1 April 8th, 2008 12:45 PM
Retrieveal of child nodes.... nathilson21 Classic ASP XML 0 May 7th, 2007 04:54 AM
Trying to group child nodes aalbetski XSLT 3 November 18th, 2006 01:29 PM
counting child nodes Tomi XSLT 1 September 6th, 2006 03:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.