17

I have the following paths:

  • /my:company/ns1:Audit/ns1:Visit/ns1:customField10
  • /my:company/ns1:Audit/ns1:Visit/ns1:accountNumber

I want to select ns1:customField10 if it is not empty, otherwise I want to return ns1:accountNumber.

I tried the following:

xml.SelectSingleNode(
    "(/my:cobius/ns1:Audit/ns1:Visit/ns1:customField10 | 
    /my:cobius/ns1:Audit/ns1:Visit/ns1:accountNumber)", ns)

But it is always returning accountNumber. I tried flipping the order of customField10 and accountNumber, but no joy. It still returns accountNumber.

How can I do the equivalent of the coalesce operator in XPath?

Update:

Here's the final code:

xml.SelectSingleNode(
    "(/my:company/ns1:Audit/ns1:Visit/ns1:customField10[normalize-space()] | 
    /my:company/ns1:Audit/ns1:Visit/ns1:accountNumber[not(normalize-space(../ns1:customField10))])", ns)
7
  • Why not do xml.SelectSingleNode() for both nodes and a normal if ... after that?
    – Tomalak
    Commented Jan 25, 2012 at 19:31
  • 1
    It might be easier to answer your question if you told us what the coalesce operator is, or at any rate, which technology you are thinking of that has such an operator. Commented Jan 25, 2012 at 22:11
  • @Tomalak For the same reason I'd use a coalesce operator in SQL or C#. Why not do it inline rather than assign two variables and do a check? Commented Jan 25, 2012 at 22:29
  • 1
    @Michael Kay - I explain what I want to do in the question, but for examples of coalesce see: C#, MSSQL, Oracle Commented Jan 25, 2012 at 22:35
  • 4
    Well, yeah. There really isn't. You could use (node1, node2)[1] in XPath 2.0, but only because XPath 2.0 is sequence-order-oriented. In contrast, XPath 1.0 is document-order-oriented (and it does not have a comma operator) so this will not work there.
    – Tomalak
    Commented Jan 25, 2012 at 23:07

1 Answer 1

6

Your code would work if the node was missing (kind of equivalent to a NULL in SQL). But empty is not missing (like an empty string in SQL).

You can of course add a predicate:

/my:cobius/ns1:Audit/ns1:Visit/ns1:customField10[string-length(.)] | /my:cobius/ns1:Audit/ns1:Visit/ns1:accountNumber
9
  • Actually, more like /foo[string-length()] | /bar[not(/foo[string-length()])]
    – Tomalak
    Commented Jan 25, 2012 at 19:26
  • 2
    | is not some sort of short-circuiting logical-or. It's a union operator. In other words, if both exist, you'll get both, which is not what the OP wants.
    – Wayne
    Commented Jan 25, 2012 at 19:27
  • @lwburk: Not necessarily, see above comment. ;)
    – Tomalak
    Commented Jan 25, 2012 at 19:28
  • 1
    @Tomalak - Although I'd probably prefer [normalize-space()] to [string-length()]
    – Wayne
    Commented Jan 25, 2012 at 19:46
  • 2
    The OP is doing a SelectSingleNode, which returns the first node of the set. Therefore it isn't a problem is the set contains both nodes. There is, however, a caveat which I should have mentionned: the code expects the customField10 to be before accountNumber in the document, since the union operator returns the node in document order.
    – Lucero
    Commented Jan 25, 2012 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.