I have an XML file that looks something like this:
<PACKAGES>
<PACKAGE>
<SHORT-NAME>Element1</SHORT-NAME>
<PACKAGES>
<PACKAGE>
<SHORT-NAME>Element2</SHORT-NAME>
<ELEMENTS>
<MODULE>
<SHORT-NAME>Element3</SHORT-NAME>
<DESC>
</DESC>
<CATEGORY>Item</CATEGORY>
</MODULE>
</ELEMENTS>
</PACKAGE>
</PACKAGES>
</PACKAGE>
</PACKAGES>
I would like the ability to build an XPath query to the MODULE element with the SHORT-NAME of "Element3", based on the SHORT-NAMES of "Element3"'s parents...so something like this:
//SHORT-NAME='Element1'.//SHORT-NAME='Element2'.//SHORT-NAME='Element3'
I've tried the above query, but it doesn't seem to work, not a valid query. I've also tried this:
//*[text()='Element1']//*[text()='Element2'] etc...
but again, this doesn't seem to be a valid query.
I'd like this to be scale-able such that the specific query could be any path, but the path is always based on the text value of SHORT-NAME.
So something like:
/Element1/SubElement2/SubSubElement3/SubSubSubElement4
could also be queried for.
An important note: For the above query, I ONLY want Element3 IF it is a child of Element2, who is a child of Element1. IF Element3 exists elsewhere in the document, I do NOT want that node.
I'm hoping this is possible I'm just not building the right query, but I'm at a loss for how to even search for this topic to try to find the answer.
EDIT: The answer provided below by Andersson almost 100% works for my use-case. The issue is highlighted below:
If I still need to query /Element1/Element2/Element3 but my XML looks like below:
<PACKAGES>
<PACKAGE>
<SHORT-NAME>Element1</SHORT-NAME>
<PACKAGES>
<PACKAGE>
<SHORT-NAME>Element2</SHORT-NAME>
<ELEMENTS>
<PACKAGE>
<SHORT-NAME>RandomElement</SHORT-NAME>
<MODULE>
<SHORT-NAME>Element3</SHORT-NAME>
<DESC>
</DESC>
<CATEGORY>Item</CATEGORY>
</MODULE>
</PACKAGE>
</ELEMENTS>
</PACKAGE>
</PACKAGES>
</PACKAGE>
</PACKAGES>
Element3 is still returned, however Element3's parent is not Element2. This makes thing more complicated and I suspect I will need to create a function that systematically goes through a search for the next child SHORT-NAME and to check if the for query is satisfied. Unless the XPath query could be modified in such a way to satisfy the above use-case.
moduleitem on other levels.//modulewill find it"Element3"which is descendant of node with value"Element2"which is descendant of node"Element1"?//*[SHORT-NAME='Element1']and then find.//*[SHORT-NAME='Element2']etc. while xpath will return items