xsl-list
[Top] [All Lists]

Re: [xsl] Deepest element in the tree

2016-08-24 16:58:59
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(_at_)rickquatro(_dot_)com
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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(_at_)frameexpert(_dot_)com







-- 
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.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>