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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:32:49+00:00 2026-05-26T06:32:49+00:00

Given an XML document like this: <r> <a/><b/><c/> <d> <d1/> <d2> <d2a/> <d2b/> <d2c/>

  • 0

Given an XML document like this:

<r>
  <a/><b/><c/>
  <d>
    <d1/>
    <d2>
      <d2a/>
      <d2b/>
      <d2c/>
    </d2>
  </d>
  <e/>
</r>

And given the criteria “Start at b, stop at d2b” is there an XPath expression that can select either:

Ideally:

<c/><d><d1/><d2><d2a/></d2></d>

Reasonably:

<c/>

I know that with the criteria “start at ‘a’ and end at ‘e'” I can use the expression //*[preceding-sibling::a][following-sibling::e]; I’m wondering if there’s a way to do some odd intersection of ancestor axes and preceding siblings to find a common ancestor when the start and end elements are not guaranteed to share the same parent.

  • 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-26T06:32:49+00:00Added an answer on May 26, 2026 at 6:32 am

    XPath (both 1.0 and 2.0) is a query language for XML documents. As such it cannot alter the nodes and structure of any XML document.

    The wanted result can be obtained via an XSLT transformation (I. XSLT 1.0 used below):

    <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:param name="pStart" select="/*/b"/>
     <xsl:param name="pEnd" select="/*/d/d2/d2b"/>
    
      <xsl:variable name="vFollowingStart" select=
      "$pStart/following::* | $pStart/descendant::*"/>
    
     <xsl:variable name="vPrecedingEnd" select=
      "$pEnd/preceding::* | $pEnd/ancestor::*"/>
    
     <xsl:template match="node()|@*" name="identity">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="*">
      <xsl:choose>
          <xsl:when test=
          "count(.|$vFollowingStart) = count($vFollowingStart)
          and
           count(.|$vPrecedingEnd) = count($vPrecedingEnd)
          ">
           <xsl:call-template name="identity"/>
          </xsl:when>
          <xsl:otherwise>
           <xsl:apply-templates/>
          </xsl:otherwise>
      </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document:

    <r>
      <a/><b/><c/>
      <d>
        <d1/>
        <d2>
          <d2a/>
          <d2b/>
          <d2c/>
        </d2>
      </d>
      <e/>
    </r>
    

    the wanted, correct result, is produced:

    <c/>
    <d>
       <d1/>
       <d2>
          <d2a/>
       </d2>
    </d>
    

    Explanation:

    1. The identity rule copies every matched node “as-is”.

    2. There is a single overriding template matching any element.

    3. Inside this template two tests are made: whether the current node belongs to the set of all elements “following the start” and whether the current node belongs to the set of all elements “preceding the end”. If so, the current node is passed to the identity template (copied), otherwise it is ignored (deleted).


    II. XSLT 2.0 solution

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pStart" select="/*/b"/>
     <xsl:param name="pEnd" select="/*/d/d2/d2b"/>
    
      <xsl:variable name="vFollowingStart" select=
      "$pStart/following::* | $pStart/descendant::*"/>
    
     <xsl:variable name="vPrecedingEnd" select=
      "$pEnd/preceding::* | $pEnd/ancestor::*"/>
    
     <xsl:variable name="vWanted" select=
      "$vFollowingStart intersect $vPrecedingEnd"/>
    
     <xsl:template match="node()|@*" name="identity">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="*[not(. intersect $vWanted)]">
      <xsl:apply-templates/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the XML file above, again the same correct result is produced.

    Explanation: Use of the XPath 2.0 operator intersect.


    III. XPath 1.0 solution, selecting just the nodes without altering the document:

    For readability I am providing an XSLT transformation that outputs the result of selecting the wanted nodes. With the same purpose, sub-expressions are defined as variables:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:param name="pStart" select="/*/b"/>
     <xsl:param name="pEnd" select="/*/d/d2/d2b"/>
    
     <xsl:template match="node()|@*">
      <xsl:variable name="vFollowingStart" select=
      "$pStart/following::* | $pStart/descendant::*"/>
    
     <xsl:variable name="vPrecedingEnd" select=
      "$pEnd/preceding::* | $pEnd/ancestor::*"/>
    
      <xsl:copy-of select=
       "$vFollowingStart
          [count(.|$vPrecedingEnd)
          =
           count($vPrecedingEnd)
          ]
       "/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied to the provided XML document (above), the wanted, selected nodes are output:

    <c/>
    <d>
    
       <d1/>
    
       <d2>
    
          <d2a/>
    
          <d2b/>
    
          <d2c/>
    
       </d2>
    
    </d>
    <d1/>
    <d2>
    
       <d2a/>
    
       <d2b/>
    
       <d2c/>
    
    </d2>
    <d2a/>
    

    Explanation: Here I use the Kayessian (by @Michael Kay) formula for the intersection of two node-sets $ns1 and $ns2:

    $ns1[count(.|$ns2) = count($ns2)]
    

    IV. Finally the Xpath 2.0 solution (corresponding to the XPath 1.0 solution):

    I am again using an XSLT (2.0) transformation to copy the results to the output:

    <xsl:stylesheet version="2.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:xs="http://www.w3.org/2001/XMLSchema">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
         <xsl:param name="pStart" select="/*/b"/>
         <xsl:param name="pEnd" select="/*/d/d2/d2b"/>
    
         <xsl:template match="node()|@*">
          <xsl:variable name="vFollowingStart" select=
          "$pStart/following::* | $pStart/descendant::*"/>
    
         <xsl:variable name="vPrecedingEnd" select=
          "$pEnd/preceding::* | $pEnd/ancestor::*"/>
    
          <xsl:sequence select=
           "$vFollowingStart intersect $vPrecedingEnd"/>
         </xsl:template>
    </xsl:stylesheet>
    

    The same results (exactly the wanted nodes) as from the XPath 1.0 solution are produced:

    <c/>
    <d>
            <d1/>
            <d2>
                   <d2a/>
                   <d2b/>
                   <d2c/>
            </d2>
        </d>
    <d1/>
    <d2>
                <d2a/>
                <d2b/>
                <d2c/>
    </d2>
    <d2a/>
    

    UPDATE: Here is a XPath 1.0 solution for the “reasonably” question. Again it is expressed as XSLT stylesheet module, in which, for better readability, subexpressions are defined as separate variables:

    <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:param name="pStart" select="/*/*/b"/>
     <xsl:param name="pEnd" select="/*/*/d/d2/d2b"/>
    
      <xsl:variable name="vFollowingStart" select=
      "$pStart/following::* | $pStart/descendant::*"/>
    
      <xsl:variable name="vcommonAncestor" select=
      "$pStart/ancestor::*
        [count(.|$pEnd/ancestor::*)
        =
         count($pEnd/ancestor::*)
        ][1]
        "/>
     <xsl:variable name="vEndHighestAncestor" select=
      "$vcommonAncestor/*
           [count($pEnd | descendant::*)
           =
            count(descendant::*)
           ]"/>
    
      <xsl:variable name="vPrecedingEnd" select=
      "$vEndHighestAncestor/preceding::*
      |
       $vEndHighestAncestor/ancestor::*"/>
    
    
     <xsl:template match="node()|@*" name="identity">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/">
      <xsl:copy-of select=
      "//*[count(.|$vFollowingStart) = count($vFollowingStart)
          and
           count(.|$vPrecedingEnd) = count($vPrecedingEnd)
          ]
      "/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document (the same as the provided, but wrapped into one more top element, and two children (g and h) added to c — to make it more interesting:

    <t>
    <r>
      <a/><b/><c><g/><h/></c>
      <d>
        <d1/>
        <d2>
          <d2a/>
          <d2b/>
          <d2c/>
        </d2>
      </d>
      <e/>
    </r>
    </t>
    

    the wanted, correct node-set is selected and copied to the output:

    <c>
       <g/>
       <h/>
    </c>
    <g/>
    <h/>
    

    Explanation: This is almost the same as before, but we take as $pEnd its highest ancestor — that is an immediate child of the common ancestor of $pStart and $pEnd.

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

Sidebar

Related Questions

Given an XML document like this: <!DOCTYPE doc SYSTEM 'http://www.blabla.com/mydoc.dtd'> <author>john</author> <doc> <title>&title;</title> </doc>
Given an MLS #, I'd like to get an XML document with details about
Given this XML, what XPath returns all elements whose prop attribute contains Foo (the
Given an XML structure like this: <garage> <car>Firebird</car> <car>Altima</car> <car>Prius</car> </garage> I want to
Given an xml document like <root> <row> <a>2</a> <b>1</b> <c>8</c> </row> <row> <b>5</b> <a>2</a>
Given this source document: <?xml version=1.0 encoding=utf-8?> <config> <group name=global> <globals> <item grp=db prop=userid
I have an XML document like this: <article> <author>Smith</author> <date>2011-10-10</date> <description>Article about <b>frobnitz</b>, crulps
I have an xml sitemap structured like a document tree, such that it looks
I have a given XML document (structure can not be changed) and want to
Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I

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.