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 August 2nd, 2012, 06:27 PM
Authorized User
 
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
Default Need Help With Position of Current Node From Root

Hello.

I need to get the position of the current node in relation to the root "/" of the XML document.

Code:
<doc>
    <EOB>
        <DETAIL>
            <DETAILLINE>
                <RCDATA>
                    <RCDATALINE>
                        <TRC>AAAAAAAAA1</TRC>
                    </RCDATALINE>
                    <RCDATALINE>
                        <TRC/>
                    </RCDATALINE>
                </RCDATA>
                <CRCDATA>
                    <CRCDATALINE>
                        <CRCFUNCTION>800</CRCFUNCTION>
                    </CRCDATALINE>
                    <CRCDATALINE>
                        <CRCFUNCTION/>
                    </CRCDATALINE>
                </CRCDATA>
            </DETAILLINE>
            <DETAILLINE>
                <RCDATA>
                    <RCDATALINE>
                        <TRC>WWWWWWWW1</TRC>
                    </RCDATALINE>
                    <RCDATALINE>
                        <TRC/>
                    </RCDATALINE>
                </RCDATA>
                <CRCDATA>
                    <CRCDATALINE>
                        <CRCFUNCTION>900</CRCFUNCTION>
                    </CRCDATALINE>
                    <CRCDATALINE>
                        <CRCFUNCTION/>
                    </CRCDATALINE>
                </CRCDATA>
            </DETAILLINE>
        </DETAIL>
    </EOB>
</doc>
So if my current node is <CRCFUNCTION>800</CRCFUNCTION>, I need to figure out that the position() is 15 (counting back to the /doc element).

I tried this:
xsl:number value="position()" format="1. " level="multiple"/> but the value was 1.

Thanks in advance!

Rita
 
Old August 3rd, 2012, 04:36 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, i can't see where your expected answer of 15 comes from. If I count preceding elements, I get 11. If I count all preceding nodes including whitespace text nodes I get 26.

I see two possible ways of getting 15. Perhaps it's the line number of the node, or perhaps it's the number of preceding tags (counting both start and end tags).

You need to explain your requirement better.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old August 3rd, 2012, 11:15 AM
Authorized User
 
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
Default New Twist on Existing Question

Thanks for your response Michael.

I was thinking about my problem last night.
It's basically very simple. I need to check all the occurrences of <CRCFUNCTION> for a value not = '900' in any of the occurrences. The problem is that <CRCFUNCTION> can be = '' (empty) which satisfies the test of not = '900'.

So I need to test for <CRCFUNCTION> not ='900' and that exact node also not be empty. I thought if I could get the node number starting from root I could use that in the second part of the test where I check for "and != '' ".

The <CRCFUNCTION> can have values of 600, 700, 800, 900 or empty.
What I'm trying to accomplish is call a template if any <CRCFUNCTION> is equal to 600, 700 or 800.

Thanks,
Rita
 
Old August 3rd, 2012, 11:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I can't see how this question relates to your previous question, where you were trying to determine the "position" of a node without saying exacly what you mean by "position".

Finding the node whose position you are interested in seems to be a completely separate question.

You can find the CRCFUNCTION nodes with a value of 600, 700, or 800 using either

Code:
CRCFUNCTION[. = ('600', '700', '800')]
or

Code:
CRCFUNCTION[not(. = ('900', ''))]
whichever you prefer.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference

Last edited by mhkay; August 3rd, 2012 at 11:54 AM..
The Following User Says Thank You to mhkay For This Useful Post:
ritagr (August 3rd, 2012)
 
Old August 3rd, 2012, 12:54 PM
Authorized User
 
Join Date: Apr 2010
Posts: 61
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Thank you Michael for all your help - much appreciated.

This question does not really relate to my previous question - I just had a couple of thoughts as to how to resolve my issue.

Your response made it so simple.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get rid of root node and child node... q1sevast XSLT 6 April 16th, 2012 02:56 AM
How do I show treeview root node sg48 BOOK: Beginning ASP.NET 2.0 and Databases 0 December 12th, 2008 02:49 PM
Position of a node outside current context QuickSilver002 XSLT 2 April 19th, 2007 02:07 PM
Replacing New Root Node fauster XSLT 4 October 6th, 2006 11:17 AM
xpath: position parent of current node orion83 XSLT 1 September 5th, 2005 07:20 AM





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