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 July 26th, 2007, 09:08 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl:for-each

Hi Friends,

I have XML which has node like

<main>
  <sec>
   <p>mmm</p>
   <p>vvvv</p>
    <B>
            <p>xxxxx</p>
     </B>
     <p>kgkgk</p>
     <p>khkhkh</p>
   </sec>

<sec>
   <p>mmm</p>
   <p>vvvv</p>
    <B>
            <p>xxxxx</p>
     </B>
     <p>kgkgk</p>
     <p>khkhkh</p>
   </sec>


</main>

When I iterate the above structure with for-each like below, all I am getting a wrong result, precisely speaking all the <p> elements gets copied to one place irrespective of the order of appearance, mean whether they are <p> elements before <B> element or after it:

<xsl:template match="main/sec/p">
<xsl:for-each select="/main/sec/p">
<xsl:value-of select="main/sec/p"/><xsl:text>
</xsl:text>
    <xsl:element name="p">
        <xsl:choose>
            <xsl:when test="position()=1">
                <xsl:attribute name="xx">bch_tx.f</xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <xsl:attribute name="xx">bch_tx</xsl:attribute>
            </xsl:otherwise>
        </xsl:choose>
            <xsl:if test="position()=1">
                <xsl:attribute name="yy">bch_tx.f</xsl:attribute>
            </xsl:if>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>
</xsl:for-each>
</xsl:template>


Can somebody suggest me where I am wrong?

Thanks
PC

__________________
Pankaj
 
Old July 26th, 2007, 10:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This code:

<xsl:template match="main/sec/p">
<xsl:for-each select="/main/sec/p">

says that whenever you find a main/sec/p element you want to process all the main/sec/p elements. You don't want that.

This code:

<xsl:value-of select="main/sec/p"/>

says "starting at the context node" (which is a p element) select the main children, then the sec grandchildren, then the p greatgrandchildren." Your p elements don't have any main children, so this will select nothing.

Read about path expressions and the importance of the context node.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 27th, 2007, 02:26 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Thanks for suggestion. Do you mean that I should use <xsl:value-of select="."/> instead of <xsl:value-of select="main/sec/p"/>. If so, I will try that. But my problem is little bit different, actually when I use this code to iterate the <p> elements, all the "p" elements are being processed (which is quite OK with me, irrespective of their appearance before or after <B> element), but they all are getting copied before <B> element.

I can understand that this is happening as they all are siblings but I want them to remain to their original places.
Any suggestions or should I use the "B/following::sibling::p" "B/preceding::sibling::p" sort of code, which is least desirable.

Pankaj

 
Old July 27th, 2007, 02:47 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm sorry, but if, after reading my reply, you can't see what's wrong with your code then you need to spend a day or two reading a good book or tutorial about the language.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 27th, 2007, 02:58 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Apologies for not understanding. I believe that I know Xpath quite well, but it seems to me I should read more about XSLT.

Any suggestions for a good with lots of examples.

Thanks in advance.






Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:param and xsl:apply-templates' "select" newbieboobers XSLT 1 March 25th, 2008 07:23 PM
Pass link values as xsl:parameter to php5 then xsl pauljr8 XSLT 1 July 2nd, 2007 10:32 PM
differnce between xsl:apply-templates and xsl:call chandu.mca007 XSLT 2 June 12th, 2007 04:12 AM
xsl advise needed - Replacing UID through XSL Billyl XSLT 6 February 28th, 2006 05:46 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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