[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Deepest element in the tree

Subject: Re: Deepest element in the tree
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 24 Aug 2016 21:58:55 -0000
Re:  Deepest element in the tree
If you want to find the last (in document order) element whose depth
is equal to the maximum element depth in the document, then I believe
this is impossible to select with a single XPath 1.0 expression.

If you can use XSLT 1.0, then below is one possible solution:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

  <xsl:template match="*[not(*)]">
    <xsl:copy-of select=
    "self::*[not(//*[count(ancestor::*) > count(current()/ancestor::*)])
           and not(following::*[count(ancestor::*) >=
count(current()/ancestor::*)])
            ]

    "/>
  </xsl:template>
</xsl:stylesheet>


Cheers,
Dimitre

On Wed, Aug 24, 2016 at 12:46 PM, Rick Quatro rick@xxxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> Hi,
>
> I am trying to find the deepest, last element in an xml file. When I use
> this xpath 1.0 statement:
>
> //*[last()]
>
> it returns all nine of the last elements. I want to get the <p>4</p> element
> because it is the last of the last. Note that I don't know ahead of time
> what element name it will be. I tried this
>
> //*[last()][last()]
>
> but it still returns all of them. Any help would be appreciated. Here is my
> xml:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <topic>
>     <body>
>         <p>
>             <ul>
>                 <li>
>                     <p>1</p>
>                 </li>
>                 <li>
>                     <p>2</p>
>                 </li>
>                 <li>
>                     <p>3</p>
>                 </li>
>                 <li>
>                     <p>4</p>
>                 </li>
>             </ul>
>         </p>
>     </body>
> </topic>
>
> Rick Quatro
> Carmen Publishing Inc.
> 585-366-4017
> rick@xxxxxxxxxxxxxxx
>
>
>
> 



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.