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?
Searching
FooonThingamabobcan be done like soBut this will give you two nodes, you want to obtain the node with the maximum
count(ancestor::*)and then get itstext()valueI 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.
EDIT 2:
I’ve also figure this should work in XPATH 2.0 but I’ve not tested it