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 3243458
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:26:09+00:00 2026-05-17T18:26:09+00:00

Look at the following two examples: <foo>some text <bar/> and maybe some more</foo> and

  • 0

Look at the following two examples:

<foo>some text <bar/> and maybe some more</foo>

and

<foo>some text <bar/> and a last <bar/></foo>

Mixed text nodes and bar elements within the foo element. Now I am in foo, and want to find out if the last child is a bar. The first example should prove false, as there are text after the bar, but the second example should be true.

How can I accomplish this with XSLT?

  • 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-17T18:26:10+00:00Added an answer on May 17, 2026 at 6:26 pm

    Just select the last node of the <foo> element and then use self axis to resolve the node type.

    /foo/node()[position()=last()]/self::bar
    

    This XPath expression returns an empty set (which equates to boolean false) if the last node is not an element. If you want to specifically get value true or false, wrap this expression in the XPath function boolean(). Use self::* instead of self::bar to match any element as the last node.

    Input XML document:

    <root>
        <foo>some text <bar/> and maybe some more</foo>
        <foo>some text <bar/> and a last <bar/></foo>
    </root>
    

    XSLT document example:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output method="text"/>
    
    <xsl:template match="foo">
        <xsl:choose>
            <xsl:when test="node()[position()=last()]/self::bar">
                <xsl:text>bar element at the end&#10;</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>text at the end&#10;</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Output of the stylesheet:

    text at the end
    bar element at the end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few procedures, for simplicity sake, look like the following: public string
Moments ago Jeff Atwood said the following on twitter : Look, I love rapid
We mostly tend to following the above best practice. Have a look at String
Look at this image: alt text http://img139.imageshack.us/img139/4488/picture2ep3.png I know how to add UITableView with
I look around and see some great snippets of code for defining rules, validation,
Some things look strange to me: What is the distinction between 0.0.0.0, 127.0.0.1, and
I am looking for some examples of a .bat OR .wsh script that can
The documentation of some JavaScript APIs shows the following snippets as an example of
I have about two macros (and climbing) in my codebase that look like this:
I have the following: <ul id ='foo'> <li><p>hello</p></li> <li><p>hello</p></li> <li><p>hello</p></li> </ul> $('#foo').delegate('li', 'click', function(event)

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.