2

Suppose I have an XML file -

<Cars>
            <Year> 2021 </Year>
            <Model>
                <Name> Porsche 911 </Name>
                <Year> 2020 </Year>
            </Model>
            <Model>
                <Name> Porsche 911 </Name>
                <Year> 2019 </Year>
            </Model>
            <Model>
                <Name> Porsche Cayenne </Name>
                <Year> 2019 </Year>
            </Model>
</Cars>

Now I want to extract the Year 2021 which is not the Year of that particular model but the date when this XML was written (It is the outer one)

But to do that.. due to restraints on the path not being hardcoded, I was using //Year//text() but it gives out the output as [2021,2020,2019,2019]. I don't want that because if the order is changed, the index changes and that would create a problem.

Please help with a XPath command where I can exclude the inner Year tags and only use the outer one.

Mud
  • 21
  • 5

1 Answers1

2

Try this one to get only outer Year

//Year[not(preceding-sibling::Name)]/text()

If required Year node (nodes) is always direct child of Cars then

//Cars/Year/text()

should also be OK

JaSON
  • 4,843
  • 2
  • 8
  • 15
  • 1
    Depending on how `Cars` varies over time and other occurrences, maybe just `/Cars/Year[1]/text()` would suffice. – kjhughes Mar 24 '21 at 13:26
  • 1
    @kjhughes yes. I assumed that spaces between `Cars` and `Year` in provided XML sample might mean that there are more "interim" nodes – JaSON Mar 24 '21 at 13:57
  • @kjhughes but what if that year is mentioned at the end and not at the top. I can't hardcode it – Mud Mar 24 '21 at 14:48
  • @Mud: In your posted example XML, `Year` appears multiple times as a direct child of `Cars`. What is the distinguishing, invariant characteristic of the particular `Year` that you want? (If you intended your indentation to represent a child relationship, the XML itself belies that intention. In that case, fix the XML to reflect the actual hierarchy, and indent properly.) – kjhughes Mar 24 '21 at 14:53
  • One ```Year``` is a direct child of Cars whereas the other ```Year```s are children of Model. Yeah sorry my bad, there was an error in the representation I've corrected it now – Mud Mar 24 '21 at 15:14
  • If there's ever only a single `Year` child of `Cars`, and that's the `Year` element whose text you want, then the solution is simple: `/Cars/Year/text()`. – kjhughes Mar 24 '21 at 17:22