Avatar of N M
N M
Flag for Luxembourg asked on

Retrieve a tag's value using XPATH depending if a different tag exists or not

I have an XML document and I need to read a specific value by calling XPATH.
My problem is that, when a tag A is present (no matter if has value or not, just be present in the document), I have to read tags X1 and X2. Otherwise I have to read another pair of tags.

Example: below, if "mytagA" exists somewhere, I have to search and get X1 ("1000") and X2 ("MAN")

<test>
<component>
  <mytagA>something</mytagA>
</component>
...
<X1>1000</X1>
<X2>MAN</X2>
</test>

Open in new window


However, below the tag "mytagA" does not exist and in this case I need to read Y1 ("123.45") and Y2 ("ORANGE")

<test>
<component>
  <mytagB>another thing</mytagA>
</component>
...
<Y1>123.45</Y1>
<Y2>ORANGE</Y2>
</test>

Open in new window



How can I do this with one XPATH one-line function ?
Reason is I am calling the XPATH from a program, and the program fails because of this "if exists" problem...

Can you help me?
XML* XPath* XSLT

Avatar of undefined
Last Comment
N M

8/22/2022 - Mon
Gertone (Geert Bormans)

Note that your XML is not wellformed

//X1[//mytagA] | //X2[//mytagA] | //Y1[not(//mytagA)] | //Y2[not(//mytagA)]

is a direct translation of what you need to XPath
However that would not be the most efficient solution
For a better solution we need more context
N M

ASKER
Sorry for the orthographic mistake in second XML, I wrote them by hand
Here is the corrected (the "mytagB" closes with an equal "/mytagB" tag)

<test>
<component>
  <mytagB>another thing</mytagB>
</component>
...
<Y1>123.45</Y1>
<Y2>ORANGE</Y2>
</test>

Open in new window


Obviously the three dots "..." imply more tags are there (irrelevant). What more context is needed?


I could provide the logic in form of pseudo code, hoping that could clarify more?


IF tag("mytagA") Exists Then
  return ("./test/X1/text()")
  return ("./test/X2/text()")
ELSE
  IF tag("mytagB") Exists Then       'just checking if exists otherwise no point
    return ("./test/Y1/text()")
    return ("./test/Y2/text()")
  ELSE
    //something went wrong, both mytagA and mytagB missing
  END IF
END IF

Open in new window


Of course, I don't ask for error control (the 'ELSE' is just part of the example).

I am only looking to retrieve X1 and X2 values if mytagA is present, otherwise the Y1 and Y2 values.
Thank you in advance for the help..
SOLUTION
Gertone (Geert Bormans)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
N M

ASKER
Returning to this question, may I ask for a simplification of your above solution:
How to return the content of mytagA based on the value of X1?

Like: If X1 value = '1000' return X2
(this should return 'MAN' )

I think that will solve the whole problem. (I believe you saw it, because you wrote on last comment "...such as testing the text value inside the nodes" - thank you !!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Gertone (Geert Bormans)

Three months without a sign of life and instead of a "thank you for your efforts and suggestions" I get a "can you please change this or that?"

I see what I can do when I get home tonight
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
N M

ASKER
Apologies for the delayed request as well as for not clearly communicated my appreciation - truth is I was trying to put my own effort at work to solve three problems arising from this case. I am definitely much obliged for the time spent on this.

By the way, you solved it. Thanks to your solution I understood how the “union” works and by tackling one tag before the one I need I am now able to read the values.


Many thanks !
N M

ASKER
Thank you for providing a solution not just to a generic problem but also tweaking the solution so that it can be used in many ways.

Thank you for your substantial support!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.