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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:49:02+00:00 2026-05-29T11:49:02+00:00

I have a configuration file in xml, with increasing specificity. That is, the closest

  • 0

I have a configuration file in xml, with increasing specificity. That is, the closest setting to some section is the setting that will be in effect, even if it is set on higher levels. A colleague suggested using XPath for this. But I’m not sure how it would work? I suppose I want to find the “deepest match” for the setting, then automatically backing out if there is no match.

For instance:

<Settings>
 <Foo>Bar</Foo>
 <Products>
  <Bar>Baz</Bar>
  <Product Name="Thingamabob">
   <Foo>Baz</Foo>
  </Product>
  <Product Name="Whizbang">
   <Bar>Foo</Bar>
  </Product>
 </Products>
</Settings>

Searching for the Bar setting for Thingamabob should return “Baz”, but “Foo” on Whizbang. In the same way, Foo has a value of “Baz” for Thingamabob, but “Bar” on Whizbang.

Can this type of lookup be implemented with XPath?

  • 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-29T11:49:03+00:00Added an answer on May 29, 2026 at 11:49 am

    Searching Foo on Thingamabob can be done like so

    //*[@Name='Thingamabob']/ancestor-or-self::*/Foo
    

    But this will give you two nodes, you want to obtain the node with the maximum count(ancestor::*) and then get its text() value

    I don’t know of a way to do max on a xml hierarchy like yours, use an external script to iterate over the node list given by the xpath to find the max depth, then grab its content.

    EDIT:

    You may be able to do this in XSLT 2.0, note I have not tested this.

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    
      <xsl:function name="my:lookup">
        <xsl:param name="name"/>
        <xsl:param name="arg"/>
        <xsl:for-each select="//*[@Name='$name']/ancestor-or-self::*/$arg">
           <xsl:sort select="count(ancestor::*)" data-type="number" order="descending"/>
           <xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if>
        </xsl:for-each>
      </xsl:function>
    
      <xsl:template match="/">
            <xsl:copy-of select="my:lookup('Thingamabob','Foo')" />
      </xsl:template>
    
    </xsl:stylesheet>
    

    EDIT 2:

    I’ve also figure this should work in XPATH 2.0 but I’ve not tested it

    //*[@Name='Thingamabob']/ancestor-or-self::*/Foo[count(ancestor::*) >= //*[@Name='Thingamabob']/ancestor-or-self::*/Foo/count(ancestor::*)]/text()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some values in a configuration file (XML file) with some values like
My application have a configuration xml-file. That file contains more than 50 program settings.
I have a configuration XML file. some nodes are used for computation purpose and
I have an XML configuration file that contains a list of documents with field
Just say I have a file configuration.xml that a php script needs to parse
I have an XML configuration file with a collection of strings like so: <SomeSetting>value</SomeSetting>
I have the following configuration file for NHibernate : <?xml version=1.0 encoding=utf-8 ?> <hibernate-configuration
I have a XML File like that <?xml version=1.0 encoding=utf-8 ?> <Configurations> <EmailConfiguration> <userName>xxxx</userName>
I have a configuration file that I consider to be my base configuration. I'd
I have a small cgi script that fetches and validates a configuration file for

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.