Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6824291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:53:03+00:00 2026-05-26T21:53:03+00:00

This is my XML: <summary> <test> <name>test1</name> <status>failure</status> </test> <test> <name>test2</name> <status>success</status> </test> <test>

  • 0

This is my XML:

<summary>
<test>
   <name>test1</name>
   <status>failure</status>
</test>
<test>
   <name>test2</name>
   <status>success</status>
</test>
<test>
   <name>test3</name>
   <status>success</status>
</test>
</summary>

If <status> is success, I need to store the position() of <test> node in a variable and fetch it from the next template. I only need the position() of the first match. Is that possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:53:04+00:00Added an answer on May 26, 2026 at 9:53 pm

    I recommend to use a global variable that holds the wanted position:

     <xsl:variable name="vFirstPassedPosition" select=
         "count(/*/test
                    [status='success'][1]
                       /preceding-sibling::test) +1"/>
    

    Because this variable is global (child of <xsl:stylesheet>), it can be referenced from any template without the need for recalculation within the template:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
    
     <xsl:variable name="vFirstPassedPosition" select=
     "count(/*/test
               [status='success'][1]
                      /preceding-sibling::test) +1"/>
    
     <xsl:template match="/">
         <xsl:value-of select="$vFirstPassedPosition"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this sample transformation is applied on the provided XML document:

    <summary>
        <test>
            <name>test1</name>
            <status>failure</status>
        </test>
        <test>
            <name>test2</name>
            <status>success</status>
        </test>
        <test>
            <name>test3</name>
            <status>success</status>
        </test>
    </summary>
    

    the position is accessed from within any template (in this case we have a single template) and output correctly:

    2
    

    In case if the XML document’s structure is unknown/unpredictable, and can have any level of nesting, including nested test elements, then use:

     <xsl:variable name="vFirstPassedPosition" select=
      "count((//test[status='success'])[1]
                      /preceding::test)
      +
       count((//test[status='success'])[1]
                      /ancestor::test)
    
       +1"/>
    

    Do note: It is wrong to use expressions like the following in order to find the first test element the string value of whose status child is "success":

    //test[status='success'][1]       
    
    1. In your case the // pseudo-operator isn’t necessary at all, as the structure of the XML document is statically known and more efficient XPath expressions can be used.

    2. //test[status='success'][1] in general may select more than one element. It selects any test element the string value of whose status child is "success" and which is the first child of its parent. This is one of the most FAQ in XPath. Remember: The XPath [] operator has higher precedence (priority) than the // pseudo-operator. In case you really must use // (for example the exact structure of the XML document is not known in advance), then the proper XPath to select the first such element is:

    :

    (//test[status='success'])[1]
    

    As in many other programming languages (and generally in mathematics) brackets are used to explicitly override the standard operators’ precedence.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the Telerik.Web.UI.xml file I found this entry <member name=P:Telerik.Web.UI.RadDataBoundControl.Skin> <summary>Gets or sets the
Summary The Faces Config Editor in Eclipse does not open when editing faces-config.xml. This
Given this XML ... <ListBucketResult xmlns=http://s3.amazonaws.com/doc/2006-03-01/> <Name>public.rpmware.com</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> <Contents> <Key>0.dir</Key> <LastModified>2008-06-25T16:09:49.000Z</LastModified>
Given this XML: <?xml version=1.0 encoding=utf-8?> <content dataType=XML> <item type=Promotion name=Sample Promotion expires=04/01/2009> <![CDATA[
given this xml: <root> <list> <!-- foo's comment --> <item name=foo /> <item name=bar
We have this XML: <Summary> <ValueA>xxx</ValueA> <ValueB/> </Summary> <ValueB/> will never have any attributes
I have this XML (Flash/AS3): <channel> <title>...</title> <description>...</description> <item><summary>...</summary><detail>...</detail></item> <item><summary>...</summary><detail>...</detail></item> ... </channel> I want
This XML file contained archived news stories for all of last year. I was
Given this XML, what XPath returns all elements whose prop attribute contains Foo (the
For this xml (in a SQL 2005 XML column): <doc> <a>1</a> <b ba="1" bb="2"

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.