We help IT Professionals succeed at work.

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

N M
N M asked
on
107 Views
Last Modified: 2018-12-05
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?
Comment
Watch Question

Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
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 MConsultant

Author

Commented:
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..
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
N MConsultant

Author

Commented:
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 !!
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
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
Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
N MConsultant

Author

Commented:
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 MConsultant

Author

Commented:
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!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions