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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:48:27+00:00 2026-06-01T10:48:27+00:00

I’ve a XML structure like this: <root> <element> <name>Foo</name> <subelement> <key>1.1</key> <value>Lorem ipsum</value> </subelement>

  • 0

I’ve a XML structure like this:

<root>
    <element>
        <name>Foo</name>
        <subelement>
             <key>1.1</key>
             <value>Lorem ipsum</value>
        </subelement>
        <subelement>
             <key>1.2</key>
             <value>Lorem ipsum dolor</value>
        </subelement>
    </element>
    <element>
        <name>Bar</name>
        <subelement>
             <key>7.3.4</key>
             <value>Seven three four</value>
        </subelement>
        <subelement>
             <key>7.3.8</key>
             <value>Seven three eight</value>
        </subelement>
        <subelement>
             <key>7.1</key>
             <value>Seven one</value>
        </subelement>
    </element>
</root>

What I try to achieve is to remove all <subelement>s except the one with the “highest” key.
I can’t seem to find any way to compare the <key>s of the <subelements>s within a certain <element>.

The resulting XML would look like:

<root>
    <element>
        <name>Foo</name>
        <subelement>
             <key>1.2</key>
             <value>Lorem ipsum dolor</value>
        </subelement>
    </element>
    <element>
        <name>Bar</name>
        <subelement>
             <key>7.3.8</key>
             <value>Seven three eight</value>
        </subelement>
    </element>
</root>

Any hints are very welcome.

  • 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-06-01T10:48:28+00:00Added an answer on June 1, 2026 at 10:48 am

    This XSLT 2.0 transformation works for any number of “key components” in a key and for any possible positive integer value of any key component:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:my="my:my">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
     "subelement
       [not(key eq my:max(../subelement/key))
       or
        key = preceding-sibling::subelement/key
       ]"/>
    
     <xsl:function name="my:max" as="xs:string">
       <xsl:param name="pValues" as="xs:string+"/>
    
       <xsl:sequence select=
        "if(not(distinct-values($pValues)[2]))
           then $pValues[1]
         else
          for $vMax1 in
                 max(for $s in $pValues
                      return
                        xs:integer(substring-before(concat($s,'.'),'.'))
                     ),
    
              $vcntMax1Values in
                 count($pValues[starts-with(., string($vMax1))])
    
            return
              if($vcntMax1Values eq 1)
                then $pValues[starts-with(., string($vMax1))]
                              [1]
                else
                 for $submax in
                         (my:max(for $val in
                                        $pValues[starts-with(., string($vMax1))]
                                                    [contains(., '.')],
                                        $subval in substring-after($val, '.')
                                    return
                                        $subval
                                 )
                          )
                     return
                       concat($vMax1, '.', $submax)
    
        "/>
     </xsl:function>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document (the provided one, extended to be made more interesting):

    <root>
        <element>
            <name>Foo</name>
            <subelement>
                <key>1.1</key>
                <value>Lorem ipsum</value>
            </subelement>
            <subelement>
                <key>1.2</key>
                <value>Lorem ipsum dolor</value>
            </subelement>
        </element>
        <element>
            <name>Bar</name>
            <subelement>
                <key>7.3.4</key>
                <value>Seven three four</value>
            </subelement>
            <subelement>
                <key>7.3.8</key>
                <value>Seven three eight</value>
            </subelement>
            <subelement>
                <key>7.3.8.1</key>
                <value>Seven three eight one</value>
            </subelement>
            <subelement>
                <key>7.3.8.1</key>
                <value>Seven three eight one</value>
            </subelement>
            <subelement>
                <key>7.1</key>
                <value>Seven one</value>
            </subelement>
            <subelement>
                <key>10.1</key>
                <value>Ten one</value>
            </subelement>
            <subelement>
                <key>10.1</key>
                <value>Ten one</value>
            </subelement>
        </element>
    </root>
    

    the wanted, correct result is produced:

    <root>
       <element>
          <name>Foo</name>
          <subelement>
             <key>1.2</key>
             <value>Lorem ipsum dolor</value>
          </subelement>
       </element>
       <element>
          <name>Bar</name>
          <subelement>
             <key>10.1</key>
             <value>Ten one</value>
          </subelement>
       </element>
    </root>
    

    Explanation:

    At the core of this generic solution is the function my:max() which given a non-empty sequence of “structured values” (a structured value is a sequence of positive integers, joined with the ‘.’ character), produces one (of the many possible) maximum value.

    This function is recursive. It does the following:

    1. If all the passed values are identical, then the first of them is returned.

    2. Else, finds the maximum value of the first components.

    3. If only one of the values has a first component with the found maximum value, return this value.

    4. Else, find the maximum (recursively) of the “tails” of all values that have the maximum first component.

    5. Finally, join together the maximum first component with the maximum of the “tails”, found in the previous step — and return this value.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.